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.

Core networking

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. Related documentation.

Application specific

avahi

Avahi is used to publish (announce) local services. If you tweaked /etc/avahi/* you should run:

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):

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.

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/<your Volume Group>.

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):

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:

   1 #!/bin/bash
   2 #
   3 usage() {
   4    echo "usage : $0 <new hostname>"
   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.

   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 <new hostname> [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 [ "$BACKUP" == "YES" ]; 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 <new hostname>"
   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


See also