This article explains how to change the hostname and IP address of a Linux CentOS 7 server.
There are two basic facts about subnets that are in play when making these particular changes:
- The hostname is the name a computer (or server) goes by on the network. Each computer on a particular network must be named differently. In other words, there can only be one unique hostname on a subnet. For example, a typical home network might be addressed as 192.0.0.0 through 192,0.0.255. If a computer had a hostname of susan.mydomain.com another computer named that same name would create a network conflict.
- Each computer on a particular network must reside at a different IP address. In other words, there can only be one server at any given unique IP address on a subnet. In the same example network above, only one computer on that network could exist at any given IP address on that network. If you were to add another computer to that same IP address by setting that already used IP address in its network interface card, an error would occur.
In this scenario, I needed to replace an existing server with a newer server. There was terabytes of backed up information on that existing server. Those backups needed to be moved to other servers prior to rebuilding and upgrading the box. To compound the situation, the file migration would need to be done in the background to avoid service degradation on the subnet. Such a move was estimated to take as long as a two months.
How to change the Host name
Step 1: Check Existing Hostname
It is prudent to check what your current hostname is. Typing the following command in the console will show you:
hostnamectl
The static hostname should be displayed, as well as other information about your network configuration and operating system.
Step 2: Set a New Static Hostname
As CentOS 7 only allows Fully Qualified Domain Names (FQDNs), double-check the hostname you plan to use.
Acceptable values can include these:
- Lower-case letters a to z
- Numbers 0 to 9
- Periods and hyphens
- Hostnames must be between 2 and 63 characters
- Hostnames must start and end with a number or letter
- Type in the following command in the terminal:
hostnamectl set-hostname serverhost.exampledomain.com
Step 3: Check the Hostname
Next, verify the hostname by using the following command again:
hostnamectl
The console should display the new hostname.
Step 4: Edit the /etc/hosts File
In my example here, I am using nano, which happens to be my favorite Linux text editor. Start by opening the hosts file in nano by typing:
nano /etc/hosts
In the text editor, look for the line that begins with 127.0.0.1 (the IP address that refers to the system you are working on). It should read:
127.0.0.1 localhost localhost.localdomain localhost 4 localhost4.localdomain4 old.hostname
Change the existing entry (old.hostname in the example) to your new server hostname (serverhost.exampledomain.com was used in the example in step 2 above) and spell it the same as you did in Step 2.
Save the file and exit.
Step 5: Reboot and Check CentOS 7 machine hostname
Restart the computer (or server). Again, open a console window, and run:
hostnamectl
It should display the new hostname.
You can also use your text editor again to verify your /etc/hostsfile. It should still have the new hostname listed.
How to change the IP address
Step 1: Find Your Network Configuration File
Go to the /etc/sysconfig/network-scripts directory
Locate the configuration file of your interface (for example, mine showed ifcfg-eno1, ifcfg-eno1:0, and ifcfg-eno2).
Step 2: Edit Your Network Configuration File
Enter the following command:
nano /etc/sysconfig/network-scripts/ifcfg-eno1
Change the appropriate parts in the file.
ONBOOT=yes
BOOTPROTO=static
IP_ADDR=1.1.1.1 (Fill in your actual IP Address)
NETMASK=255.255.255.255 (Fill in your actual netmask here)
In the configuration file NM_CONTROLLED=no indicates that this interface will be set up using this configuration file, instead of being managed by Network Manager service. ONBOOT=yes tells the system to bring up the interface during boot.
Save your changes.
Step 3: Restart Network
Restart your network by entering the command:
service network restart
or
reboot
Your new static IP Address should be functioning now.
If it does not work, repeat the same steps again but this time set BOOTPROTO=none.