How to set a temporary IP Address in Linux?

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:

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. 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 IP address. I added a PHP file that shows the IP address of the visitor. The script works on

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.

After 10 seconds, the marked as deprecated. It works properly for IPv6 as well.



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