Created
June 28, 2012 15:11
-
-
Save fsmithred/3011914 to your computer and use it in GitHub Desktop.
script for changing user's name
This file contains hidden or 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
| #!/usr/bin/env bash | |
| # change-username (2012-06-26) | |
| # 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=" | |
| Usage: | |
| $0 <oldname> <newname> | |
| " | |
| # function to exit the script if there are errors | |
| check_exit () { | |
| [[ $? -eq 0 ]] || { echo "Exit due to error: $?" ; exit 1 ; } | |
| } | |
| # DON'T! Record errors in a logfile. (this hides the prompt for a new password) | |
| #error_log=/var/log/change-username_error.log | |
| #exec 2>"$error_log" | |
| # 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 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 " | |
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment