Cheat Sheet

Friday, January 6, 2017

Script to rename hostname and append to /etc/hosts

#!/bin/bash

# save this as anyfile.sh at webserver

# call curl -s http://somewhere.over.the.rainbow/anyfile.sh | sudo bash -s {hostname}

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

if [ $# -eq 0 ]
  then  echo "Please supply new hostname for this machine."
fi

echo "Hostname entered: " $1

hostnamectl set-hostname $1

echo 127.0.0.1 $1 >> /etc/hosts

cat >> /etc/hosts << EOF
# list of hosts
EOF

systemctl restart systemd-hostnamed

No comments:

Post a Comment