Linux Command: hostnamectl
Control system hostname
hostnamectl manages the system hostname. It can set the static hostname (persistent), the pretty hostname (UTF-8 descriptive name), and the transient hostname (temporary). hostnamectl is the systemd way to manage hostnames, replacing manual editing of /etc/hostname. Changes are permanent and take effect immediately. hostnamectl also shows useful system information including the machine ID, boot ID, operating system, kernel, and architecture.
Syntax
hostnamectl [COMMAND]Common Examples
hostnamectl— Shows hostname, OS, kernel, and architecture.sudo hostnamectl set-hostname production-web-01— Permanently changes the hostname.sudo hostnamectl set-hostname "Production Web Server 01" --pretty— Sets a descriptive name (can include spaces and special characters).sudo hostnamectl set-hostname newname && sudo sed -i "s/oldname/newname/g" /etc/hosts— Changes hostname and updates /etc/hosts to match.
Pro Tips
Master this and 230+ other Linux commands with our comprehensive eBooks and cheat sheets.
Related Resources