Sat May 05, 2012 1:03 pm
#!/usr/bin/env bash
# change-username (2012-05-03)
# Change user name from user to something else, for a live-build system
# that was installed with refractainstaller.
# Run as root with newname and oldname as arguments.
# Copyright [email protected] 2011
# License: GPL-3
oldname="$1"
newname="$2"
help_text="
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
# Make sure that the user isn't logged in.
# Test if xinit or a display manager is running; if so, stop it.
if ps -C xinit; then
dm_status="no"
# kill $(ps -C xinit -opid=)
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"
fi
while true; do
echo "
$oldname must not be logged in if you want to proceed.
If you answer \"yes\", this script will stop the display manager and
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]*) pkill -u "$oldname"
if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" stop ; break ;;
elif [[ $dm_status = no ]]; then
kill $(ps -C xinit -opid=) ; break ;;
fi
[Nn]*) exit 0
esac
done
echo " Changing user name and group...
"
sleep 4
# 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 3
echo "
Checking $newname's group memberships:
"
if ! id $newname ; then
exit 1
fi
sleep 3
# 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"
check_exit
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
# 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 "
If you commented out the last line in /etc/sudoers in the last
step, answer \"yes\". However, if you changed the user name, then
answer \"no\".
Another way to ask this question - Do you want to disable sudo mode
for gksu? If you answer \"yes\", then gksu will use root password.
"
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.
I'll attempt to restart the xserver.
"
sleep 2
if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" start
fi
exit 0
Sat May 05, 2012 7:54 pm
read ans
case $ans in
[Yy]*) pkill -u "$oldname"
if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" stop ; break ;;
elif [[ $dm_status = no ]]; then
kill $(ps -C xinit -opid=) ; break ;;
fi
[Nn]*) exit 0
esac
done
read ans
case $ans in
[Yy]*) pkill -u "$oldname";
if [[ $dm_status = yes ]]
then
/etc/init.d/$dm stop
elif [[ $dm_status = no ]]
then
kill $(ps -C xinit -opid=)
fi ;;
[Nn]*) exit 0
esac
done
Sun May 06, 2012 9:19 am
Sun May 06, 2012 10:25 am
read ans
case $ans in
[Yy]*) pkill -u "$oldname"
if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" stop
elif [[ $dm_status = no ]]; then
kill $(ps -C xinit -opid=)
fi
break ;;
[Nn]*) exit 0
esac
done
Sun May 06, 2012 11:14 am
Sun May 06, 2012 11:38 am
fsmithred wrote:Problem I was having was that not all instances of "/home/$oldname" were being changed in the new user's config files. There were some errors from the sed command, and it was causing the script to exit.
I removed the "check_exit" after the sed command, and now sed spits out errors for some files, but it doesn't exit the script. Seems to be working now.
Here's what it looks like:
http://paste.debian.net/167526/
.
root# grep -r user /home/molloy/.local
root# grep -r user /home/molloy/.config
/home/molloy/.config/user-dirs.dirs:# This file is written by xdg-user-dirs-update
grep: /home/molloy/.config/deadbeef/socket: No such device or address
/home/molloy/.config/uzbl/config:set useragent = Uzbl (Webkit @{WEBKIT_MAJOR}.@{WEBKIT_MINOR}.@{WEBKIT_MICRO}) (@(+uname -s)@ @(+uname -m)@ [@ARCH_UZBL]) (Commit @COMMIT)
grep: /home/molloy/.config/geany/geany_socket_refracta__0: No such file or directory
root#
Sun May 06, 2012 3:03 pm
Sun May 06, 2012 8:48 pm
#!/usr/bin/env bash
# change-username (2012-05-06)
# Change user name from user to something else, for a live-build system
# that was installed with refractainstaller.
# Run as root with newname and oldname as arguments.
# Copyright [email protected] 2011
# License: GPL-3
oldname="$1"
newname="$2"
help_text="
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
# Make sure that the user isn't logged in.
# Test if xinit or a display manager is running; if so, stop it.
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"
fi
while true; do
echo "
$oldname must not be logged in if you want to proceed.
If you answer \"yes\", this script will stop the display manager and
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]*) if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" stop
/sbin/killall5
else
/sbin/killall5
fi
break ;;
[Nn]*) exit 0
esac
done
echo " Changing user name and group...
"
sleep 4
# 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 3
echo "
Checking $newname's group memberships:
"
if ! id $newname ; then
exit 1
fi
sleep 3
# 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
# 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 "
If you commented out the last line in /etc/sudoers in the last
step, answer \"yes\". However, if you changed the user name, then
answer \"no\".
Another way to ask this question - Do you want to disable sudo mode
for gksu? If you answer \"yes\", then gksu will use root password.
"
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.
I'll attempt to restart the xserver.
"
sleep 2
if [[ $dm_status = yes ]]; then
/etc/init.d/"$dm" start
fi
exit 0