How to Change the Hostname in Linux (Debian, Ubuntu, Arch, RedHat)

Linux Change Hostname

This article will show you how to change the hostname for your Linux device (Debian, Arch, Ubuntu, or RedHat).

The methods below should work for the vast majority of current and obsolete Linux Distributions.

What is the Hostname?

The hostname of a device on the network is the human-readable label of the system. It can be used to identify or connect to a system on the network instead of connecting to it via an IP address.

Displaying the Current Hostname

Regardless of your distribution, you can find out the current hostname of your system by running the hostname command:

hostname

Easy!

Temporarily Changing the Hostname

The hostname command can also be used to change the hostname temporarily. In the terminal run:

hostname NEWHOSTNAME

…where NEWHOSTNAME is the new hostname you wish to use. Alphanumeric characters, dashes, and underscores only!

Changing the hostname via this method will only last to the next time your computer shuts down or reboots – it is a temporary change.

Permanently Changing the Hostname on Modern Releases of Ubuntu, Debian, RedHat/Centos, and Arch Based Linux Distributions

All commands from here on should be run as root or using sudo – administrative privileges are required.

Modern releases of Ubuntu, Debian, Arch, and other distributions based on them, utilize the SystemD set of system components – components that underpin the operating system and provide many of the system functions.

To permanently change the hostname on SystemD based systems, you can run the hostnamectl command in the terminal:

sudo hostnamectl set-hostname NEWHOSTNAME

…where NEWHOSTNAME is the new hostname you wish to use. If you’re permanently changing your hostname, it’s probably worth rebooting after the change is made so that all services are aware of the change.

If you are running a Linux operating system released in the last few years, it’s most likely that it’s SystemD based, so this is probably what you’re looking for!

Permanently Changing the Hostname on Legacy Debian/Ubuntu Based Linux Distributions

Older versions of the above are not based on SystemD and lack the hostnamectl command, so you’ll have to edit some configuration files to change the hostname.

First, edit the /etc/hostname file (here I’m using the nano text editor):

sudo nano /etc/hostname

The /etc/hostname file contains one thing and one thing only – a single line of text containing the system’s hostname.

Please note the current hostname for later, then edit it to what you want, save, and close.

Next, you’ll have to edit /etc/hosts:

sudo nano /etc/hosts

There will be a line somewhere in the file that looks like this:

127.0.0.1 PREVIOUSHOSTNAME

…where PREVIOUSHOSTNAME is the old hostname, which you made a note of in the last step. Change it so that it reads:

127.0.0.1 NEWHOSTNAME

…where NEWHOSTNAME is the name you gave your system in the /etc/hosts file.

To apply the change, run:

/etc/init.d/hostname restart

Though as before, I recommend rebooting after changing the hostname so that all services are aware of the change – some may have cached it.

Permanently Changing the Hostname on RedHat/Centos Based Linux Distributions

Finally, here’s how to change the hostname on older versions of RedHat/Centos. Edit the following file:

sudo nano /etc/sysconfig/network

The contents will look something like this (yours will probably differ slightly):

NETWORKING=yes
HOSTNAME="OLDHOSTNAME"
GATEWAY="192.168.1.254"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"

Find the line beginning with HOSTNAME= and replace the value following it (keeping the quotes) with the new hostname you wish to use.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *