How to set static or dynamic ip address on linux?
11,031
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 videos on Youtube
Author by
Gappa
Updated on September 18, 2022Comments
-
Gappa almost 2 years
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?
-
Gappa about 11 yearsit doesn't work. It is the same thing I already tried
-
NickW about 11 yearsThey should be on separate stanzas, but when you say they don't work, what doesn't work exactly?
-
Gappa about 11 yearsI'm testing if I could reach a computer, with a static ip address, from another computer, just connecting each other with a LAN without passing through a router. The 2 computers are in the same subnet.
-
Gappa about 11 yearsand if apply that configuration I cannot reach the computer
-
NickW about 11 yearsFrom inside, what does the machine say? Is the network DHCP or static, and what does
ip route show
say? -
NickW about 11 yearsI'm thinking it may just be a routing issue, though I'd like to see what the logs on your machine say..
-
Gappa about 11 yearsip route show: default via 192.168.1.10 dev eth0 and 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.150
-
NickW about 11 years
-
NickW about 11 yearsBTW, if the other machine (the one you can't ping) is a windows 7 machine, it is normal that it doesn't respond to pings!
-
Gappa about 11 yearsnooooooo come on!!!!!!!!!!! are you kidding me?!?!??!?!!?!?!? I cannot explain to you my mood right now!!!!!!!!!! I'km shocked!!! I'm more a unix guy!!!!
-
Gappa about 11 yearsI'm very to have wested your time yesterday! anyway the machine is a windows 8!!!!
-
NickW about 11 yearsNot a problem at all, these sort of little things are absolutely normal :)