Skip to main content

How to change the identity of a Linux system

Set the hostname on your Linux systems by using the hostnamectl command.
How to change the identity of a Linux system

Photo by Pixabay from Pexels

I'd like to show you a magic trick. Go to your favorite shell prompt, and type:

[skipworthy@showme ~]$ hostname

The hostname command is provided in Linux to query "who" the computer thinks it is. How did that happen? How does my computer know what it's called? I show you how the trick is done in a bit, but first, let me show you another one:

[root@showme skipworthy]# hostnamectl set-hostname spot
[root@showme skipworthy]# hostname

Thanks once again to systemd (which is implemented in most but not all modern GNU/Linux distributions, including Red Hat-based systems) changing the name of your Linux computer is pretty easy. (By the way, the name in the prompt will change with the next login.) I've discussed how DNS allows us to identify a computer on the network by mapping a name to an IP address. This is on the other side of that. Here you're looking at how the machine identifies itself, which is important for making things like authentication work properly if the computer is part of a domain.

[ You might also enjoy reading: 10 basic Linux commands you need to know ]

After the hardware is enumerated and drivers have been loaded at bootup, on a systemd-based distribution, the bootloader loads and runs the systemd scripts. Near the beginning of this process is when the hostname is set:

  2.653601] rtc_cmos 00:00: setting system clock to 2020-11-17 19:36:44 UTC (
[  2.654885] Freeing unused kernel memory: 1980k freed
[  2.655179] Write protecting the kernel read-only data: 12288k
[  2.657125] Freeing unused kernel memory: 416k freed
[  2.658710] Freeing unused kernel memory: 552k freed
[  2.663220] random: systemd: uninitialized urandom read (16 bytes read)
[  2.663530] random: systemd: uninitialized urandom read (16 bytes read)
[  2.663543] random: systemd: uninitialized urandom read (16 bytes read)
[  2.667147] systemd[1]: systemd 219 running in system mode. (+PAM +AUDIT +SEL
[  2.667189] systemd[1]: Detected virtualization kvm.
[  2.667200] systemd[1]: Detected architecture x86-64.
[  2.667204] systemd[1]: Running in initial RAM disk.
[  2.667250] systemd[1]: Set hostname to <showme>.
[  2.701020] random: systemd: uninitialized urandom read (16 bytes read)

Apart from that, in systems where systemd is not managing the user-space, you can configure the hostname in a couple of files. These files are actually where systemd looks for this info and records it when you use the hostnamectl command. The hostname and hostnamectl commands actually use kernel-level system calls named gethostname, getdomainname, and some functions provided by the resolver system call. These have been around since pretty early on in the history of the Linux kernel. These are the same system calls used by the shell to populate the machine's name in the prompt, among other places.

Both of the files you're going to look at reside in /etc, which you'll recall is the default location for most of the files used to configure and manage a Linux system. The first one is called, appropriately, /etc/hostname:

[root@showme skipworthy]# cat /etc/hostname

Pretty simple: When you invoke hostname from the shell prompt, Linux looks at the /etc/hostname file for the answer. You can also use the hostname command to change the computer name, but in systemd-managed kernels, it's generally preferred to use the hostnamectl command instead.

The other file is /etc/hosts, which is where Linux looks to translate IP addresses to names before it checks /etc/resolv.conf for DNS info. Here is the /etc/hosts file:

[skipworthy@showme ~]$ cat /etc/hosts       showme.forest showme  showme.forest showme      Jupiter      uhura

The first couple of lines are what you want to look at. Note the format is:

ip address   fqdn alias

So you have the IP address, then the hostname.domainname (or Fully Qualified Domain Name), followed by the hostname. You can also add aliases or additional domain aliases if they are used in the environment.

Use the -f switch when you invoke the hostname command to see the FQDN of your host:

[skipworthy@showme ~]$ hostname -f

[ Download now: A sysadmin's guide to Bash scripting. ] 

The hostnamectl command has some other interesting and useful switches and flags: You can set the environment type (development vs. production, for example), and you can even put location notes (rack 3, room 200, for example). You can query and set these remotely. As usual, you can learn all kinds of cool things from the good ol' man pages.

Topics:   Linux   Linux administration  
Author’s photo

Glen Newell

Glen Newell has been solving problems with technology for 20 years. As a Systems Engineer and administrator, he’s built and managed servers for Web Services, Healthcare, Finance, Education, and a wide variety of enterprise applications. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.