How to set a temporary IP Address in Linux?

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

What are the valid_lft and preferred_lft?

valid_lft = Valid lifetime

ip addr add 192.168.4.244/23 dev eth0 valid_lft 10 preferred_lft 10
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
root@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.244
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
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 forever
root@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

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.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store