I am posting this answer to give another dimension to the question and existing answers, not as a direct answer to the original question. Your IP will be lost at reboot. However, I think it's worth you understanding these commands, iproute2
package is the currently method of on-the-fly interface management via the CLI. Lots of people are still writing scripts using ifconfig
for example.
I think it's worth noting the use of the iproute2
packages;
Show current IPs on all interface (ip addr
)
n2:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:e0:4c:6d:0a:49 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.221/24 brd 172.22.0.255 scope global wlan0
inet6 fe80::76ea:3aff:fead:2e00/64 scope link
valid_lft forever preferred_lft forever
Add an IP address to my wireless interface (ip addr add
)
n2:~$ sudo ip a a 10.0.0.56/24 dev wlan0
[sudo] password for bensley:
Show my IP addresses again, note the additional IP on wlan0 (ip addr
)
n2:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:e0:4c:6d:0a:49 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.221/24 brd 172.22.0.255 scope global wlan0
inet 10.0.0.56/24 scope global wlan0
inet6 fe80::76ea:3aff:fead:2e00/64 scope link
valid_lft forever preferred_lft forever
Show the link state of my interfaces (ip link
)
n2:~$ ip l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:e0:4c:6d:0a:49 brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
Delete this 2nd IP address from my wireless interface (ip addr delete
)
n2:~$ sudo ip a del 10.0.0.56/24 dev wlan0
Show IP addresses just for wlan0 to check (ip addr show wlan0
)
n2:~$ ip a s wlan0
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 74:ea:3a:ad:2e:00 brd ff:ff:ff:ff:ff:ff
inet 172.22.0.221/24 brd 172.22.0.255 scope global wlan0
inet6 fe80::76ea:3aff:fead:2e00/64 scope link
valid_lft forever preferred_lft forever
The same commands could have been used to remove my current IP, 172.22.0.221 then add another, but I would have lost connectivity, which I can't right now. Below are those commands though, after which I would also need to add a new default route route;
n2:~$ ip r s
default via 172.22.0.1 dev wlan0 proto static
169.254.0.0/16 dev wlan0 scope link metric 1000
172.22.0.0/24 dev wlan0 proto kernel scope link src 172.22.0.221 metric 2
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
ip a del 172.22.0.221/24 dev wlan0
ip a a 172.22.0.10/24 dev wlan
ip r a default via 172.22.0.1
# Re-add the same default route ^ via the same default gateway
sudo service networking restart