How to configure Static IP address in Linux and Unix

If you are a System administrator, You should/must know how to configure static IP address in Linux and Unix platforms. This is one of the core skill of every Linux and Unix administrator. Configuring IP address in GUI mode is much easier, so we will not cover that in this guide. But what about in Command line mode? It is not that complicated too. This brief tutorial describes how to configure a static IP in your Linux and Unix systems. For the purpose of this tutorial, I will be showing you how to do this in Linux distributions like CentOS, Debian, Ubuntu and Unix flavors like FreeBSD. Follow me.

Configure Static IP address in Linux

On  RHEL / CentOS / Fedora / Scientific Linux:

In Fedora, RHEL and its clones like CentOS, Scientific Linux, the network interface card (shortly NIC) configuration will be stored under /etc/sysconfig/network-scripts/ directory.

First, we must the name of the network card. To do so, run:

ip addr

Sample output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
 valid_lft forever preferred_lft forever
 inet6 fe80::a00:27ff:fe80:6319/64 scope link 
 valid_lft forever preferred_lft forever

root@server1:~_001

Usually, the wired network card name will start with letter “e”, and wireless card name will start letter with “w”.

As you see in the above output, my wired network card name is enp0s3.

Let us setup a static IP address to this NIC.

Open the network card config file in any editor:

sudo nano /etc/sysconfig/network-scripts/ifcfg-enp0s3

Add the IP address, subnet mask, gateway, and DNS server as shown below.

TYPE="Ethernet"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
NAME="enp0s3"
UUID="e9f9caef-cb9e-4a19-aace-767c6ee6f849"
ONBOOT="yes"
HWADDR="08:00:27:80:63:19"
IPADDR0="192.168.1.150"
PREFIX0="24"
GATEWAY0="192.168.1.1"
DNS1="192.168.1.1"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"

root@server1:~_002

This is how a typical network card configuration file looks like in any RPM based systems. Did you notice the lines that I have marked in bold? Those are the important lines.

Let us see what are those files one by one.

  • BOOTPROTO=”none”– This line shows that the network card’s IP address should be configured manually. If you set the value as “dhcp”, then the network card will accept the IP address from any DHCP server in the network.
  • IPADDR0=”192.168.1.150″ – This line indicates the IP address of the network card. Here, you might notice the number 0(zero) after the line IPADDR. This indicates that this card has only one IP address. If you want to set more than one IP address (i.e virtual IP address, then you need to change this line as IPADDR1, IPADDR2 respectively.
  • PREFIX0=”24″ – This line indicates the subnet mask, i.e 25.255.255.0. Here you can specify more than PREFIX with lines PREFIX1, PREFIX 2 etc.
  • GATEWAY0=”192.168.1.1″ – This is the gateway address of the NIC.
  • DNS1=”192.168.1.1″ – The Name server address.

Once you setup all details, save and close the file. Restart the network service for the changes to take effect.

sudo systemctl restart network

Or, simply reboot your system.

Now, verify the new static IP address using command:

ip addr

Or, you can check a specific network card’s address as shown below.

ip a s enp0s3

Sample output:

 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
 valid_lft forever preferred_lft forever
 inet6 fe80::a00:27ff:fe80:6319/64 scope link 
 valid_lft forever preferred_lft forever

root@server1:~_003

Alternatively, you can use the nmtui command.

nmtui edit enp0s3

Enter the IP address, netmask, gateway, and DNS etc.

root@server1:~_005

Click OK to save the changes, and restart network service or reboot your system to take effect the changes.

Want to know how to configure multiple IP addresses for a single network interface card? Well, check the following link.

On Ubuntu / Debian / Linux Mint:

The NIC configuration file will be stored under /etc/network/ directory in DEB based systems such as Ubuntu, Linux Mint, and Elementary OS etc.

To configure a static IP address in any DEB based systems, do the following.

Edit /etc/network/interfaces/ file in any editor:

sudo nano /etc/network/interfaces

Add or modify the following lines to configure static IP address.

auto enp0s3
iface enp0s3 inet static
 address 192.168.1.105
 netmask 255.255.255.0
 gateway 192.168.1.1
 dns-nameservers 192.168.1.1

sk@ubuntuserver: ~_006

Save and close the file.

Restart network service using command:

sudo systemctl restart ifup@enp0s3

Or, simply reboot the system.

Now, check the new static IP address using any one of the following commands:

ifconfig
ip addr
ip a s enp0s3

Sample output:

2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
 link/ether 08:00:27:12:f8:c1 brd ff:ff:ff:ff:ff:ff
 inet 192.168.1.105/24 brd 192.168.1.255 scope global enp0s3
 valid_lft forever preferred_lft forever
 inet6 fe80::a00:27ff:fe12:f8c1/64 scope link 
 valid_lft forever preferred_lft forever

sk@ubuntuserver: ~_007

Configure Static IP address in Unix

We already know how to confiugre a static IP address in Linux from Command line. Now, We will configure static IP address in Unix. For the purpose of this tutorial, we will be using FreeBSD 10.3.

Just like Linux, we use “ifconfig” command to find out the network card name.

ifconfig

Sample output:

em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
 options=9b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM>
 ether 08:00:27:f8:ba:d8
 inet 192.168.1.103 netmask 0xffffff00 broadcast 192.168.1.255 
 nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
 media: Ethernet autoselect (1000baseT <full-duplex>)
 status: active
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
 options=600003<RXCSUM,TXCSUM,RXCSUM_IPV6,TXCSUM_IPV6>
 inet6 ::1 prefixlen 128 
 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 
 inet 127.0.0.1 netmask 0xff000000 
 nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>

sk@sk:~_008

Here em0 is the network interface card name.

To configure a static IP address, edit /etc/rc.conf file:

nano /etc/rc.conf

Add/modify the lines as shown below.

hostname="freebsd.ostechnix.local"
ifconfig_em0="inet 192.168.1.103 netmask 255.255.255.0"
defaultrouter="192.168.1.1"
local_unbound_enable="YES"
sshd_enable="YES"
moused_enable="YES"
ntpd_enable="YES"
powerd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"

sk@sk:~_009

Save and close the file.

Restart network service using the followng command:

/etc/rc.d/netif restart && /etc/rc.d/routing restart

sk@sk:~_010

To configure network card to obtain IP address from a DHCP server, add or modify the following lines only:

hostname="freebsd.ostechnix.local"
ifconfig_em0="DHCP"

Save and close the file. Restart networking service or reboot your system to take effect the changes.

That’s all for today folks. We know now how to configure a static IP in Unix and Linux distributions. As you can see in the above examples, configuring IP address from command line is pretty easy. I always prefer to use command line mode for doing any sort of administrative tasks. Hope this helps.

I will be here soon with another useful guide. Until then, stay tuned with OSTechNix.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

  • Dan St.André

    Thank you for this clearly written description of static IP configuration. I would be interested in a follow-up article that discusses static IP for a walk-about laptop. Specifically, I need STATIC_IP_A when I’m part of NETWORK_1, STATIC_IP_B when I’m part of NETWORK_2,…, STATIC_IP_Z when I’m part of NETWORK_N, and dynamic IP address the rest of the time.

    • SK

      I am afraid we couldn’t do that in Linux with single network interface card. However, It is possible to do it with manageable network switch. We could create different vlans and assign different set of IP addresses for each vlan.

  • Dan St.André

    Is there some way to:
    1. detect which network is available
    2. run one or more scripts that are per-network specific
    3. the scripts would used DHCP or static-IP as needed
    4. might need to connect-inspect-bounce-configure

    While I would like to detect wired & wireless and launch appropriate scripts, wireless is by far the most common and most useful.

    I am trying, with poor success, to find the network connection hooks that I could use to launch my own scripts.

    ~~~0;-Dan

    ASIDE — I use Linux Mint 17.3 from the Ubuntu family of distributions.

  • SK

    As I understood..
    Create a script..First detect which network available on system. And show available network status like up or down and also show Static ip or DHCP IP.
    Something like-
    Available—Status—DH/ ST——–IP
    eth0. Up. dhcp. *******
    wlan0. Down. —- ——-
    Second option detect network configuration – Enter Available network name- when we enter name , then again show option set ip address dhcp or static-
    -> Input dhcp (auto set dhcp ip)
    -> Input static (Then auto set static ip) or
    Enter -> Ipaddress-
    -> netmask-
    -> gateway-
    -> dns1
    -> dns2

    I think when we are connected both network (wlan & lan) ..the first priority of wireless network if wireless network down then automatic up lan port…