Delete IP address alias by label name

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.


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 \K\S+') 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.

Tags:

Linux

Iproute