How to change hostname in Alpine Linux?

With the hostname command you can show or set/change the name of your host/server, hostname is part of the BusyBox suite.

You can get help with:

$ hostname --help
BusyBox v1.33.1 () multi-call binary.

Usage: hostname [-sidf] [HOSTNAME | -F FILE]

Show or set hostname or DNS domain name

    -s  Short
    -i  Addresses for the hostname
    -d  DNS domain name
    -f  Fully qualified domain name
    -F FILE Use FILE's content as hostname

Show hostname

$ hostname

Show the domain name

  $ hostname -d

Show IP

  $ hostname -i

Show the FQDN

  $ hostname -f

Change name

You need root permissions to change the hostname.

# hostname

You can use sudo:

$ sudo hostname

Keep in mind that the change is volatile and it is lost once the operating system is restarted.

Change name and persist

Edit /etc/hostname and /etc/hosts files, verify that the name you put in /etc/hostname have an entry in

Edit hostname

Add new name.

# echo mynewname > /etc/hostname

Edit hosts.

Verify that exist an enttry in /etc/hosts:

IP  mynewname

It is also possible to use FQDN, here is an example of my configuration

$ cat /etc/hostname
$ cat /etc/hosts    mynewname localhost.localdomain localhost
::1        localhost localhost.localdomain


Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.