corrosiontears

Change Hostname Linux Shell Script

Nov 25th, 2015
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.50 KB | None | 0 0
  1. #!/bin/bash
  2. #Assign existing hostname to $hostn
  3. hostn=$(cat /etc/hostname)
  4.  
  5. #Display existing hostname
  6. echo "Existing hostname is $hostn"
  7.  
  8. #Ask for new hostname $newhost
  9. echo "Enter new hostname: "
  10. read newhost
  11.  
  12. #change hostname in /etc/hosts & /etc/hostname
  13. sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
  14. sudo sed -i "s/$hostn/$newhost/g" /etc/hostname
  15.  
  16. #display new hostname
  17. echo "Your new hostname is $newhost"
  18.  
  19. #Press a key to reboot
  20. read -s -n 1 -p "Press any key to reboot"
  21. sudo reboot
Add Comment
Please, Sign In to add comment