Configuring IP address is one of the mandatory skill that every Linux and Unix administrator should learn. We can easily configure IP address in GUI mode. But it is entirely different in Command line mode. This step by step tutorial describes how to configure static IP address in Linux and Unix operating systems. The steps provided below are tested on CentOS 7 server, Ubuntu 16.04 server, Ubuntu 18.04 LTS server and desktop editions and FreeBSD 12. However, it should work on most RPM-based and DEB-based Linux systems and Unix flavors.
Configure Static IP address in Linux
First, we will see how to configure IP address on RPM-based systems.
Configure Static IP address 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.
Note: Here, I run all commands as root user. If you logged-in as normal user, just use 'sudo' in-front of each command.
First, let us find the name of the network card. To do so, run:
# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
Or, use this command to display detailed output:
# ip addr
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.225.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. It might be different in your distribution, but it usually start with letter "e".
Let us now configure a static IP address to this NIC.
Open the network card config file in any editor:
# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
Here, vi is the text editor. You can use any text/graphical editor of your choice, for example nano or gedit.
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.225.150" PREFIX0="24" GATEWAY0="192.168.225.1" DNS1="126.96.36.199" 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 (and arrows in the image)? Those are the important lines.
Let me explain about those lines:
- 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.225.150" - This line indicates the IP address of the network card. Here, you might have noticed 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 add new lines - for example IPADDR1, IPADDR2 and set different IP addresses of your choice.
- PREFIX0="24" - This line indicates the subnet mask, i.e 255.255.255.0. Here you can specify more than one subnet with lines PREFIX1, PREFIX 2 etc.
- GATEWAY0="192.168.225.1" - This is the gateway address of the NIC.
- DNS1="188.8.131.52" - The Name server address.
Once you setup all details, save and close the file. Restart the network service for the changes to take effect.
# 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
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff inet 192.168.225.150/24 brd 192.168.225.255 scope global noprefixroute enp0s3 valid_lft forever preferred_lft forever inet6 2405:204:714e:faa:a00:27ff:fe80:6319/64 scope global noprefixroute valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe80:6319/64 scope link noprefixroute valid_lft forever preferred_lft forever
Alternatively, you can use the NetworkManager TUI (nmtui) utility to configure IP address.
If it is not installed already, you can install it using command:
# yum install NetworkManager-tui
Now, start nmtui utility by entering the following command:
Choose "Edit a connection" option:
Choose the network card to configure from the left pane and select "Edit" option on the right and hit ENTER key:
Enter the IP address, netmask, gateway, and DNS details etc. Finally, Click OK to save the changes.
Restart network service or reboot your system to take effect the changes.
Configure Static IP address on Debian / Ubuntu:
All NIC configuration files are stored under /etc/network/ directory in DEB based systems.
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 [email protected]
Or, simply reboot the system.
Now, check the new static IP address using any one of the following commands:
$ ip addr
$ 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
Starting from Ubuntu 17.10, we no longer use /etc/network/interfaces file to configure IP address. In recent Ubuntu versions, we use Netplan utility to configure IP address. To configure IP address on recent Ubuntu distributions, refer the following link.
Configure Static IP address in Unix
We just learned how to configure static IP address in Linux from Command line. Let us now configure static IP address in Unix. For the purpose of this tutorial, I will be using FreeBSD 12.
Just like Linux, we use "ifconfig" command to find out the network card name. Here, I logged-in as root user to perform the following commands.
em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500 options=81009b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,VLAN_HWFILTER> ether 08:00:27:7e:6d:d4 inet6 fe80::a00:27ff:fe7e:6dd4%em0 prefixlen 64 scopeid 0x1 inet6 2405:204:714e:faa:a00:27ff:fe7e:6dd4 prefixlen 64 autoconf inet 192.168.225.27 netmask 0xffffff00 broadcast 192.168.225.255 media: Ethernet autoselect (1000baseT <full-duplex>) status: active nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL> lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384 options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6> inet6 ::1 prefixlen 128 inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 inet 127.0.0.1 netmask 0xff000000 groups: lo nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>
Here em0 is the network interface card name.
To configure static IP address, edit /etc/rc.conf file:
# vi /etc/rc.conf
Add/modify the lines as shown below.
hostname="freebsd.ostechnix.local" ifconfig_em0="inet 192.168.255.110 netmask 255.255.255.0" defaultrouter="192.168.255.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 following command:
# /etc/rc.d/netif restart && /etc/rc.d/routing restart
Now, check if the IP address has been changed or not using command:
To configure network card to obtain IP address from a DHCP server, add or modify the following lines only:
hostname="freebsd.ostechnix.local" ifconfig_DEFAULT="DHCP inet6 accept_rtadv"
Save and close the file. Restart networking service or reboot your system to take effect the changes.