Find the current hostname

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

_$: root@mail1:/etc# grep -nri "" ./*
./hosts:2:	mail1
./lvm/backup/vg00:8:creation_host = ""	# Linux 3.2.0-38-generic #61-Ubuntu SMP Tue Feb 19 12:18:21 UTC 2013 x86_64
./postfix/ =
./postfix/ =,, localhost

Replace the old hostname with the new one in configuration files

Be careful whith the /etc/postfix/ 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

Generate aliases files

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

Check the hostname has been changed

_$: hostname
_$: hostname ; hostname

Restart all services that were using the old hostname

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