This guide is created for people who want to set a static IP address on their CentOS computer or virtual machine.
Setting a static IP address is a very important thing to do if you want to set-up a webserver or web-application server. If you use a dynamic IP your IP address might change on every reboot or when the lease expires. Resulting in other application not being able to reach the server.
To follow the steps of this guide please make sure you have the following:
- An operating system running CentOS, Fedora or openSUSE
- A text editor to edit configuration files
- Access to the terminal of your OS, either using it locally or through SSH
- About 5-10 minutes of your time
If you don’t have a text editor installed, you can easily do so by running ‘sudo yum install nano‘, please make sure you have access to the internet so you can download the packages.
Listing the Network Adapters
First we need to check which network adapter you’re currently using. You can do so by using the following command:
ip link show
This will show a list of the current active adapters. The first one is often the loopback adapter (lo). In the picture below it shows that number 2 (eth0) is my active adapter. Remember the name of the adapter.
Getting a Static IP
Now we have the name of the active adapter, we can change the configuration file of the named adapter. Change your directory to:
You’re now in the directory where the config file is stored. Use this command to show a list of all the files in your directory:
As you can see there is a file called ‘ifcfg-<your_adapter_name>‘. We need to open this file with a text editor:
sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0
Do not forget to change the ‘eth0’ to your own adapter name.
You will see a list of options to edit.
Edit the options to your desire. To change the IP Address, change the line of IPADDR=”10.40.14.200″ to your own. Remember to change the DNS server and gateway accordingly.
Press CTRL-O to save your changes, or CTRL-X to close. To apply your changes, restart your networking service by using:
service network restart
You now have a Static IP address!