CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / System Information / hostname

hostname

Show or set the system's host name

Beginneressentialnetworkingsystem info

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.

Syntaxhostname [-s | -f | -I | -i] sudo hostname NEWNAME hostnamectl set-hostname NAME

Watch it run

You're filling in an inventory spreadsheet and need the server's name and IPs.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
hostname

The kernel's hostname, the same string as uname -n and the one in your prompt.

Anatomy of a command

$
commandsudo

Changing the hostname needs root (CAP_SYS_ADMIN).

How it works

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.

Options you'll actually use

(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 reboot
hostnamectlShow host info, or set-hostname permanently

Common mistakes

sudo hostname web-01 and calling it done

That only changes the running kernel's name. After a reboot /etc/hostname wins and the old name comes back.

sudo hostnamectl set-hostname web-01
Using 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 addr
Renaming the host but leaving /etc/hosts alone

sudo 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 too
Expecting the new name in your current prompt

The shell expanded \h when it started. The hostname did change.

exec bash, or log in again

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print the short hostname, the FQDN and all IP addresses of the box.

  2. 2

    Find where the hostname is stored for the next boot and print it.

  3. 3

    Find out what the hostname resolves to and which file decides that.

Related commands

unameShow the kernel name, version and architectureipShow and change interfaces, addresses and routesdigQuery DNS and read the full answer
PreviousunameNextlscpu
Linux / System Information / hostname

hostname

Show or set the system's host name

Beginneressentialnetworkingsystem info

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.

Syntaxhostname [-s | -f | -I | -i] sudo hostname NEWNAME hostnamectl set-hostname NAME

Watch it run

You're filling in an inventory spreadsheet and need the server's name and IPs.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 4
hostname

The kernel's hostname, the same string as uname -n and the one in your prompt.

Anatomy of a command

$
commandsudo

Changing the hostname needs root (CAP_SYS_ADMIN).

How it works

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.

Options you'll actually use

(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 reboot
hostnamectlShow host info, or set-hostname permanently

Common mistakes

sudo hostname web-01 and calling it done

That only changes the running kernel's name. After a reboot /etc/hostname wins and the old name comes back.

sudo hostnamectl set-hostname web-01
Using 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 addr
Renaming the host but leaving /etc/hosts alone

sudo 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 too
Expecting the new name in your current prompt

The shell expanded \h when it started. The hostname did change.

exec bash, or log in again

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print the short hostname, the FQDN and all IP addresses of the box.

  2. 2

    Find where the hostname is stored for the next boot and print it.

  3. 3

    Find out what the hostname resolves to and which file decides that.

Related commands

unameShow the kernel name, version and architectureipShow and change interfaces, addresses and routesdigQuery DNS and read the full answer
PreviousunameNextlscpu