Difference between revisions of "Linux hostname"
Jump to navigation
Jump to search
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Debian based = | |||
In CentOS/Ubuntu and other distros you can control <tt>hostname</tt> using: | In CentOS/Ubuntu and other distros you can control <tt>hostname</tt> using: | ||
<source lang=bash> | |||
sudo hostnamectl set-hostname <new hostname> | |||
</source> | |||
In CentOS 7 it updates also /etc/hostname file that stores centrally the system's hostname | |||
= RPM based = | |||
In Redhat (RPM) based distros change the hostname permanently by editing: | In Redhat (RPM) based distros change the hostname permanently by editing: | ||
<source lang=bash> | |||
$ vi /etc/sysconfig/network | |||
NETWORKING=yes | |||
HOSTNAME=newHostName | |||
# requires a reboot for eg. Amazon Linux | |||
</source> | |||
= RHEL 7 or CentOS 7 = | |||
<source lang=bash> | |||
# 1 | |||
sudo hostnamectl set-hostname -–static <new-name> # this command does not update /etc/hosts file | |||
# 2 on your RHEL 7 or CentOS 7 but not required for RHEL 7.4 and above | |||
sudo vi /etc/cloud/cloud.cfg | |||
# 3 Add the following string to the bottom of the /etc/cloud/cloud.cfg file to be sure that the new | |||
sudo vi /etc/cloud/cloud.cfg | |||
preserve_hostname: true | |||
# reboot | |||
hostname #verify that the hostname change persisted | |||
</source> | |||
= Amazon Linux AMI = | |||
<source lang=bash> | |||
# change to public DNS name | |||
sudo vi /etc/sysconfig/network | |||
HOSTNAME=webserver.mydomain.com | |||
# or without public dns name | |||
HOSTNAME=webserver.localdomain | |||
# reboot | |||
</source> | |||
= Amazon Linux 2 = | |||
Change the system hostname to a public DNS name | |||
<source lang=bash> | |||
sudo hostnamectl set-hostname webserver.mydomain.com | |||
# reboot | |||
</source> | |||
= Other = | |||
In other distros it should be enough to change: | In other distros it should be enough to change: | ||
<source lang=bash> | |||
$ vi /etc/hosts | |||
127.0.0.1 ''newHostName'' | |||
127.0.0.1 localhost localhost.localdomain | |||
::1 localhost localhost.localdomain | |||
</source> | |||
= Ubuntu >14.04 = | |||
<source lang=bash> | |||
$ vi /etc/hosts | |||
127.0.0.1 localhost | |||
127.0.1.1 ''newHostName'' ''newHostName''.home | |||
</source> | |||
= Temp hostname = | |||
Temporarily changing <tt>hostname</tt> can be achieved by the command below then logout and login again to see the effect in bash. | Temporarily changing <tt>hostname</tt> can be achieved by the command below then logout and login again to see the effect in bash. | ||
$ '''hostname''' ''newHostName'' | $ '''hostname''' ''newHostName'' | ||
= Restart networking = | |||
If you changed <tt>'''/etc/hosts'''</tt> or <tt>'''/etc/sysconfig/network'''</tt> file you want to restart networking to ensure that changes will be persistent on reboot: | If you changed <tt>'''/etc/hosts'''</tt> or <tt>'''/etc/sysconfig/network'''</tt> file you want to restart networking to ensure that changes will be persistent on reboot: | ||
$ /etc/init.d/network restart # Redhat based | $ /etc/init.d/network restart # Redhat based | ||
$ sudo service networking restart # Debian/Ubuntu distros | $ sudo service networking restart # Debian/Ubuntu distros |
Latest revision as of 22:06, 3 April 2019
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
RPM based
In Redhat (RPM) based distros change the hostname permanently by editing:
$ vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=newHostName # requires a reboot for eg. Amazon Linux
RHEL 7 or CentOS 7
# 1 sudo hostnamectl set-hostname -–static <new-name> # this command does not update /etc/hosts file # 2 on your RHEL 7 or CentOS 7 but not required for RHEL 7.4 and above sudo vi /etc/cloud/cloud.cfg # 3 Add the following string to the bottom of the /etc/cloud/cloud.cfg file to be sure that the new sudo vi /etc/cloud/cloud.cfg preserve_hostname: true # reboot hostname #verify that the hostname change persisted
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