How to Change Hostname of Various Linux Systems

By:   |   Updated: 2021-12-15   |   Comments   |   Related: More > SQL Server on Linux


   Free MSSQLTips webinar - "Efficient Monitoring and Management of SQL Server" (click to register)

Problem

I installed RHEL 8.1 on a VM server and noticed the Linux system was not configured properly and it was still showing as localhost. Changing the hostname in Linux is quite simple and I am going to show you how to change the hostname of various Linux systems like RHEL, SUSE and Ubuntu systems.

Solution

Hostname is the name of the machine through which we recognize the machine on the network. There are multiple ways to change the hostname of the machine and if you want to test them then you can do it in your development environment. I strongly recommend not doing this on production machines unless it is a real requirement.

Change Hostname of Red Hat Enterprise Linux System

First, we will check the current hostname of the VM by simply running the hostname command. You can also use hostnamectl as well which we will also use below to change the hostname.

--Check current hostname
$ hostname
 
--You can also run below command to check the hostname
$ hostnamectl 

You can see below the current hostname of this machine is "localhost.localdomain".

linux command line

RHEL 8.1 comes with service manager systemd that provides the hostnamectl command to manage hostnames in Linux without the need to locate and edit the /etc/hostname. We will use this command to change the hostname.

--Change hostname using hostnamectl
$ sudo hostnamectl set-hostname <NEW-SERVER-NAME>
 
--I used below command
$ sudo hostnamectl set-hostname SQLServer-RHEL8
 
--Check Hostname after making changes
$ hostnamectl 

You can see I connected using su account in the below image. This command needs sudo access to change the hostname. You can see the command has been executed successfully and the hostname has been changed to SQLServer-RHEL8.

linux command line

If we use the hostname command again, we can see the current hostname below. Another thing to notice is the terminal prompt now show "[manvendra@SQLServer-RHEL8 ~]" where before it showed "[manvendra@loasthost ~]".

linux command line

You can also connect to PuTTY to verify this change. Get the IP of the server by running ifconfig as I did in below image or you can use the new hostname to establish the connection to the server.

linux command line

You can see in the below highlighted rectangle where the server name is now displaying the new hostname.

linux command line

Change Hostname of SuSE Linux System

Now, we look at how to do this for a SUSE Linux system.

Connect to your target SuSe server either using PuTTY or by logging in to the server locally. If logged in locally then open the terminal by right clicking on your SuSE desktop and choose Open Terminal option as shown in the below image.

linux command line

I used the command hostnamectl to check and change the hostname details.

--Check Hostname e 
$ hostnamectl 

You can see the hostname is linux-v9km in the below image.

linux command line

Run the below command to make the change.

--Change hostname using hostnamectltl
$ sudo hostnamectl set-hostname <NEWSERVERNAME>
 
--I used below command$ sudo hostnamectl set-hostname SQLServer-SUSE12

After running the command, you can see below the hostname has been changed to SQLServer-SUSE12.

linux command line

You can also use the IP of the machine and make a remote connection using PuTTY to validate the changes.

linux command line

Change Hostname of Ubuntu Linux System

Use the below command to perform a hostname change for an Ubuntu system.

--Check Hostname e 
$ hostnamectl 
 
--Change hostname using hostnamectl$ sudo hostnamectl set-hostname <NEWSERVERNAME> 

Another Way to Change Linux Hostname

Another approach is to edit the hostname file in /etc/hostname.

Follow the below steps to change the hostname of a Linux system. The changes will be reflected once you will reboot the system after making all changes.

  1. Edit /etc/hostname, remove old hostname and make an entry of new hostname
  2. Edit /etc/hosts, replace old hostname with new hostname
  3. Reboot the system
--Edit /etc/hostname
--Remove old name and enter new hostname in this file
$ sudo nano /etc/hostname
 
--Edit /etc/hosts file. Replace older hostname with the new name
$ sudo nano /etc/hosts
 
--Reboot the server
$ Sudo reboot 
Next Steps
  • Sometimes there may be a need to change the hostname for a Linux system and hopefully the above will provide you the tools to make these changes. 
  • Read these other articles related to SQL Server on Linux.



Get Started Now - Click here to get your free 14 day trial of SolarWinds Database Insights




get scripts

next tip button



About the author
MSSQLTips author Manvendra Singh Manvendra Singh has over 5 years of experience with SQL Server and has focused on Database Mirroring, Replication, Log Shipping, etc.

View all my tips


Article Last Updated: 2021-12-15

Comments For This Article





download














get free sql tips
agree to terms