hostname – show or set the system hostname

hostname command
hostname command

hostname displays or sets the system’s hostname — the name that identifies the machine on a network.

Synopsis

hostname [OPTIONS] [NAME]

Common Options

OptionDescription
-fFQDN (fully qualified domain name)
-sShort hostname
-dDomain name
-iIP address
-IAll IP addresses

Examples

Show hostname

$ hostname
server01

$ hostname -f
server01.example.com

$ hostname -s
server01

Show IP addresses

$ hostname -i
192.168.1.10

$ hostname -I
192.168.1.10 10.0.0.5

Set hostname (temporary)

$ sudo hostname newname

Set hostname permanently (systemd)

$ sudo hostnamectl set-hostname server01.example.com

Permanent Hostname

systemd systems (modern Linux)

$ sudo hostnamectl set-hostname myserver
$ hostnamectl
   Static hostname: myserver
         Icon name: computer-vm
           Chassis: vm
        Machine ID: abc123...
           Boot ID: def456...
    Virtualization: kvm
  Operating System: Ubuntu 22.04 LTS
            Kernel: Linux 5.15.0

Traditional method

# Edit /etc/hostname
$ sudo nano /etc/hostname
myserver

# Update /etc/hosts
$ sudo nano /etc/hosts
127.0.0.1   localhost
127.0.1.1   myserver.example.com myserver

Tips

  • Use hostnamectl: Preferred on systemd systems
  • Reboot not always needed: But recommended for full effect
  • Update /etc/hosts: To avoid resolution delays
  • Cloud instances: May reset on reboot, check cloud-init config

See Also

Tutorials