For system administrator frequently cloning Debian GNU/Linux virtual hosts, this guide ease the modification of the system hostname.

This howto is tested on:

  • Debian 10.0 Buster

Settings

Provide the new (fully qualified) domain name for the system:

newFQDN="myhost.domain.lan"

Configuration

Detect if sudo is available (“command” is used if not):

cmdProxy='command'
command type -f 'sudo' &>'/dev/null' && cmdProxy='sudo'

Compute the new hostname from the given domain name:

newHostname="${newFQDN%%.*}"

Get the current hostname and FQDN:

currentFQDN="$(command hostname --fqdn)"
currentHostname="$(command hostname)"

Update the system configuration files:

while read confFile; do
  if [ -e "${confFile}" ]; then
    ${cmdProxy} sed -i -e "s/${currentFQDN}/${newFQDN}/g" "${confFile}"
    ${cmdProxy} sed -i -e "s/${currentHostname}/${newHostname}/g" "${confFile}"
  fi
done \
<<< "/etc/hostname
/etc/motd
/etc/hosts
/etc/mailname
/eetc/exim4/update-exim4.conf.conf"

Apply the new domain name:

${cmdProxy} hostname --file '/etc/hostname'

Update Exim 4 configuration:

${cmdProxy} update-exim4.conf \
  && ${cmdProxy} systemctl restart

Generate a new encryption key for the SSH server:

${cmdProxy} rm "/etc/ssh/ssh_host_rsa_key" \
    "/etc/ssh/ssh_host_rsa_key.pub" \
    "/etc/ssh/ssh_host_dsa_key" \
    "/etc/ssh/ssh_host_dsa_key.pub"
${cmdProxy} ssh-keygen -q -t rsa -N "" -C "root@${newHostname}" -f "/etc/ssh/ssh_host_rsa_key"
${cmdProxy} ssh-keygen -q -t dsa -N "" -C "root@${newHostname}" -f "/etc/ssh/ssh_host_dsa_key"

Restart the SSH server:

${cmdProxy} systemctl 'restart' 'ssh'
Categories: Setup

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.