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

[skipworthy@showme ~]$ hostname
showme

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
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[1]: 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[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
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 /etc/hosts file:

[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.

Use the -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. ] 

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.


저자 소개

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. He’s been working with and promoting open source technologies and methods for his entire career and loves to share what he learns and help people understand technology.

UI_Icon-Red_Hat-Close-A-Black-RGB

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Virtualization icon

가상화

온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래