2 # Filename: grml-hostname
3 # Purpose: simple frontend to set hostname
4 # Authors: (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports: see http://grml.org/bugs/
6 # License: This file is licensed under the GPL v2.
7 ################################################################################
9 . /etc/grml/script-functions
13 [ -n "$SUDO_USER" ] && RUNASUSER=$SUDO_USER || RUNASUSER=grml
16 OLDHOSTNAME="$(hostname)"
18 case "$1" in -h|--help) echo "Usage: $0 [hostname]">&2; exit 1 ;; esac
24 if [ -x /usr/bin/random-hostname ] ; then
25 NEW_HOSTNAME="$(/usr/bin/random-hostname)"
27 NEW_HOSTNAME="$(hostname)"
31 if [ -z "$NONINTERACTIVE" ] ; then
32 NEW_HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \
33 --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\
34 \n\nTip: press 'Propose hostname' for another proposal" 14 70 $NEW_HOSTNAME)"
42 echo "$NEW_HOSTNAME" > /etc/hostname
43 echo "$NEW_HOSTNAME" > /etc/mailname
44 sed -i "s/^127.0.0.1[ \t]*$OLDHOSTNAME[ \t]*localhost/127.0.0.1 $NEW_HOSTNAME localhost/" /etc/hosts
45 sed -i "s/^::1[ \t]*ip6-localhost ip6-loopback[ \t]*$OLDHOSTNAME/::1 ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts
47 if [ -r /etc/postfix/main.cf ] ; then
48 sed -i "s/^mydestination = .*/mydestination = $NEW_HOSTNAME, localhost, localhost.localdomain/" /etc/postfix/main.cf && \
49 sed -i "s/^myhostname = .*/myhostname = $NEW_HOSTNAME/" /etc/postfix/main.cf && POSTFIX='
50 Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. Do not forget to restart postfix if necessary.'
52 if [ -n "$DISPLAY" ] ; then
53 if sudo -u $RUNASUSER xauth add $(xauth -n list | grep "${OLDHOSTNAME}/unix:0" | sed "s|${OLDHOSTNAME}/unix:0|${NEW_HOSTNAME}/unix:0|") ; then
54 sudo -u $RUNASUSER xauth remove "${OLDHOSTNAME}/unix:0"
58 /etc/init.d/hostname.sh
60 if [ -z "$NONINTERACTIVE" ] ; then
61 dialog --stdout --title "${PN}" --msgbox "Setting hostname to $NEW_HOSTNAME was successful.$POSTFIX" 0 0
63 echo "Setting hostname to $NEW_HOSTNAME: done"
68 echo "Cancel pressed."
77 ## END OF FILE #################################################################