I'd like to show you a magic trick. Go to your favorite shell prompt, and type:
[skipworthy@showme ~]$ hostname showme
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 spot
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 ( 1605641804) [ 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: systemd 219 running in system mode. (+PAM +AUDIT +SEL INUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL + XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN) [ 2.667189] systemd: Detected virtualization kvm. [ 2.667200] systemd: Detected architecture x86-64. [ 2.667204] systemd: Running in initial RAM disk. [ 2.667250] systemd: 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
hostnamectl commands actually use kernel-level system calls named
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,
[root@showme skipworthy]# cat /etc/hostname showme
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
[skipworthy@showme ~]$ cat /etc/hosts 127.0.0.1 showme.forest showme 192.168.11.111 showme.forest showme 192.168.0.200 Jupiter 192.168.0.100 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.
-f switch when you invoke the
hostname command to see the FQDN of your host:
[skipworthy@showme ~]$ hostname -f showme.forest
[ Download now: A sysadmin's guide to Bash scripting. ]
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.