Skip to content

Instantly share code, notes, and snippets.

@p3t3r67x0
Last active February 29, 2016 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p3t3r67x0/0b2862188535d9da32e6 to your computer and use it in GitHub Desktop.
Save p3t3r67x0/0b2862188535d9da32e6 to your computer and use it in GitHub Desktop.
Hosteurope virtual server hostname setting script

Script

#!/bin/bash
### BEGIN INIT INFO
# Provides: hostname.sh
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
# Short-Description: set hostname.
# Description: Host Europe virtual server hostname setting script.
### END INIT INFO
#
# chkconfig: 2345 7 93
# description: Host Europe virtual server hostname setting script
#
# /etc/init.d/hostname.sh
#
HOSTNAME=mail.example.com
[ -f /etc/hostname ] && echo $HOSTNAME > /etc/hostname
[ -f /etc/HOSTNAME ] && echo $HOSTNAME > /etc/HOSTNAME
[ -f /etc/mailname ] && echo $HOSTNAME > /etc/mailname
[ -f /etc/sysconfig/network ] && sed -i.hebackup s/HOSTNAME=".*"/HOSTNAME="$HOSTNAME"/ /etc/sysconfig/network
/bin/hostname $HOSTNAME
[ -f /etc/my.resolv.conf ] && cp /etc/my.resolv.conf /etc/resolv.conf
[ -f /etc/my.hosts ] && cp /etc/my.hosts /etc/hosts
exit 0

Make the script executable

chmod +x /etc/init.d/hostname.sh

Debian/Ubuntu

update-rc.d hostname.sh defaults 09

CentOS

chkconfig --add hostname.sh

Replace old hostname with the new one

vim /etc/hosts

Copy the new hostname to a save location

cp /etc/hosts /etc/my.hosts

Reboot your server

reboot -h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment