Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»Linux ip Command and Examples

    Linux ip Command and Examples

    RahulBy RahulJuly 14, 20218 Mins ReadUpdated:July 15, 2021

    Linux ip command is a command-line utility used for show/update ip address, kernel routing tables, network devices, interfaces, and tunnels. This command is a part of iproute2 package. With the help of ip command, the system administrators assign an address to a network interface and/or configure network parameters on an interface.

    IP (Internet Protocol) address is a numerical label assigned to a network interface of a computer that allows the communication of the system over the internet. On a computer network, a system communicates with another system by its IP address.

    The older operating systems were uses the ifconfig. Which was also a powerful command, is deprecated on newer Linux systems, and replaced by the ip command. But, You can also use the ifconfig command on modern OS/Distros by installing net-tools package..

    In this tutorial, you will learn about the uses of the Linux ip command with useful examples.

    How to Use ip Command

    The modern Linux systems provide iproute2 package, which replaces the various commands provided by the net-tools package. Also, the ip command comes under this packages, with great features.

    Syntax:

    The ip command uses the following syntax:

    ip [ OPTIONS ] OBJECT { COMMAND | help }
    

    Remember that configurations created with this command like IP addresses, routes, and policy routing rules (and so on) are not persistent. Make changes permanently, you need to edit the appropriate configuration to your platform/OS.

    Get Help

    Just type “help” after the ip command to view commands uses details.

    ip help 
    
    Output:
    Usage: ip [ OPTIONS ] OBJECT { COMMAND | help } ip [ -force ] -batch filename where OBJECT := { link | address | addrlabel | route | rule | neigh | ntable | tunnel | tuntap | maddress | mroute | mrule | monitor | xfrm | netns | l2tp | fou | macsec | tcp_metrics | token | netconf | ila | vrf | sr | nexthop } OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] | -h[uman-readable] | -iec | -j[son] | -p[retty] | -f[amily] { inet | inet6 | mpls | bridge | link } | -4 | -6 | -I | -D | -M | -B | -0 | -l[oops] { maximum-addr-flush-attempts } | -br[ief] | -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] | -rc[vbuf] [size] | -n[etns] name | -N[umeric] | -a[ll] | -c[olor]}

    In addition, use the help command to get object specific help, like:

    ip addr help 
    

    Similarly, try below command to get help for route object subcommand.

    ip route help 
    

    Try the help command with other objects to get help.

    Next, you will learn about the uses of the ip command. For the learning purpose, use a system physically accessible. In other words, any wrong command may down the network interface and you may disconnect from the remote systems. But, the physical system still be accessible.

    View and Manage Network Interfaces

    Use link subcommand with ip command to manage and display the state of all network interfaces. The Name of the network interfaces may differ based on the Linux distributions and hardware platforms etc.

    1. View all available network interfaces

      The ip link default command shows the details of all interfaces.

      ip link 
      [OR]
      ip link show 
      
      Output:
      1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 6e:dc:96:05:82:a8 brd ff:ff:ff:ff:ff:ff
    2. View specific network interfaces

      Define the network interface name to view information about the specific one.

      ip link show eth0
      
      Output:
      2: eth0: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 6e:dc:96:05:82:a8 brd ff:ff:ff:ff:ff:ff
    3. View statics of a network interface

      Use -s option to view the network interface statics. It shows all details about the packet and data sent and receive on that interface.

      ip -s link show eth0
      
      Output:
      2: eth0: mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 6e:dc:96:05:82:a8 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 5746680521 36516527 0 0 0 0 TX: bytes packets errors dropped carrier collsns 33339856329 33119340 0 0 0 0

      Remove the interface name (eth0) to view statics for all the available network interfaces.

    4. Bring UP and Down Network Interfaces

      Use the ip link set command to alter the network interface state. Here up is used to bring the network interface online.

      ip link set eth0 up
      

      Use down option to bring the network interface offline

      ip link set eth0 down
      

      Note: While working with remote systems, don’t bring down the interface. It may disconnect your system from the network.

    Display and Add IP Address on Network Interfaces

    The ip addr subcommand is used for displaying the IP address configured on network interfaces. You can also use the same command to set an IP address on a network interface.

    1. Display IP Address of all interfaces

      The default ip addr subcommand shows the IP addresses details on all interfaces. You can also include show is optional, while viewing the IP details of all network interface.

      ip addr 
      [OR]
      ip addr show 
      
      Output:
      1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 6e:dc:96:05:82:a8 brd ff:ff:ff:ff:ff:ff inet 192.168.10.110/24 brd 192.168.10.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::6cdc:96ff:fe05:82a8/64 scope link valid_lft forever preferred_lft forever

      You can also display only IPv4 or IPv6 ip addresses by using ip -4 addr or ip -6 addr commands.

    2. Display IP address of a single interface:

      Specify the interface name with ip addr show subcommand to display IP address of a specific interface.

      For example, view the IP address on eth0 network interface:

      ip addr show dev eth0
      
      Output:
      2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 6e:dc:96:05:82:a8 brd ff:ff:ff:ff:ff:ff inet 192.168.10.110/24 brd 192.168.10.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::6cdc:96ff:fe05:82a8/64 scope link valid_lft forever preferred_lft forever
    3. Assing IP address of an interface:

      The ip addr add subcommand is used to add an address on a network interface. The below command will assign ip address 192.168.10.100 with netmask 24 on eth0 network interface.

      ip addr add 192.168.10.100/24 dev eth0
      
    4. Assing multiple IP address of an interface:

      In addition, you can assign more IP address to a single interface. Use same command as abvoe to assign multiple IP address on the single network interface. For example:

      ip addr add 192.168.10.100/24 dev eth0
      ip addr add 192.168.10.105/24 dev eth0
      
    5. Remove IP address from interface:

      If you think, that an IP address is no more required or adding by mistake can be removed from the interface. Use the ip addr del subcommand to delete an IP address from any network interface.

      For example, use the following command to remove IP address 192.168.10.105 with 24 subnet mask from eth0 interface

      ip addr del 192.168.10.105/24 dev eth0
      

    Display and Alter the Routing Table

    The ip command also provides you the option to view or change the routing table of any network interface.

    Use ip route subcommand to work with the routing tables.

    1. List kernel routing table

      By default ip route command lists all the routing entries in the kernel. You can also prefix show option to view the same results.

      ip route 
      
      Output:
      default via 192.168.10.1 dev eth0 proto static 192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.1

      The above results show that 192.168.10.1 is the default gateway used by the kernel via the eth0 network interface.

    2. Add an entry to the routing table

      • Add a default route (for all addresses) via the local gateway 192.168.10.1 that can be reached on device eth0
        ip route add default via 192.168.10.1 dev eth0 
        
      • Add a route to 192.168.10.0/24 via the gateway at 192.168.10.1
        ip route add 192.168.10.0/24 via 192.168.10.1 
        
      • Add a route to 192.168.10.0/24 that can be reached on device eth0
        ip route add 192.168.10.0/24 dev eth0 
        
    3. Delete a routing table entry

      Use the following command to delete the route for 192.168.10.0/24 via the gateway at 192.168.10.1

      ip route delete 192.168.10.0/24 via 192.168.10.1 
      
    4. Replace, or add if not defined, a route

      Replace an exiting route defined for 192.168.10.0/24 to use device eth0

      ip route replace 192.168.10.0/24 dev em1 
      
    5. Display the route an address will take

      This command is very useful during the troubleshooting of the network. In other words, you can find the route used by the system to reach a defined destination IP address.

      ip route get 8.8.8.8 
      
      Output:
      8.8.8.8 via 192.168.10.1 dev eth0 src 192.168.10.108 uid 0

    Conclusion

    In conclusion, you have learned about Linux ip command with useful examples. Here we describe, how to manage a network interface on Linux systems. Adding or deleting the kernel routing table entries. Make a network interface UP (online) and down (offline). Adding, remove or display the IP addresses on a network interface.

    command ifconfig ip networking
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous Article(Resolved) – /bin/rm: Argument list too long
    Next Article How To Setup FTP Server with VSFTPD on Ubuntu 20.04

    Related Posts

    Configuring the Static IPv4 Address on Ubuntu using Netplan

    3 Mins Read

    How to Check IPv4 Address on Ubuntu 22.04

    2 Mins Read

    How to Configure Static IP Address on Ubuntu 22.04

    Updated:June 30, 20223 Mins Read

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.