Skip to content

Instantly share code, notes, and snippets.

@jnikolak
Created October 26, 2016 13:47
Show Gist options
  • Save jnikolak/686298f0dc06b58d1f521b83d7ab907b to your computer and use it in GitHub Desktop.
Save jnikolak/686298f0dc06b58d1f521b83d7ab907b to your computer and use it in GitHub Desktop.
Auto generate Hostname on Rhel 7 and auto-add into /etc/hosts
#!/bin/bash
#### This script will create hostname for rhel 7 and add dns entries into /etc/hosts.
#### There is also a way to generate a random hostname, but the code is commented out.
#### Future version, will try to make it work for when user doesn't have any entry.
dom='.china.com'
old=$(hostname -f)
#This is if you want random letters.
#new2=$(tr -dc 'a-z0-9' < /dev/urandom | head -c12)
#### This prompts user for hostname and enters it in
read -p "Type in your hostname and then press enter : " yourhostname
new=$yourhostname$dom
hostnamectl set-hostname "$new"
### Lets get a read out of current ip.
oldip=$(sed '3!d' /etc/hosts | awk '{print $1}')
newip=$(ip -o -4 addr show dev eth0 | sed 's/.* inet \([^/]*\).*/\1/')
sed -i "s/$old/$new/g" /etc/hosts
sed -i "s/$oldip/$newip/g" /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment