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:
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
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"
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 220.127.116.11. 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:
Or, you can check a specific network card’s address as shown below.
ip a s enp0s3
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
Alternatively, you can use the nmtui command.
nmtui edit enp0s3
Enter the IP address, netmask, gateway, and DNS etc.
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
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:
ip a s enp0s3
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
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.
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>
Here em0 is the network interface card name.
To configure a static IP address, edit /etc/rc.conf file:
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"
Save and close the file.
Restart network service using the followng command:
/etc/rc.d/netif restart && /etc/rc.d/routing restart
To configure network card to obtain IP address from a DHCP server, add or modify the following lines only:
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.
Thanks for stopping by!
How can I benefit from this blog:
- Subscribe to our Email Newsletter : Sign Up Now
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!