hostnameShow or set the system's host name
hostname prints the machine's name, its IP addresses (-I) or its fully qualified name (-f), and as root can change the name until the next reboot. To rename a server permanently use hostnamectl set-hostname, and keep /etc/hosts in step.
hostname [-s | -f | -I | -i] sudo hostname NEWNAME hostnamectl set-hostname NAMEYou're filling in an inventory spreadsheet and need the server's name and IPs.
sudoChanging the hostname needs root (CAP_SYS_ADMIN).
The hostname lives in the kernel, in the current UTS namespace: hostname reads it with uname() or gethostname() and sets it with sethostname(), which needs CAP_SYS_ADMIN. That change lasts until reboot. At boot, systemd reads /etc/hostname and sets it, so a permanent change means updating that file, which is what hostnamectl does over D-Bus through systemd-hostnamed. Options that sound similar work very differently: -I lists every address on every interface (getifaddrs), skipping loopback, and needs no DNS. -i and -f ask the resolver (getaddrinfo: /etc/hosts, then DNS) about the name, which is why on Ubuntu -i often prints 127.0.1.1 from /etc/hosts and why -f fails on a box whose name doesn't resolve. sudo also resolves the hostname, and warns when it can't.
(none)Print the hostname-sShort name: everything before the first dot-fFully qualified domain name, via /etc/hosts or DNS-dDNS domain part of the FQDN-IAll IP addresses of all interfaces (no loopback, no DNS)-iAddresses the hostname resolves to (often 127.0.1.1 on Ubuntu)NAME (as root)Set the hostname until reboothostnamectlShow host info, or set-hostname permanentlysudo hostname web-01 and calling it doneThat only changes the running kernel's name. After a reboot /etc/hostname wins and the old name comes back.
sudo hostnamectl set-hostname web-01Using hostname -i to get the server's IP-i resolves the name via /etc/hosts; on Ubuntu that's 127.0.1.1. It can also return nothing or fail if DNS is broken.
hostname -I, or ip -br addrRenaming the host but leaving /etc/hosts alonesudo and other tools resolve the hostname; they print 'unable to resolve host' and may pause on DNS timeouts.
Update the 127.0.1.1 line in /etc/hosts tooExpecting the new name in your current promptThe shell expanded \h when it started. The hostname did change.
exec bash, or log in againTry these on a Linux machine of your own. Each task has a hint and a solution.
Print the short hostname, the FQDN and all IP addresses of the box.
Find where the hostname is stored for the next boot and print it.
Find out what the hostname resolves to and which file decides that.
hostnameShow or set the system's host name
hostname prints the machine's name, its IP addresses (-I) or its fully qualified name (-f), and as root can change the name until the next reboot. To rename a server permanently use hostnamectl set-hostname, and keep /etc/hosts in step.
hostname [-s | -f | -I | -i] sudo hostname NEWNAME hostnamectl set-hostname NAMEYou're filling in an inventory spreadsheet and need the server's name and IPs.
sudoChanging the hostname needs root (CAP_SYS_ADMIN).
The hostname lives in the kernel, in the current UTS namespace: hostname reads it with uname() or gethostname() and sets it with sethostname(), which needs CAP_SYS_ADMIN. That change lasts until reboot. At boot, systemd reads /etc/hostname and sets it, so a permanent change means updating that file, which is what hostnamectl does over D-Bus through systemd-hostnamed. Options that sound similar work very differently: -I lists every address on every interface (getifaddrs), skipping loopback, and needs no DNS. -i and -f ask the resolver (getaddrinfo: /etc/hosts, then DNS) about the name, which is why on Ubuntu -i often prints 127.0.1.1 from /etc/hosts and why -f fails on a box whose name doesn't resolve. sudo also resolves the hostname, and warns when it can't.
(none)Print the hostname-sShort name: everything before the first dot-fFully qualified domain name, via /etc/hosts or DNS-dDNS domain part of the FQDN-IAll IP addresses of all interfaces (no loopback, no DNS)-iAddresses the hostname resolves to (often 127.0.1.1 on Ubuntu)NAME (as root)Set the hostname until reboothostnamectlShow host info, or set-hostname permanentlysudo hostname web-01 and calling it doneThat only changes the running kernel's name. After a reboot /etc/hostname wins and the old name comes back.
sudo hostnamectl set-hostname web-01Using hostname -i to get the server's IP-i resolves the name via /etc/hosts; on Ubuntu that's 127.0.1.1. It can also return nothing or fail if DNS is broken.
hostname -I, or ip -br addrRenaming the host but leaving /etc/hosts alonesudo and other tools resolve the hostname; they print 'unable to resolve host' and may pause on DNS timeouts.
Update the 127.0.1.1 line in /etc/hosts tooExpecting the new name in your current promptThe shell expanded \h when it started. The hostname did change.
exec bash, or log in againTry these on a Linux machine of your own. Each task has a hint and a solution.
Print the short hostname, the FQDN and all IP addresses of the box.
Find where the hostname is stored for the next boot and print it.
Find out what the hostname resolves to and which file decides that.