Hostname is a identification for a machine to identify it in a network To apply the changes in hostname first need to be super user that is root. As this is a system change so prvilege of super user is require. Below is the command to become root user. $ su -
Or $ sudo -i
To change hostname in Debian below is the command # echo "<hostname>" > /etc/hostname
After systemd added to Linux distros it became more easier to manage, due to this in most of the Linux distro commands are much similar. To change the hostname use the below command.
# hostnamectl set-hostname [desire hostname]
For example: # hostnamectl set-hostname linuxmachine
Or you may use sudo tool with above command as shown in below example. $ sudo hostnamectl set-hostname [desire hostname]
For example: $ sudo hostnamectl set-hostname linuxmachine
Below command to check current hostname of the machine. $ hostname
linuxmachine There is no require to reboot the machine to apply changes done in hostname. Re-login to see the changes in shell prompt if configured. After changing the hostname it is always recommended to update hostname in hosts file or if there is no entry for hostname then add hostname beside loopback IP address or else if you want to resolve hostname by any other IP address then add IP address with the new hostname. Like in below example. 10.0.1.2 linuxmachine
Below is the default hosts file entries are found in CentOS machine which will be without hostname entry but in other OS's like Ubuntu hostname are added in installation in that case when you change the hostname then it is require to update hostname in hosts file to avoid any conflict. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 Hostname entry in hosts file is require when application try to communication with hostname at then machine will try to resolve hostname to IP address from the hosts file. # vi /etc/hosts
systemd: /usr/bin/hostnamectl
|