Created
October 26, 2016 13:47
-
-
Save jnikolak/686298f0dc06b58d1f521b83d7ab907b to your computer and use it in GitHub Desktop.
Auto generate Hostname on Rhel 7 and auto-add into /etc/hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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