Hostname

From LinuxReviews
Jump to navigationJump to search

A system hostname is, put simply, a computer's name. This name is used to identify and access it on both local and global networks if a fully qualified domain name is set. hostname is also a command which can be used to display and change the computers hostname.

HOWTO change a computers hostname[edit]

A machines hostname can be changed in a huge variety of ways which all end up changing the configuration file /etc/hostname which is where the machines hostname is specified. This file has historically only contained the actual machine's name (like "chaekyung") and not the fully qualified domain name (like "chaekyung.example.com").

hostnamectl[edit]

systemd enabled distributions (most of them) have a command called hostnamectl which will display the system hostname as well as a lot of incriminating information when it's ran without an argument. It has the option set-hostname with takes a hostname as an argument. Thus; if you want to change your hostname to yoona you can run this command:

sudo hostnamectl set-hostname yoona

hostnamectl will change the system's run-time hostname and make the new hostname permanent by writing it to the configuration file /etc/hostname

the good old manual way[edit]

The command hostname will let you change the runt-ime hostname by running it with a new host name as an argument:

sudo hostname yoona

will change the hostname to yoona at run-time but it will not make it permanent by changing /etc/hostname. The change will be lost after a reboot.

Making a hostname change is a simple matter of editing the /etc/hostname file. which means it can be changed by simply writing a new hostname to that file:

sudo sh -c "echo yoona > /etc/hostname"

Kemonomimi rabbit.svg
Note: The above command runs sudo sh -c to start a sub-shell because it uses file redirection (>) and this does not work without starting sudo in a sub-shell[1]. Running sudo echo yoona > /etc/hostname would try to write to /etc/hostname as a regular user - which won't work. A simple echo yoona > /etc/hostname would obviously be enough if you run that command as root

notes[edit]

Questions?[edit]


Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.