Find the current hostname

Go to the /etc directory and look for the current hostname. Let us supose that the hostname is mail1.domain.com and it corresponds to a mail server.

_$: root@mail1:/etc# grep -nri "mail1.domain.com" ./*
./awstats/awstats.mail.conf:4:SiteDomain="mail1.domain.com"
./hostname:1:mail1.domain.com
./hosts:2:127.0.1.1	mail1.domain.com	mail1
./lvm/backup/vg00:8:creation_host = "mail1.domain.com"	# Linux mail1.domain.com 3.2.0-38-generic #61-Ubuntu SMP Tue Feb 19 12:18:21 UTC 2013 x86_64
./mailname:1:mail1.domain.com
./postfix/main.cf:21:myhostname = mail1.domain.com
./postfix/main.cf:25:mydestination = mail1.domain.com, localhost.domain.com, localhost
./zabbix/zabbix_agentd.conf:92:Hostname=mail1.domain.com

Replace the old hostname with the new one in configuration files

Be careful whith the /etc/postfix/main.cf file. It has a field called mydestination that you should check.

There are several ways to change all occurrences of one string for another. Some examples are shown:

# sed:
# Changes all files in this directory
_$: sed -i 's/old-word/new-word/g' *.txt

# grep + sed:
# Changes all files in this directory and recursively
_$: grep -lr "old-world" ./* | while read f; do sed -i 's/old-world/new-world/g' $f; done

# vi:
# Changes the file you are editing
:%s/old-word/new-word/g

Generate aliases files

_$: newaliases
_$: postmap /etc/postfix/relay_recipients

Check the hostname has been changed

_$: hostname
mail1
_$: hostname mail1.newdomain.com ; hostname
mail1.newdomain.com

Restart all services that were using the old hostname

_$: service zabbix-agent restart
_$: service postfix restart