The ip command is used to assign an address to a network interface and/or configure network interface parameters on Linux operating systems. It replaces old good and now deprecated ifconfig command on modern Linux distributions. Though, ifconfig command still works and available on most Linux distros.

The ip command

NAME
       ip - show / manipulate routing, devices, policy routing and tunnels

SYNOPSIS
       ip [ OPTIONS ] OBJECT { COMMAND | help }

What the ip command can do

Similar to ipconfig, the ip command to display and configure the network parameters for host interfaces.

Find out which interfaces are configured on the system.
Query the status of a IP interface.
Configure the local loop-back, Ethernet and other IP interfaces.
Mark the interface as up or down.
Configure and modify default and static routing.
Configure tunnel over IP.
Configure ARP or NDISC cache entry.
Assign IP address, routes, subnet and other IP information to IP interfaces.

Some IP Command Examples

How to Assign an IP Address to a Specific Interface

Assign IP Address to a specific interface (eth0) on the run.

# ip addr add 192.168.1.2 dev eth0
or
$ sudo ip addr add 192.168.1.2 dev eth0

Note: Unfortunately all these settings will be lost after a system restart.

Check all network interfaces

To get the depth information of your network interfaces like IP Address, MAC Address information:

# ip addr show
or
$ ip a # show all network interfaces
$ ip addr # show all network interfaces

# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9000 qdisc mq state UP qlen 1000
    link/ether 00:21:28:4e:e3:da brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.5/24 brd 10.0.20.255 scope global eth0
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:21:28:4e:e3:db brd ff:ff:ff:ff:ff:ff

How to check one network interfaces/one type

#Only show TCP/IP IPv4

ip -4 a 

# Only show TCP/IP IPv6

ip -6 a

List particular interface TCP/IP details:
#Only show eth0 interface

ip a show eth0
ip a list eth0
ip a show dev eth0

#Only show running interfaces

ip link ls up

How to Remove an IP Address

# ip addr del 192.168.1.2/24 dev eth0
$ sudo ip addr del 192.168.1.2/24 dev eth0

How to Enable/disable Network Interface

The “up” flag with interface name (eth2) enables a network interface. While 'up' flag with interface name disables a network interface.

# ip link set eth0 up
$ sudo ip link set eth0 up
# ip link set eth0 down
$ sudo ip link set eth0 down

How do I change the txqueuelen of the device?

You can set the length of the transmit queue of the device:

ip link set txqueuelen {NUMBER} dev {DEVICE}

In this example, change the default txqueuelen from 1000 to 10000 for the eth0:
ip link set txqueuelen 10000 dev eth0
ip a list eth0

How do I change the MTU of the network interface

For gigabit/10G networks you can set maximum transmission units (MTU) sizes (JumboFrames) for better network performance.

The syntax is:

ip link set mtu {NUMBER} dev {DEVICE}

To change the MTU of the device eth0 to 9000, enter:

ip link set mtu 9000 dev eth0
ip a list eth0

Flush ARP entry

This flush or f command flushes neighbour/arp tables, by specifying some condition.

The syntax is:

ip -s -s n f {IPAddress}

In this example, flush neighbour/arp table

ip -s -s n f 10.10.2.4

OR

ip -s -s n flush 10.10.2.4

Flush the IP address from the interface

You can delete or remote an IPv4/IPv6 address one-by-one. However, the flush command can remove as flush the IP address as per given condition.

For example, you can delete all the IP addresses from the private network 10.10.2.5/24 using the following command:

ip -s -s a f to 10.10.2.5/24

How do I Check Route Table?

Check the routing table information of system.

# ip route show
$ sudo ip route show

# ip route show
10.0.2.0/24 dev eth0  proto kernel  scope link  src 10.0.2.5
192.168.1.0/24 via 10.0.2.254 dev eth0
10.0.1.0/24 via 10.0.2.254 dev eth0
default via 10.0.2.245 dev eth0

Add and Delete Static Route

If you want the traffic not pass through the default gateway, then you need to add Static routes to pass traffic from best way to reach the destination.

Note: route will be lost after a system restart

# ip route add 10.10.20.0/24 via 192.168.1.100 dev eth0
$ sudo ip route add 10.10.20.0/24 via 192.168.1.100 dev eth0

To remove assigned static route, simply type the following command.

# ip route del 10.10.20.0/24
$ sudo ip route del 10.10.20.0/24

Add Persistence Static Routes

To add permanent Static route, you have to change the static route config file, edit file /etc/sysconfig/network-scripts/route-eth0. By default route-eth0 file will not be there, need to be created.

# vi /etc/sysconfig/network-scripts/route-eth0
10.10.20.0/24 via 192.168.1.100 dev eth0

For Ubuntu/Debian/Linux

Open the file /etc/network/interfaces and at the end add the persistence Static routes. IP Addresses may differ in your environment.

$ sudo vi /etc/network/interfaces

Add the following line for the new static route

up ip route add 10.10.20.0/24 via 192.168.1.100 dev eth0

Next, restart network services.

# /etc/init.d/network restart
$ sudo /etc/init.d/network restart

Add Default Gateway

Default gateway can be specified globally or for in interface-specific config file. Advantage of default gateway is If we have more than one NIC is present in the system. You can add default gateway on the fly as shown below command.

# ip route add default via 10.10.2.254
$ sudo ip route add default via 10.10.2.254

 

Comments powered by CComment