Posts Tagged ‘change hostname linux’

How to change hostname in Ubuntu, Debian and Redhat based Linux machine

Thursday, September 27th, 2018

change-hostname-on-any-linux-distributions-universal-way-to-change-linux-hostname-howto

The hostname is set at the time when a Linux OS is installed by the respective installer (set-up scripts) on a bare-metal server or  virtual machine. 

Historically to change the hostname in most GNU / Lonux distributions (Debian / Ubuntu / Fedora / CentOS etc.) it was as easy as:

1. Getting your current setting for hostname with hostname command
 

hipo@jeremiah:~$ hostname –fqdn
jeremiah


Logging to the remote machine via ssh.
 

 

 

ssh user@whetever-host.com


3. Editting /etc/hosts and substituting with the new desired hostname
 

 

vim /etc/hosts
127.0.0.1   localhost
127.0.0.1   jeremiah

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

4. Run 

/etc/init.d/hostname.sh start


5. Run command
 

hostname your-new-desired-hostname


and logout and login again to the host to make the new hostname active for the ssh session

Since around 2015 a new way was introduced to change hostname in Ubuntu 13.04 onwards and Fedora 21 and Debian 8 / 9 the way to set a new hostname comes again up to editting
/etc/hosts

and running command:
 

hostnamectl set-hostname your-new-desired-hostname

 

 

On Redhat based Linux distributions and Red Hat Enterprise Linux to change the hostname you will also need to edit:
 

vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME="domain.com"
GATEWAY="192.168.0.1"
GATEWAYDEV="eth0"
FORWARD_IPV4="yes"


Another universal way to edit hostname on any Linux distribution is to use sysctl cmd like so:
 

sysctl kernel.hostname

sysctl kernel.hostname=your-desired-hostname