Iproute2 command to enable a IPv4 address

ipiproutenetworking

I am investigating how to have iproute2 commands replace the old ifconfig and ifup ifdown command, and I found out something interesting.

My NIC setup is:

[16:07:41 root@vm network-scripts ]# cat /etc/sysconfig/network-scripts/ifcfg-eth2

DEVICE=eth2
ONBOOT=no
BOOTPROTO=dhcp

To bring up and down an interface, the old way will be:
ifup eth2
ifdown eth2

[16:25:10 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff

[16:25:14 root@vm network-scripts ]# ifup eth2

Determining IP information for eth2... done.

[16:25:22 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.4/24 brd 192.168.1.255 scope global eth2
    inet6 fe80::a00:27ff:feb8:13b4/64 scope link
       valid_lft forever preferred_lft forever

[16:25:26 root@vm-cention network-scripts ]# ifdown eth2

[16:27:51 root@vm-cention network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff

To use iproute2 command to do this, normally we use ip link set eth2 up, but apparently the iproute2 can only bring up the link layer of the NIC, but not the IP address:

[16:36:25 root@vm network-scripts ]# ip link set eth2 up

[16:37:16 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:feb8:13b4/64 scope link
       valid_lft forever preferred_lft forever

[16:37:20 root@vm network-scripts ]# ping yahoo.com
ping: unknown host yahoo.com

But the traditional ifup can do that:

[16:37:39 root@vm network-scripts ]# ifup eth2

Determining IP information for eth2... done.

[16:39:59 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.4/24 brd 192.168.1.255 scope global eth2
    inet6 fe80::a00:27ff:feb8:13b4/64 scope link
       valid_lft forever preferred_lft forever

[16:40:04 root@vm network-scripts ]# ping yahoo.com
PING yahoo.com (98.139.183.24) 56(84) bytes of data.
64 bytes from ir2.fp.vip.bf1.yahoo.com (98.139.183.24): icmp_seq=1 ttl=43 time=243 ms
64 bytes from ir2.fp.vip.bf1.yahoo.com (98.139.183.24): icmp_seq=2 ttl=43 time=341 ms

I think this is due to ifup bring up the link layer, and also the IPv4 address together.

So my question is: how do we use iproute2 to enable the IPv4 address as well ?

Side note:
Interestingly, when iproute2 bring down the link layer, it doesn't disable the IPv4 address:

[16:42:50 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.4/24 brd 192.168.1.255 scope global eth2
    inet6 fe80::a00:27ff:feb8:13b4/64 scope link
       valid_lft forever preferred_lft forever

[16:42:58 root@vm network-scripts ]# ip link set eth2 down

[16:43:04 root@vm network-scripts ]# ip a show eth2
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 08:00:27:b8:13:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.4/24 brd 192.168.1.255 scope global eth2

[16:43:09 root@vm network-scripts ]# ping yahoo.com
ping: unknown host yahoo.com

Best Answer

ip and ifup serve different purposes, and are complimentary. ip should not be used to replace ifup. Actually, ifup operates at a higher level.

ifconfig (traditional, portable) and ip (Linux-only, but much better interface) are two commands that serve the same purpose. They are for setting interface configuration directly. ip does indeed fully replace ifconfig (and route and some of netstat) because of its much nicer interface and much wider capabilities, except that ifconfig remains for compatibility.

Neither ip nor ifconfig contain or manage persistent configuration. They apply the request they get on the command line, and that's it.

ifup and ifdown are for bringing interfaces up and down according to the system coniguration. On some systems this configuration is kept in /etc/network/interfaces, on others it's in /etc/sysconfig/something. Their job is to read the whole configuration, including IP addresses, routes, DNS servers, custom scripts, etc..., and apply it to the system. They do this (at least conceptually) by calling ip or ifconfig.

You can manually execute all of the ip commands that ifup would have used to bring up an interface, but beware that the ifup/ifdown persistent status information will be out of sync with reality. ifup will continue thinking the interface is down even after you bring it up with ip.

Related Question