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
Share:
11,031

Related videos on Youtube

Gappa
Author by

Gappa

Updated on September 18, 2022

Comments

  • Gappa
    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
    Gappa about 11 years
    it doesn't work. It is the same thing I already tried
  • NickW
    NickW about 11 years
    They should be on separate stanzas, but when you say they don't work, what doesn't work exactly?
  • Gappa
    Gappa about 11 years
    I'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
    Gappa about 11 years
    and if apply that configuration I cannot reach the computer
  • NickW
    NickW about 11 years
    From inside, what does the machine say? Is the network DHCP or static, and what does ip route show say?
  • NickW
    NickW about 11 years
    I'm thinking it may just be a routing issue, though I'd like to see what the logs on your machine say..
  • Gappa
    Gappa about 11 years
    ip 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
    NickW about 11 years
  • NickW
    NickW about 11 years
    BTW, 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
    Gappa about 11 years
    nooooooo 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
    Gappa about 11 years
    I'm very to have wested your time yesterday! anyway the machine is a windows 8!!!!
  • NickW
    NickW about 11 years
    Not a problem at all, these sort of little things are absolutely normal :)