- Linux Administration Cookbook
- Adam K. Dean
- 179字
- 2021-07-02 14:24:29
Adding and removing an IP against an interface
This can be accomplished with the ip address command, but does require root.
In this example, I chose another IP in the 192.168.33.0/24 subnet, which I know isn't in use:
In the real world, it's good practice to ping an IP first, even if you believe it's free to use.
$ sudo ip address add 192.168.33.22/24 dev eth1
Checking our eth1 interface, we can now see the secondary IP:
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:0d:d9:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.33.10/24 brd 192.168.33.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet 192.168.33.22/24 scope global secondary eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe0d:d90c/64 scope link
valid_lft forever preferred_lft forever
Upon a reboot, this change will be lost. We will talk about adding IP information permanently in the section titled 'Adding and configuring network interfaces'.
If we want to remove an IP, we again use ip, this time replacing the add instruction with a del instruction:
$ sudo ip address del 192.168.33.22/24 dev eth1