Setting a temporary ip address and gateway on linux server

Solution 1:

ifconfig is deprecated, but it still works.

The "correct" way would be:

sudo ip addr add <extra ip> dev eth0:0

Solution 2:

Think I found it, but it might be worth sharing anyway:

ifconfig eth0:0 <extra ip> up
sudo route add default gw <gatewayip>

Solution 3:

I just want to add something on blauwblaatje's answer. https://serverfault.com/a/49651/575693

sudo ip addr add <ip/cidr> dev eth0

If you don't put cidr notation, the host will be configured as host only or /32. Hence, you won't be able to communicate with other machine.

Avoid the following example unless you don't need networking. E.g.

sudo ip addr add 172.16.1.1 dev eth0

same as

sudo ip addr add 172.16.1.1/32 dev eth0

Instead, define your subnet based on your requirement, let say Class C or /24

sudo ip addr add 172.16.1.1/24 dev eth0