Alpine Linux Change Hostname (computer name)

I am a new Alpine Linux user. By default, my server name is set to ‘localhost’ during installation. Now, I would like to change the computer name. How do I change the Alpine Linux computer name? Can you tell me more about Alpine Linux change hostname command?

We use the hostname command to get or set the hostname or DNS domain name on Alpine Linux. Since it is a systemd free system, there is no hostnamectl. Alpine Linux comes with OpenRC as an init system. Hence the procedure to change or update hostname is different. This page explains how to change hostname on Alpine Linux using the CLI.

Displaying hostname on Alpine Linux

Run the following command to view current hostname:
hostname
Outputs:

localhost

To see fully qualified domain name, run:
hostname -f

localhost.localdomain

Changing Alpine Linux hostname

Alpine Linux change hostname with the following syntax:
# hostname {new-hostname-here}
# hostname ln-sg-vpn-1

Setting system hostname permanently

To set the system hostname, edit the /etc/hostname file as root user. For example:
# vi /etc/hostname
## OR ##
# nano /etc/hostname

Delete existing name and add a new name such as:
ln-sg-vpn-1
Save and close the editor. Another option is to use the echo command to overwrite this file:
# echo "ln-sg-vpn-1" > /etc/hostname
Immediately activate the change by running the following command. In other words use the /etc/hostname file’s content as hostname:
# hostname -F /etc/hostname
Make sure you update the /etc/hosts file too with a staic IP configuration:
# vi /etc/hosts
The syntax is:

ipv4.here     ln-sg-vpn-1
ipv6.here     ln-sg-vpn-1
# we can use FQDN too #
192.168.2.250 ln-sg-vpn-1.cyberciti.biz
Save and close the vim editor.
NOTE: No need to reboot the system.

Getting help

Run the following command:
hostname --help
The hostname command comes from BusyBox version 1.31.1. It is multi-call binary. The syntax is as follows get or set hostname and DNS domain name:
Usage: hostname [OPTIONS] [HOSTNAME | -F FILE]
Where,

  • -s : Short name of the system
  • -i : Addresses for the hostname
  • -d : DNS domain name
  • -f : Fully qualified domain name (FQDN)
  • -F FILE : Use FILE’s content as hostname

Conclusion

On Alpine Linux, one can use the hostname command to control the system hostname and set a new name. It is the recommended method for all Alpine Linux users. Another option is to update the /etc/hostname file using a text editor such as nano or vi. You can download Alpine Linux for production or testing purpose.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • A dev from hell Feb 2, 2021 @ 6:51

    this page is very useful and solved my problem with Docker image.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum