Delete IP address alias by label name

I would like to delete an alias I created using:

ip addr add 192.168.1.1 dev eth0 label eth0:100

without having to know the IP address. Basically, I would like to do

ip addr del dev eth0 label eth0.100

which, according to documentation should be valid, but rather gives me:

ip: RTNETLINK answers: Operation not supported

In the mean time, I worked around using

ip addr del $(ip addr list label eth0:100 | awk '{ print $2 }') dev eth0 label eth0.100
Asked By: bcolpron

||

You can delete net alias using ifconfig command:

/sbin/ifconfig eth0:100 down

According to manual for ip command, only device name is required for IP address deletion. The rest arguments are optional. If no arguments given the first IP address will be deleted from the interface. But really I couldn’t reproduce this behaviour.
Looks like the IP address is required too.

The last command you’ve shown works just because the expression ip addr list label eth0:100 | awk '{ print $2 }' gives the IP address of the net alias and insert this IP to ip addr delete ... command.

Answered By: svq

What you have is the best route (though I would use grep over awk, but that’s personal preference). The reason being is because you can have multiple addresses per ‘label’. Thus you have to specify which address you want to delete.

# ip addr help
Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]
                                                      [ CONFFLAG-LIST ]
       ip addr del IFADDR dev STRING
       ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]
                            [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]
       ip addr {showdump|restore}
IFADDR := PREFIX | ADDR peer PREFIX
          [ broadcast ADDR ] [ anycast ADDR ]
          [ label STRING ] [ scope SCOPE-ID ]

Note the ip addr del syntax which says the parameters are IFADDR and STRING. IFADDR is defined below that, and says PREFIX is a required parameter (things in [] are optional). PREFIX is your IP/subnet combination. Thus it is not optional.


As for what I meant about using grep, is this:

ip addr del $(ip addr show label eth0:100 | grep -oP 'inet KS+') dev eth0 label eth0:100

The reason for this is in case the position of the parameter changes. The fields positions in the ip addr output can change based on optional fields. I don’t think the inet field changes, but it’s just my preference.

Answered By: phemmer

ip command has -o flag which produces output suitable for parsing. For instance:

# ip -o addr show label wlp2s0:1|awk '{print $4}'
172.16.127.5/24

It is sufficient do wrap this as command-substitution $(...) and we have the following:

# ip addr del $(ip -o addr show label wlp2s0:1|awk '{print $4}') dev wlp2s0

Variation on the same theme but with xargs:

# ip -o a show label wlp2s0:1|awk '{print $4}'|xargs -I% ip a del % dev wlp2s0

Note that you can replace addr with a for shorter command

Side note:

Please note that for ip command this is the required syntax as per 2011 man-page ip-address(8)

ip address del IFADDR dev IFNAME

Hence the address with prefix has to follow del and dev IFNAME is required.

Answered By: Sergiy Kolodyazhnyy
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.