If you are a Linux system administrator, time will come when you will need to configure networking on your system. Unlike desktop machines where you can use dynamic IP addresses, on a server infrastructure, you will need to setup a static IP address (at least in most cases).
Read Also: How to Set or Change System Hostname in Linux</p
This article is meant to show you how to configure static IP address on most frequently used Linux distributions.
For the purpose of this tutorial, we will use the following Internet Protocol version 4 (IPv4) details:
IP address: 192.168.0.100 Netmask: 255.255.255.0 Hostname: node01.tecmint.com Domain name: tecmint.com Gateway: 192.168.0.1 DNS Server 1: 8.8.8.8 DNS Server 2: 4.4.4.4
Configure Static IP Address in RHEL/CentOS/Fedora:
To configure static IP address in RHEL / CentOS / Fedora, you will need to edit:
/etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0
Where in the above "ifcfg-eth0"
answers to your network interface eth0
. If your interface is named “eth1"
then the file that you will need to edit is "ifcfg-eth1"
.
Let’s start with the first file:
# vi /etc/sysconfig/network
Open that file and set:
NETWORKING=yes HOSTNAME=node01.tecmint.com GATEWAY=192.168.0.1 NETWORKING_IPV6=no IPV6INIT=no
Next open:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
Note: Make sure to open the file corresponding to your network interface. You can find your network interface name with ifconfig -a command.
In that file make the following changes:
DEVICE="eth0" BOOTPROTO="static" DNS1="8.8.8.8" DNS2="4.4.4.4" GATEWAY="192.168.0.1" HOSTNAME="node01.tecmint.com" HWADDR="00:19:99:A4:46:AB" IPADDR="192.68.0.100" NETMASK="255.255.255.0" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="8105c095-799b-4f5a-a445-c6d7c3681f07"
You will only need to edit the settings for:
- DNS1 and DNS2
- GATEWAY
- HOSTNAME
- NETMASK
- IPADDR
Other settings should have already been predefined.
Next edit resolve.conf
file by opening it with a text editor such as nano or vi:
# vi /etc/resolv.conf
nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip
Once you have made your changes restart the networking with:
# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]
Set Static IP Address in Debian / Ubuntu
To setup static IP address in Debian/ Ubuntu, open the following file:
# nano /etc/network/interfaces
You may see a line looking like this:
auto eth0 iface eth0 inet dhcp
Change it so it looks like this:
auto eth0 iface eth0 inet static address 192.168.0.100 netmask 255.255.255.0 gateway 192.168.0.1 dns-nameservers 4.4.4.4 dns-nameservers 8.8.8.8
Save the file and then edit /etc/resolv.conf
like this:
# nano /etc/resolv.conf
nameserver 8.8.8.8 # Replace with your nameserver ip nameserver 4.4.4.4 # Replace with your nameserver ip
Restart the networking on your system with:
# /etc/init.d/network restart [On SysVinit] # systemctl restart network [On SystemD]
Your static IP address has been configured.
Conclusion:
You now know how to configure a static IP address on a Linux distro. If you have any questions or comments, please do not hesitate to submit them in the comment section below.
There isn’t an “eth0” on my server because it is connected by WIFI only.
@Thuemaychuaonet,
If your server is connected only via WiFi, the network interface will likely have a different name, such as `wlan0` or something similar, depending on your system and its configuration.
The steps to set a static IP address and configure the network are similar, but you’ll need to use the correct interface name for your WiFi connection.
You can find the name of your WiFi interface by running the `
ip a`
command in the terminal. Once you have the correct interface name, you can follow the same process to configure your network settings.The time will come when you will need to configure networking on your system. Unlike desktop machines where you can use dynamic IP addresses, on a server infrastructure, you will need to set up a static IP address (at least in most cases).
Terrible – and my ‘linux distro‘ isn’t the same as yours, there’s no ‘/etc/sysconfig/‘ folder.
Hi Ravi,
In Ubuntu 20.04 there is no interfaces file they switch to netplan. If you can update this article to include the new change it will help a lot.
thanks
Raouf
Well, this isn’t correct. Just trashed my Linux mint distro
Is it public Static IP? or can I use to access data from other networks?
Failed to restart network.service: Unit network.service not found.
I’m asking a question on a fairly old thread, but just in case, is it possible to do this on a WIFI network?
For example, when using the first command (# nano /etc/network/interfaces) in Ubuntu, the result I see is:
There isn’t an “eth0” on my server because it is connected by WIFI only. Will it still work using another option?
@Matthew,
Yes it will work I think so, just change the settings in the interfaces file as explained in this article.
Hi,
I set the static IP in ifcfg-eth0, added HWADDR and UUID, but on reboot system does not associate the IP to eth0.
This is VM. Any idea why its happening and steps to troubleshoot.
Thanks
I think you need to make sure that you select “manual” and the correct IP address, subnet mask, and gateway and save the configuration as explained in the article. Also, I personally would select a new and different IP address, so that you can really check if it has been saved by opening the terminal and typing:
after a restart.
If i set ip address as static am not able to ping google.com why and also packages are not installed.
Please give me reply as soon as possible.
@Rajeshkar,
Please add the DNS Name servers in your /etc/resolv.conf file..
@Ravi Saive thanks for your reply , yes i did /etc.resolv.conf also but getting the same problem
@Rajesekar,
The file is /etc/resolv.conf, in this file add your DNS name servers for example.
When I enter the /etc/resolv.conf file, what is it supposed to look like? And when you say “edit” do you mean delete what’s there and write what you’ve provided, or just add new lines?
Hello Marin,
Thank you very much for this article. It was a major help in my class project. This is my first time using a vm and it is an awesome learning experience. I’m really glad I ran into this article, it was well written and easy to follow.
Hi Marin!
Just wanted to say a million thank you’s for this well-written, comprehensive and easily-understood article! Awesome stuff! A real lifesaver too, as I had to quickly configure a static IP for myself to get access to remote computing. Thank you! :-D
A question from a linux newbie. Does this instruction apply for both ubuntu running on my desktop PC as well as debian linux on an embedded board?
@Rob,
Yes, the instructions will works on any Debian/Ubuntu based distribution without any issues..have you tried on your embedded board? does these instructions worked? let us know.
Hi Ravi, thanks for your reply.
Default the folder /etc/sysconfig does not exist on my embedded system.
Of Course i could create it as well as the files mentioned, but it would be out of the context of this instructions.
Thanks, Rob
I found here very good stuff! You are doing an excellent job and I like your site! Thanks!
@Ice,
Thanks for finding this site very useful and thanks for appreciating our work, Keep visiting for more such useful articles…
Great job, I was I actually looking for an article like this one. So thank you so very much. Keep up the good work.
@Alex,
Thanks for appreciating and finding this article useful, keep connected to Tecmint for such wonderful articles…:)
What if I have 2 NICs on my server one for LAN & one for WAN and I want to set one of them (WAN) as default gateway? How to configure this server as gateway and as a router.
@Rahul,
The easiest way to add default gateway using route command as shown:
Don’t forget to replace the gateway IP address and interface-name in the above command.
One can also use following command to setup static IP on eth0 interface for example.
# ifconfig eth0 192.168.1.1 netmask 255.255.255.0 up
# route add default gw 192.168.0.1
# service network restart
@Vikas,
Thanks for the tip, but I think setting IP address directly from the commandline using ifconfig and route will only allow you to set temporarily, once you reboot, these settings disappears. So, the best option to set static IP address permanently in network configuration files only….
wipe on reboot, so that best option is to set permannetly
Hello Ravi, What is difference between the service “NetworkManager” and “network”
@Augustine,
This article will help you to understand the difference between and NetworkManager and Network: http://askubuntu.com/questions/1786/what-is-the-difference-between-network-manager-and-ifconfig-ifup-etc
Why do we need to specify DNS in both ifcfg-eth0 and resolvlf.conf?
Hello Augustine,
Actually if you have added the DNS servers in the ifcfig-eth0 file the DNS servers will be automatically added to /etc/resolv.conf. You can skip defining the DNS servers in the ifcfig-eth0 file, but then you will need to have them set in /etc/resolv.conf manually. It’s a good practice to make sure that the DNS servers are specified correctly in both files, this is why the article says to set them in both files.