hostname - set or print name of current host system

The hostname utility prints the name of the current host. The super-user (root) can set the hostname by supplying an argument. For FreeBSD, if you wish to make changes permanent, you'd need also need to edit /etc/rc.conf

For Linux, it will depend on the distro you're using. Latest versions of Ubuntu, CentOS, Debian, Fedora, and so on, will come with systemd, a system and service manager that provides a hostnamectl command to manage hostnames in Linux.

If you're root already, you can change your hostname by typing:

$ hostnamectl set-hostname MY_NEW_HOSTNAME

Otherwise, </code>$ hostnamectl set-hostname MY_NEW_HOSTNAME</code>

For older Linux edit the hostname file, /etc/hostmame

Once this is done, add another record for the hostname in /etc/hosts

For example

127.0.0.1 host1-linux-node

After this, you'd need to execute the following command: /etc/init.d/hostname restart

If you're using CentOS-based system that use init, the hostname is changed by modifying, /etc/sysconfig/network file

Here's the snippet from that file:

$ cat /etc/sysconfig/network

NETWORKING=yes
HOSTNAME="host1-legacy-linux-node"
GATEWAYDEV=ens160
GATEWAY=192.168.10.1
FORWARD_IPV4="yes"

To make changes permament, change the value next to “HOSTNAME” to the one of your desired hostname.