Linux – How to set static or dynamic ip address on linux

internet connectionip addresslinux

I don't know a priori if a DHCP server would be present in the environment. So, I would like to set a dynamic IP address if there is a DHCP server, instead, I would like to set a static IP address if there isn't any DHCP server. Automatically.

I have just one network interface, eth0.

I tried to edit the /etc/network/interfaces like that:

auto eth0 eth0:1

iface eth0 inet dhcp
iface eth0:1 inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.10

but it did not work.

I also tried this way

auto eth0

iface eth0 inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.10
iface eth0 inet dhcp

but I had no success.
I also tried to use the default iface:

iface default inet static
address 192.168.1.150
netmask 255.255.255.0
gateway 192.168.1.10

Can you explain to me what is the default iface? when do you have to use it?

any idea about how to have a static or dynamic IP address with just one network interface?

Best Answer

Try this:

auto eth0  
iface eth0 inet dhcp

auto eth0:1  
iface eth0:1 inet static  
address 192.168.1.150  
netmask 255.255.255.0  
gateway 192.168.1.10
Related Question