Solved: ChangeHostname
HowToChangeHostname
Translation(s): English - Italiano - Русский
Rename a computer
This page explains how to change a system's hostname (i.e. rename a computer/server)
Executive summary: hostname(1) isn't enough.
Contents
Rename a computer
Core networking
systemd
Application specific
avahi
CUPS
ejabberd
Exim
Mailname
Message Of The Day
OpenSSH server
Self-signed SSL certificate
ssmtp
sysklogd
fail2ban
lvm
md
Idea of script to help you on
Intrusive script
Improved intrusive script
Not-so intrusive script
See also
Core networking
Update /etc/hostname
Update /etc/hosts, so local address(es) resolves with the new system name.
Reload the network configuration. You have two options:
Reload configuration files
<!> This will temporarily disconnect your system from the network (ssh usually resists short disconnection)
<!> This might definitively disconnect your system from the network because networking might not restore connections; please reboot, which is not lazy, but ensures that your setup is really correct
invoke-rc.d hostname.sh start
invoke-rc.d networking force-reload
invoke-rc.d network-manager force-reload
ToDo: is it useful to reload network-manager?
or the lazy way: Restart the system.
systemd
Versions of Debian with systemd installed can use "hostnamectl set-hostname mymachine". This tool depends on dbus so be sure to install that first.
Application specific
avahi
Avahi is used to publish (announce) local services. If you tweaked /etc/avahi/* you should run:
invoke-rc.d avahi-daemon force-reload
CUPS
CUPS is the Common Unix Printing System.
Adjust the /etc/printcap file.
You'll want to change the old hostname in any lines like this, hostname in bold: Kyocera_TASKalfa_3050ci|Kyocera TASKalfa 3050ci:rm=debian:rp=Kyocera_TASKalfa_3050ci: You'll need to restart the service for changes to take effect.
ejabberd
Refer to the Change Computer Hostname section of the ejabberd documentation website.
Exim
Reconfigure Exim (this adjusts /etc/exim4/update-exim4.conf.conf and /etc/mailname):
dpkg-reconfigure exim4-config
Or adjust manually /etc/exim4/update-exim4.conf.conf (hostname=). You need to restart the service.
Mailname
Reconfigure Exim (see above) or adjust manually /etc/mailname. You don't need to restart the service.
Message Of The Day
Adjust /etc/motd. You don't need to restart a service.
OpenSSH server
Adjust /etc/ssh/ssh_host_rsa_key.pub, /etc/ssh/ssh_host_dsa_key.pub, /etc/ssh/ssh_host_ed25519_key.pub and /etc/ssh/ssh_host_ecdsa_key.pub (root@hostname at the end). You need to restart the service.
Self-signed SSL certificate
Recreate the self-signed certificate created by the ssl-cert package using the hostname currently configured on your computer.
make-ssl-cert generate-default-snakeoil --force-overwrite
You need to restart the services using it, e.g. apache2.
ssmtp
Ssmtp is a light weight, send-only SMTP server.
Adjust /etc/ssmtp/ssmtp.conf (hostname=, etc.). You don't need to restart the service.
sysklogd
In order to make new hostname appear on syslog you should restart sysklogd service
fail2ban
fail2ban scan logs to detect connection failures and can ban IP on too many failures.
The hostname may be in the sender mail address in /etc/fail2ban/jail.local. You need to reload the service.
lvm
Logical Volume Manager (LVM) is a device mapper target that provides logical volume management.
Hostname appears in the backup configuration file /etc/lvm/backup/.
md
On a system that is using md on the root device boot will fail once update-initramfs -u is run if the hostname in /etc/mdadm/mdadm.conf does not match the hostname in the superblocks of the array members.
To change the hostname in the superblocks at the (initramfs) _ prompt (or from rescue media):
mdadm -A /dev/md/0 --update=name --name=new_hostname:0 /dev/sd[ab]1 # use YOUR devices
The md device must be down to be renamed.
Best reference: https://lists.debian.org/debian-arm/2016/02/msg00072.html
Idea of script to help you on
Intrusive script
Please understand that this script is dangerous. You should check if all the files listed by grep -rl "$old" /etc must really be modified before launching this script: Toggle line numbers
1 #!/bin/bash 2 # 3 usage() { 4 echo "usage : $0 " 5 exit 1 6 } 7 8 [ "1" ] || usage 9 10 old=(hostname) 11 new=1 12 13 grep "old" /etc/ -rl 2>/dev/null | 14 while read file 15 do 16 sed "s:old:new:g" "file" > "file.tmp" 17 mv -f "file.tmp" "file" 18 done
Take care you'd better do a grep before...
Improved intrusive script
This script will do the same stuff like the one above but it will ask you with a shell dialog which files you want modify before editing them. Toggle line numbers
1 #!/bin/bash
2 #
3
4 # Fetching new and old hostname
5 OLD_HOSTNAME=$(hostname)
6 NEW_HOSTNAME=1 7 8 WHIPTAIL_TITLE="Changing hostname" 9 WHIPTAIL_BACKTITLE="ShrimpDev <dev@csoellinger.at>" 10 WHIPTAIL_TEXT="Below you will find a checklist with all files where we found your old hostname \"{OLD_HOSTNAME}" inside /etc\n\nPlease check which files we should update to your new hostname "${NEW_HOSTNAME}""
11
12 not_root() {
13 echo "ERROR: You have to be root to execute this script"
14 exit 1
15 }
16
17 usage() {
18 echo "usage : $0 [Optional:--backup/Set backup mode on] [Optional:--debug/Set debug mode on]"
19 exit 1
20 }
21
22 # Check if user is root
23 [ $EUID != 0 ] && not_root
24
25 # Check if we have at least one parameter
26 [ "$1" ] || usage
27
28 [ "$1" == "--backup" ] && usage
29 [ "$1" == "--debug" ] && usage
30
31 DEBUG="NO"
32 BACKUP="NO"
33
34 if [ "$2" == "--debug" ] || [ "$3" == "--debug" ]; then
35 DEBUG="YES"
36 fi
37
38 if [ "$2" == "--backup" ] || [ "3" == "--backup" ]; then 39 BACKUP="YES" 40 fi 41 42 [ "DEBUG" == "YES" ] && echo "DEBUG (NO FILES WILL BE CHANGED)"
43 [ "BACKUP" == "YES" ] && echo "BACKUP MODE ON" 44 45 # Grep all possible targets 46 TARGETS=((grep "OLD_HOSTNAME" /etc/ -rl)) 47 let TARGETSLENGTH={#TARGETS[@]}
48
49 CHECKLIST=()
50 for ((i=0; i<{#TARGETS[@]}; i++)); do 51 CHECKLIST+=("{TARGETS[i]}" "" "ON") 52 done 53 54 CHECKLIST_RESULT=(whiptail --clear --fb --title "WHIPTAIL_TITLE" --backtitle "WHIPTAIL_BACKTITLE" --checklist "$WHIPTAIL_TEXT" 30 80 TARGETSLENGTH "{CHECKLIST[@]}" 3>&2 2>&1 1>&3)
55 CHECKLIST_RESULT=${CHECKLIST_RESULT//"/}
56 CHECKLIST_RESULT_ARR=(echo $CHECKLIST_RESULT | tr ' ' '\n'
)
57
58 clear
59
60 for CHECKLIST_ITEM in "{CHECKLIST_RESULT_ARR[@]}"; do 61 echo "sed \"s:{OLD_HOSTNAME}:{NEW_HOSTNAME}:g\" \"{CHECKLIST_ITEM}" > "{CHECKLIST_ITEM}.tmp\"" 62 [ "DEBUG" == "NO" ] && sed "s:OLD_HOSTNAME:NEW_HOSTNAME:g" "CHECKLIST_ITEM" > "CHECKLIST_ITEM.tmp"
63
64 if [ "2" == "--backup" ]; then 65 echo "cp \"{CHECKLIST_ITEM}" "{CHECKLIST_ITEM}.BCK\"" 66 [ "DEBUG" == "NO" ] && cp "CHECKLIST_ITEM" "CHECKLIST_ITEM.BCK"
67 fi
68
69 echo "mv -f "{CHECKLIST_ITEM}.tmp\" \"{CHECKLIST_ITEM}""
70 [ "DEBUG" == "NO" ] && mv -f "CHECKLIST_ITEM.tmp" "$CHECKLIST_ITEM"
71
72 echo ""
73 done
74
75 exit 0
Not-so intrusive script
#!/bin/bash
usage() { echo "usage : $0 " exit 1 }
[ "$1" ] || usage
old=$(hostname) new=$1
for file in
/etc/exim4/update-exim4.conf.conf
/etc/printcap
/etc/hostname
/etc/hosts
/etc/ssh/ssh_host_rsa_key.pub
/etc/ssh/ssh_host_dsa_key.pub
/etc/motd
/etc/ssmtp/ssmtp.conf
do
[ -f file ] && sed -i.old -e "s:old:$new:g" $file
done