Say, you have to use an IP address temporarily. You may have already have a script, which sets the IP address. You most probably execute the script via cron.
There is already a simple solution that works with the ip command Linux.
Most probably you used the ip command. If you didn’t use it, then you should use it! You know the ifconfig command is a deprecated command.
For example output of ip addr show eth0 is:
root@adil:~# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:aa:52:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.243/23 brd 192.168.5.255 scope global eth0
valid_lft forever preferred_lft forever
I believe that lots of Linux users are familiar with this output. I have marked valid_lft and preferred_lft in bold.
What are the valid_lft and preferred_lft?
valid_lft = Valid lifetime
preferred_lft = Preferred lifetime
By default, valid_lft and preferred_lft have forever (a.k.a. zero) value.
valid_lft indicates the lifetime of the IP address.
preferred_lft indicates the lifetime of the IP address, which can be used as a source IP address (IPv6 only).
Let’s say you want to use an IP address for 10 seconds.
ip addr add 192.168.4.244/23 dev eth0 valid_lft 10 preferred_lft 10
192.168.4.244 will be used for 10 seconds. The IP address will be removed from the server after 10 seconds.
Let’s test it!
In my local environment, I have a computer with 192.168.4.71 IP address. I added a PHP file that shows the IP address of the visitor. The script works on http://192.168.4.71/ip.php
root@adil:~# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:aa:52:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.243/23 brd 192.168.5.255 scope global eth0
valid_lft forever preferred_lft foreverroot@adil:~# ip addr change 192.168.4.244/23 dev eth0 valid_lft 10 preferred_lft 10root@ubuntu:~# curl --interface 192.168.4.244 192.168.4.71/ip.php
Your IP Address is: 192.168.4.244root@adil:~# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:aa:52:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.243/23 brd 192.168.5.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.4.244/23 scope global secondary dynamic eth0
valid_lft 10sec preferred_lft 10sec
inet6 fe80::a00:27ff:feaa:5287/64 scope link
valid_lft forever preferred_lft foreverroot@adil:~# sleep 10root@adil:~# curl --interface 192.168.4.244 192.168.4.71/ip.php
curl: (45) bind failed with errno 99: Cannot assign requested address
The IP has gone. It can not be used anymore.
How about preferred_lft?
The preferred_lft is a good option. First, it can’t be greater than valid_lft. Because when the preferred_lft counter reaches zero, then the IP address can’t be used as a source IP address anymore. However, when it reaches zero, it still can accept traffic from outside.
Say, valid_lft has 100 seconds, preferred_lft has 10 seconds.
root@adil:~# ip addr change 192.168.4.244/23 dev eth0 valid_lft 100 preferred_lft 10root@adil:~# sleep 10root@adil:~# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:aa:52:87 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.243/23 brd 192.168.5.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.4.244/23 scope global secondary deprecated dynamic eth0
valid_lft 90sec preferred_lft 0sec
After 10 seconds, the 192.168.4.244 marked as deprecated. It works properly for IPv6 as well.