Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Linux Last updated: November 22, 2022
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

One of the essential tasks for a system administrator is to know how to change the hostname.

If you have entered the wrong hostname during installation or requirement to change it, you can change it as below. The following is tested on Ubuntu and CentOS.

Changing hostname involves three steps.

  • Change HOSTNAME value in /etc/sysconfig/network
  • Change hostname in /etc/hosts
  • Updating hostname on the terminal

1. Display Hostname

Let’s find out the configured name by executing hostname on the terminal.

[root@centos-s-1vcpu-2gb-lon1-01 ~]# hostname
centos-s-1vcpu-2gb-lon1-01
[root@centos-s-1vcpu-2gb-lon1-01 ~]#

Let’s change to geekflare

2. Change Hostname

Edit /etc/sysconfig/network file using vi, look for HOSTNAME= and update that to the desired name.

Note: I’ve noticed on some Cloud VM that HOSTNAME entry doesn’t exist. If you don’t find this on your server then you may ignore this step.

Before change:

# cat network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=centos-s-1vcpu-2gb-lon1-01

After change:

# cat network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=geekflare
[root@localhost sysconfig]#

The next step would change the hostname in a hosts file.

Edit /etc/hosts file, look for current hostname in 127.0.0.1 and server IPs (if any) line and update that to the desired one which you configured above.

Before change:

[root@centos-s-1vcpu-2gb-lon1-01 ~]# cat /etc/hosts | grep -v '^#'
127.0.0.1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

::1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

[root@centos-s-1vcpu-2gb-lon1-01 ~]#

After change:

[root@centos-s-1vcpu-2gb-lon1-01 ~]# cat /etc/hosts | grep -v '^#'
127.0.0.1 geekflare
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

::1 centos-s-1vcpu-2gb-lon1-01 centos-s-1vcpu-2gb-lon1-01
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

[root@centos-s-1vcpu-2gb-lon1-01 ~]#

3. Change hostname on terminal

And, finally, execute the hostname command with the desired value.

[root@centos-s-1vcpu-2gb-lon1-01 ~]# hostname geekflare

Try to re-login to your server and you’ll notice the new hostname.

Using DigitalOcean Server?

If you would like to change the hostname on the DigitalOcean VM, then you also have to update the hostname in /etc/hostname file.

Here is a quick video demonstration.

YouTube video

That was easy.

Isn’t it? Interested in learning more about Linux? Check out this administration course.

  • Chandan Kumar
    Author
    As the founder of Geekflare, I’ve helped millions to excel in the digital realm. Passionate about technology, I’m on a mission to explore the world and amplify growth for professionals and businesses alike.
Thanks to our Sponsors
More great readings on Linux
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Monday.com is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder