Change the hostname
This is how to change the hostname as well as the domainname of a machine running Debian or a Debian-like OS.
Warning
Changing the hostname on a running Linux system will break X on that machine—that means you will probably get in trouble while running in a graphical interface. It won't cause any trouble in a virtual terminal (Ctrl+Alt+F1) or remotely via SSH.
Temporary, immediate change
gert@oldhostname:~# hostname mynewhostname
Now close your shell (or log out), open a new one and you'll see the hostname is changed.
You can do the same thing for any of the domain names, just exchange the hostname
command with any of the ones below (taken from the manpage of hostname
):
domainname - show or set the system's NIS/YP domain name
ypdomainname - show or set the system's NIS/YP domain name
nisdomainname - show or set the system's NIS/YP domain name
dnsdomainname - show the system's DNS domain name
This change will not survive a reboot. See below for a permanent way to change it.
Permanent change
Edit the file containing your hostname, for example, just overwrite it with the new one:
# echo "mynewhostname" > /etc/hostname
Then, change the entries accordingly in /etc/hosts
:
127.0.1.1 oldhostname
Usually only the line starting with 127.0.1.1
has to be modified.
Now, run the hostname
init-script to finish everything up:
# /etc/init.d/hostname.sh start
for Debian or
# service hostname start
for Ubuntu.
Complaints after the last command about the service not running can be discarded; it's not a real service, rather it's just a script run at boot time.
Also, don't forget to change the settings for other services like your mail server, usually in /etc/mailname
.
Check
To verify the system has picked up the new hostname, hostname
can show you what it sees.
$ hostname # just print the hostname
$ hostname -f # print the fully qualified domain name