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
  minombre

Show the domain name

  $ hostname -d
  my.domain

Show IP

  $ hostname -i
  127.0.0.1

Show the FQDN

  $ hostname -f
  myname.my.domain

Change name

You need root permissions to change the hostname.

# hostname mynewname.librebyte.net

You can use sudo:

$ sudo hostname mynewname.librebyte.net

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
/etc/hosts.

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
mynewname.librebyte.net
$ cat /etc/hosts
127.0.0.1    mynewname localhost.localdomain localhost
::1        localhost localhost.localdomain
192.168.100.195 mynewname.librebyte.net

References

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.