Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using this ifconfig command to set IP on a network interface

ifconfig eth0 x.x.x.x netmask x.x.x.x mtu 1400

Now they say that ifconfig is deprecated and I should use iproute2. I converted that command into two commands:

ip addr add x.x.x.x/24 dev eth0
ip link set mtu 1400 dev eth0

How can I join both into just one command?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Short answer: you can't, unless you wrap it into a shell function. A poor man's version:

function ifaddrmtu() {
    # Call: ifaddrmtu eth0:foobar 1.1.1.1 255.255.255.254
    #   or: ifaddrmtu eth0:foobar 1.1.1.1 255.255.255.254 1400
    local label=$1
    local ip=$2
    local mask=$3
    local mtu=$4
    sudo ip addr add $ip/$mask brd + dev ${label%:*} label $label
    [ ! -z $mtu ] && sudo ip link set mtu $mtu dev $label
}

Now, if you really want to map the old ifconfig behaviour to iproute2, you have to write a parser, which maps ifconfig syntax and semantics into iproute2. A little over ten years ago, I wrote such a biest in shell for a company I worked for. If you need it I can try to dig it out from one of my old hard drives. It was pretty neat and also came with more user friendly ways of showing the routing tables and their entries.

A more detailed answer: Using the iproute2 framework adding an IP address and setting the mtu of a device are two rather different things from the kernel's point of view. While ifconfig uses the old-style ioctl communication to alter the configuration in the kernel's network stack (the kernel subsequently does all the work internally calling different functions to add an IP address and setting the mtu), iproute2 uses the netlink messaging framework to communicate with the network stack.

You can best see it using strace (excerpt from my Linux VM):

# ip addr del 1.1.1.1/32 dev eth4

# strace -e ioctl ifconfig eth4:new 1.1.1.1 netmask 255.255.255.255 mtu 1400
ioctl(4, SIOCSIFADDR, {ifr_name="eth4:new", ifr_addr={AF_INET, inet_addr("1.1.1.1")}}) = 0
ioctl(4, SIOCGIFFLAGS, {ifr_name="eth4:new", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
ioctl(4, SIOCSIFFLAGS, {ifr_name="eth4:new", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
ioctl(4, SIOCSIFNETMASK, {ifr_name="eth4:new", ifr_netmask={AF_INET, inet_addr("255.255.255.255")}}) = 0
ioctl(4, SIOCSIFMTU, {ifr_name="eth4:new", ifr_mtu=1400}) = 0

# ip -4 addr show dev eth4
2: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UP qlen 1000
    inet 10.211.55.4/24 brd 10.211.55.255 scope global eth4
    inet 1.1.1.1/32 scope global eth4:new

# ip addr del 1.1.1.1/32 dev eth4                                          

# strace -e "send,sendmsg,recvmsg" ip addr add 1.1.1.1/32 dev eth4 label eth4:new
send(3, "\24\0\0\0\22\0\1\3\206\35\31R\0\0\0\0\0\0\0\0", 20, 0) = 20
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"\340\3\0\0\20\0\2\0\206\35\31R\257\10\0\0\0\0\4\3\1\0\0\0I\0\1\0\0\0\0\0"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 2992
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"\24\0\0\0\3\0\2\0\206\35\31R\257\10\0\0\0\0\0\0\1\0\0\0I\0\1\0\0\0\0\0"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 20
sendmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"8\0\0\0\24\0\5\6\207\35\31R\0\0\0\0\2 \0\0\2\0\0\0\10\0\2\0\1\1\1\1"..., 56}], msg_controllen=0, msg_flags=0}, 0) = 56
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"$\0\0\0\2\0\0\0\207\35\31R\257\10\0\0\0\0\0\0008\0\0\0\24\0\5\6\207\35\31R"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 36                       = ?

# strace -e "send,sendmsg,recvmsg" ip link set mtu 1420 dev eth4
send(3, " \0\0\0\20\0\5\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 32, 0) = 32
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"4\0\0\0\2\0\0\0\0\0\0\0\250\10\0\0\355\377\377\377 \0\0\0\20\0\5\0\0\0\0\0"..., 8192}], msg_controllen=0, msg_flags=0}, 0) = 52
send(3, "\24\0\0\0\22\0\1\3;\35\31R\0\0\0\0\0\0\0\0", 20, 0) = 20
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"\340\3\0\0\20\0\2\0;\35\31R\250\10\0\0\0\0\4\3\1\0\0\0I\0\1\0\0\0\0\0"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 2992
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"\24\0\0\0\3\0\2\0;\35\31R\250\10\0\0\0\0\0\0\1\0\0\0I\0\1\0\0\0\0\0"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 20
sendmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"(\0\0\0\20\0\5\0<\35\31R\0\0\0\0\0\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0"..., 40}], msg_controllen=0, msg_flags=0}, 0) = 40
recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, pid=0, groups=00000000}, msg_iov(1)=[{"$\0\0\0\2\0\0\0<\35\31R\250\10\0\0\0\0\0\0(\0\0\0\20\0\5\0<\35\31R"..., 16384}], msg_controllen=0, msg_flags=0}, 0) = 36

# ip -4 addr show dev eth4
2: eth4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1420 qdisc pfifo_fast state UP qlen 1000
    inet 10.211.55.4/24 brd 10.211.55.255 scope global eth4
    inet 1.1.1.1/32 scope global eth4:new

There are of course more reasons why an IP address and the setting of the mtu for a device entry are two different things. Let's just say that IP addresses do not really belong to interfaces as suggested by ifconfig. The name is merely a label used as an interface name which the forward information base (FIB) uses to lookup/filter and route packets to. The kernel from a packet handling point of view could very well live without the device name.

share|improve this answer
    
Thank you for a detailed answer. I ended up using the two commands. –  kuchi Aug 25 '13 at 5:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.