If you recently changed your domain name, and want your server’s hostname to match, you’ve come to the right post :) .

Immediate, temporary way

The immediate way to do this, is with one simple command:

sudo hostname example.com

where example.com is your new domain name. Now, exit the SSH session, and open a new one, and the hostname should have changed:

admin@example:~$

if you don’t use bash, then check with the hostname command:

example.com

Persist across reboots

If you want the new hostname to persist across reboots, then you need to edit a few more files. First, edit /etc/hostname with:

sudo nano /etc/hostname

and edit the first, and only line, to your new domain. Next, you need to edit the /etc/hosts file, with:

sudo nano /etc/hosts

and change all lines with your old domain to your new one, which should just be 127.0.0.1, and the IPv6 equivalent if you have that enabled.

A few more considerations

When changing your domain name, make sure you also update the following(if you use them):

Postfix

Edit the config file:

sudo nano /etc/postfix/main.cf

and change all occurrences of your old domain with the new one.

Apache

Edit the virtual hosts:

sudo nano /etc/apache2/sites-enabled/*

and change all occurrences of your old domain with the new one.

NGINX

Edit the virtual hosts:

sudo nano /etc/nginx/sites-enabled/*

and change all occurrences of your old domain with the new one.

Lighttpd

Edit the virtual hosts:

sudo nano /etc/lighttpd/lighttpd.conf

and change all occurrences of your old domain with the new one.

Lets Encrypt

If you use Let’s Encrypt, be sure to get a certificate for your new domain, and remember to change all apps that use the SSL certificates to the new path.

Short way

If you’re lazy, use the sed way:

sudo sed -i 's/olddomain/newdomain/g' /path/to/file/here

after making a backup of course :)