Hostname Info and Management with hostnamectl
Use hostnamectl to view and update hostname metadata
I’ve just realised that I have been managing hostnames the old, manual way on most of my servers:
- check
/etc/hosts
- update network configuration scripts
- if necessary, edit
/etc/hostname
- reboot
But the same can be accomplished much easier on most of the modern Linux distros and releases: just use the hostnamectl
command.
Without any parameters, hostnamectl
will show you current settings:
greys@becky:~ $ hostnamectl
Static hostname: becky
Icon name: computer
Machine ID: 18634e1404184594bc66ddacfba0bf43
Boot ID: 7f9b03a40fdf492bb0c0dd3b6f6a7e94
Operating System: Raspbian GNU/Linux 9 (stretch)
Kernel: Linux 4.14.48-v7+
Architecture: arm
There’s quite a few parameters that hostnamectl
command takes, all implementing additional flexibility in naming and tracking your nodes on infrastructure.
set-hostname
– actually configuring the hostname (you don’t need to know config file locations, just enter the hostname)set-chassis
– easy way of confirming the hardware your OS is running on (and whether it’s a VM or not)set-deployment
– option to specify if your Linux box is development, staging or productionset-location
– allows you to specify geographical location of the server (freeform text)
For example, here’s how I can specify that my Raspberry Pi is a server grade hardware:
root@becky:~# hostnamectl set-chassis "server"
… if we check now, we will see the Chassis field showing:
root@becky:~# hostnamectl
Static hostname: becky
Icon name: computer-server
Chassis: server
Machine ID: 18634e1404184594bc66ddacfba0bf43
Boot ID: 7f9b03a40fdf492bb0c0dd3b6f6a7e94
Operating System: Raspbian GNU/Linux 9 (stretch)
Kernel: Linux 4.14.48-v7+
Architecture: arm