Tesy

RPI change username

Sep 24th, 2014
228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #/bin/bash
  2. [ $UID = 0 ] || exit 1
  3. #if were here we are root, usually, but be root anyway
  4.  
  5. echo "you are about to regex things, this is generally considered heavy handed and you are advised to take caution. ctrl-c now to quit without doing anything, otherwise press enter to continue."
  6.  
  7. #pause the script
  8. line
  9.  
  10. #ok, the real work
  11. read -p 'Enter Username: ' NEWNAME
  12.  
  13. #rough test to ensure uniqueness
  14. grep $NEWNAME /etc/passwd
  15. [ $? = 1 ] && exit 1
  16.  
  17. #back the files up to the fat partition so you can copy them over to a flash stick or something and put them back
  18. cp -t /boot /etc/passwd /etc/group /etc/shadow /etc/gshadow
  19.  
  20. #do the change
  21. for i in {passwd, shadow, group, gshadow, sudoers}; do
  22. sed -i s/pi/$NEWNAME/g /etc/$i
  23. done
  24.  
  25. #freshen the backups
  26. for i in {passwd, group, shadow, gshadow}; do cp /etc/$i /etc/$i-; done
  27.  
  28. #I think were done, normally I do the changes by hand in nano and use regexes very sparingly
  29. #it is possible to back up these file first before doing this by copying them to another directory as root
  30. #so if your careful you can use this blunt instrument of a script
  31. #oh, and I added sudoers up their because otherwise your new username wouldn't have root access via the sudo command
RAW Paste Data