Linux hostname

From Ever changing code
Jump to navigation Jump to search

Debian based

In CentOS/Ubuntu and other distros you can control hostname using:

sudo hostnamectl set-hostname <new hostname>

In CentOS 7 it updates also /etc/hostname file that stores centrally the system's hostname

Amazon Linux AMI

# change to public DNS name
sudo vi /etc/sysconfig/network
HOSTNAME=webserver.mydomain.com
# or without public dns name
HOSTNAME=webserver.localdomain
# reboot

Amazon Linux 2

Change the system hostname to a public DNS name

sudo hostnamectl set-hostname webserver.mydomain.com
# reboot

Other

In other distros it should be enough to change:

$ vi /etc/hosts
127.0.0.1 ''newHostName''
127.0.0.1   localhost localhost.localdomain
::1         localhost localhost.localdomain

Ubuntu >14.04

$ vi /etc/hosts
127.0.0.1       localhost
127.0.1.1       ''newHostName'' ''newHostName''.home

Temp hostname

Temporarily changing hostname can be achieved by the command below then logout and login again to see the effect in bash.

$ hostname newHostName

Restart networking

If you changed /etc/hosts or /etc/sysconfig/network file you want to restart networking to ensure that changes will be persistent on reboot:

$ /etc/init.d/network restart        # Redhat based
$ sudo service networking restart    # Debian/Ubuntu distros