Skip to content

Instantly share code, notes, and snippets.

@fsmithred
Created July 1, 2012 14:54
Show Gist options
  • Select an option

  • Save fsmithred/3028674 to your computer and use it in GitHub Desktop.

Select an option

Save fsmithred/3028674 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
version="change-username (2012-07-01)"
# Change user name from user to something else, for a live-build system
# that was installed with refractainstaller.
# Run as root with oldname and newname as arguments.
# Copyright fsmithred@gmail.com 2011, 2012
# License: GPL-3
oldname="$1"
newname="$2"
help_text="
Version: $version
Usage:
$0 <oldname> <newname>
"
# function to exit the script if there are errors
check_exit () {
[[ $? -eq 0 ]] || { echo "Exit due to error: $?" ; exit 1 ; }
}
# check root
[[ $(id -un) = "root" ]] || { echo "You need to be root!" ; exit 1 ; }
# check that there are two arguments
if [[ $# -ne 2 ]] ; then
echo "$help_text"
exit 1
fi
# check that oldname exists
if ! $(grep -q $oldname /etc/passwd) ; then
echo " $oldname does not exist."
exit 1
fi
# Test if xinit or a display manager is running, and save the information
# for later use.
if ps -C xinit; then
dm_status="no"
elif
ps -C gdm; then
dm_status="yes"
dm="gdm"
elif
ps -C gdm3; then
dm_status="yes"
dm="gdm3"
elif
ps -C kdm; then
dm_status="yes"
dm="kdm"
elif
ps -C xdm; then
dm_status="yes"
dm="xdm"
elif
ps -C lightdm; then
dm_status="yes"
dm="lightdm"
fi
# Make sure the old user is not logged in - kill that user's processes.
while true; do
echo "
$oldname must not be logged in if you want to proceed.
If you answer \"yes\", this script will kill any processes owned
by $oldname. Say \"no\" if you want to exit the script and go back
to manually close any running programs.
Kill ${oldname}'s processes now? (y or n)
"
read ans
case $ans in
[Yy]*) for i in $(pgrep -u $oldname) ; do
kill $i
done
break ;;
[Nn]*) exit 0
esac
done
echo " Changing user name and group...
"
sleep 3
# Change user name and group
usermod -l $newname $oldname ; check_exit
groupmod -n $newname $oldname ; check_exit
usermod -d /home/$newname -m $newname ; check_exit
# Show that it was done
echo "
Checking that the name has been changed...
"
sleep 2
if ! id $oldname >/dev/null 2>&1 ; then
echo "
$oldname has been deleted.
"
else
echo " Something is wrong. $oldname still exists"
exit 1
fi
sleep 2
echo "
Checking $newname's group memberships:
"
if ! id $newname ; then
exit 1
fi
sleep 2
# Ask to change newuser's password.
while true; do
echo "
Do you want to give $newname a new password?
(yes or no)
"
read ans
case $ans in
[Yy]*) passwd $newname ; break ;;
[Nn]*) break ;;
esac
done
echo "
This script will attempt to replace every instance of
/home/$oldname with /home/$newname in your user's config files.
You may need to edit the properties of desktop icons for
terminal, file manager, browser and maybe text editor.
(Just reset the working directory.)
"
read -p " Press the ENTER key when you're ready to proceed."
for i in $(grep -r "/home/$oldname" /home/$newname/.config | awk -F":" ' { print $1 }'); do
sed -i "s/\/home\/$oldname/\/home\/$newname/g" "$i"
done
#for i in $(grep -r "/home/$oldname" /home/$newname/.local | awk -F":" '{ print $1 }'); do
# sed -i "s/\/home\/$oldname/\/home\/$newname/g" "$i"
#done
if [[ -d /home/"$oldname"/.kde/share ]] ; then
sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/apps/kfileplaces/bookmarks.xml
sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/config/startupconfigfiles
sed -i "s/\/home\/$oldname/\/home\/$newname/g" /home/$newname/.kde/share/config/ksmserverrc
fi
# Change user's real name
live_user=$(awk -v pattern="$newname" -F: '$0 ~ pattern { print $5 }' /etc/passwd)
echo -n "
The user's real name is currently $live_user.
Enter the real name you want to use (without the trailing commas.)
"
read real_name
sed -i~ "s/$live_user/$real_name,,,/" /etc/passwd
:<<HOLD
# Edit /etc/gdm3/daemon.conf
while true; do
echo "
Edit /etc/gdm3/daemon.conf to disable graphical auto-login?
If you don't do this, gdm3 will hang. If that happens, you can
reboot in recovery mode and issue the command:
update-rc.d -f gdm3 remove
log out as root and log in as your user. Start the desktop with:
startx
Edit daemon.conf? (yes or no)
"
read ans
case $ans in
[Yy]*) nano /etc/gdm3/daemon.conf ; break ;;
[Nn]*) break ;;
esac
done
HOLD
sleep 2
# Edit /etc/sudoers
while true; do
echo "
You need to either comment out the line in /etc/sudoers that gives
\"user\" absolute power, or you need to replace \"user\"
with the new user name.
Edit /etc/sudoers? (yes or no)
"
read ans
case $ans in
[Yy]*) visudo ; break ;;
[Nn]*) break ;;
esac
done
sleep 2
# Disable sudo-mode for gksu
while true; do
echo "
Do you want to disable sudo mode for gksu? If you answer \"yes\",
then gksu will use the root password.
(Note: If you commented out the last line in /etc/sudoers in the
last step, answer \"yes\" here. However, if you changed the user
name, then answer \"no\".)
(yes or no)
"
read ans
case $ans in
[Yy]*) sed -i~ '/sudo-mode/s/true/false/' /home/"$newname"/.gconf/apps/gksu/%gconf.xml ; break ;;
[Nn]*) break ;;
esac
done
echo "
Done!
You can now log in as the new user.
If you'd like, I'll attempt to restart the xserver.
"
sleep 2
if [[ $dm_status = yes ]]; then
while true ; do
echo "
Would you like to restart the xserver?
(yes or no)
"
read ans
case $ans in
[Yy]*) /etc/init.d/"$dm" restart & break ;;
[Nn]*) break ;;
esac
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment