Last active
November 30, 2016 23:25
-
-
Save fade2gray/09735265f122c33207c5 to your computer and use it in GitHub Desktop.
Raspbian - Change Default User Name
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 | |
############################################################################## | |
# Works for me with clean and fully updated Rasbian Jessie on Rasberry Pi 2b # | |
# ONLY USE THIS SCRIPT If YOU KNOW WHAT YOU ARE DOING - RUN AT YOUR OWN RISK # | |
# MUST BE RUN AS ROOT FROM A COMMAND LINE LOGIN - DO NOT RUN AS SUDO USER # | |
############################################################################## | |
echo "Change username?" | |
select yn in "Yes" "No"; do | |
case $yn in | |
Yes ) | |
echo -n "Enter old user name: "; | |
read oldname; | |
echo -n "Enter new user name: "; | |
read newname; | |
# Test if old user name exists | |
test1=$(cut -d: -f1 /etc/passwd|grep ^"$oldname") | |
if [ -z "$test1" ] ; then | |
echo -e "The old user name '$oldname' does not exist.\nDid you enter it correctly?" | |
exit | |
fi | |
# Test if old user name has any running processes | |
test2=$(ps aux|grep ^"$oldname") | |
if [ -n "$test2" ] ; then | |
echo -e "The old user name '$oldname' still has running processes.\nStop the processes before running again.\n" | |
ps aux|grep ^"$oldname" | |
exit | |
fi | |
#Test if new user name already exits | |
test3=$(cut -d: -f1 /etc/passwd|grep ^"$newname") | |
if [ -n "$test3" ] ; then | |
echo -e "The new user name '$newname' already exists.\nDid you enter it correctly?" | |
exit | |
fi | |
# Modify username and home directory | |
usermod -l $newname -d /home/$newname -m $oldname; | |
# Modify groupname | |
groupmod -n $newname $oldname; | |
# Modify /usr/bin/raspi-config | |
filename=/usr/bin/raspi-config; | |
sed $filename -i -e "s/passwd "$oldname"/passwd "$newname"/"; | |
sed $filename -i -e "s/login -f "$oldname" tty1/login -f "$newname" tty1/"; | |
sed $filename -i -e "s/autologin-user="$oldname"/autologin-user="$newname"/"; | |
sed $filename -i -e "s/id -u "$oldname"/id -u "$newname"/"; | |
sed $filename -i -e "s/default user ("$oldname")/default user ("$newname")/"; | |
# Modify /etc/lightdm/lightdm.conf | |
sed /etc/lightdm/lightdm.conf -i -e "s/autologin-user="$oldname"/autologin-user="$newname"/"; | |
# Modify /etc/sudoers | |
sed /etc/sudoers -i -e "s/"$oldname" ALL=(ALL) NOPASSWD: ALL/"$newname" ALL=(ALL) NOPASSWD: ALL/"; | |
# Modify /etc/systemd/system/autologin@.service (credit to AndyD on Raspberry Pi forums) | |
sed /etc/systemd/system/autologin@.service -i -e "s/autologin "$oldname"/autologin "$newname"/"; | |
break;; | |
No ) | |
break;; | |
esac | |
done | |
echo "Script completed successfully." | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment