How to configure a static ip in Linux

This is a newbie question I get quite often.

Configuring your Linux machine to run on a static IP is easy. Tools like system-config-network and netconfig provide you simple GUIs to do this.

For today, I’ll show you how to do this from the command line instead.

Navigate to /etc/sysconfig/network-scripts/

[root@baboo]# cd /etc/sysconfig/network-scripts/

Every network interface will have it’s own interface script file. eth0,eth1,eth2 and so on. Vi the ifcfg-eth0 interface script file for interface eth0. Replace the contents of the ifcfg-eth0 file with the parameters below.

[root@baboo]# vi ifcfg-eth0.

DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.0.100
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
HWADDR=00:0F:22:71:0A:53
USERCTL=no
USERCTL=no

If you want to switch back to DHCP, repeat the steps above and replace the contents of the ifcfg-eth0 file with the parameters below.

DEVICE=eth0
BOOTPROTO=dhcp
HWADDR=00:0F:20:71:0A:50
ONBOOT=yes
TYPE=Ethernet
DHCP_HOSTNAME=klmdrpdr01p.klm1.netcel360.com

Restart your interface to apply the changes.

[root@baboo]#ifdown eth0
[root@baboo]#ifup eth0

To update your dns server settings, modify the /etc/resolv.conf.

[root@baboo]# vi /etc/resolv.conf

Replace the contents of the resolv.conf file with the parameters below. The first parameter “search” is your search path followed the nameserver parameters which hold the IPs for your primary and secondary DNS servers.

search example.com
nameserver 192.168.0.5
nameserver 192.168.0.6

That’s it. Drop me a note if you need any help.

  • Good for newbie. I would recommend “netconfig” for newbie, πŸ™‚

  • I agree with Wing Loon. netconfig is a lot easier when using command line with the ncurses UI.

  • Ivan

    Hello.
    I have a problem setting up static ip, i conecti it to my router and it works on
    IP 192.168.1.12
    Gateway 192.168.1.1
    Subnet 255.255.255.0

    when i reconectit it back to swich and try to set up it to work on static
    IP 206.139.XXX.XXX
    Gateway 206.139.XXX.XXX
    Subnet 255.255.255.240

    it does not work πŸ™

    the router works conected to the same switch and it works on the same settings

    I try to disconect the router and set the IP address thet i use for router, and i conected the comp to the same port on switch, so i will not get problem with the dead port.

    the file /etc/sysconfig/network-scripts/ifcfg-eth0:

    DEVICE=eth0
    BOOTPROTO=none
    HWADDR=00:0b:db:e7:1d:81
    ONBOOT=yes
    NETMASK=255.255.255.240
    IPADDR=206.139.XXX.XXX
    GATEWAY=206.139.XXX.XXX
    TYPE=Ethernet

    so can some one tall me whot i do wrong??
    Please.
    Thank you.

© 2008-2012 The Danesh Project
Powered by Wordpress and made by Guerrilla. Hosted at ServerFreak