Stuff that has not yet gone into the official build.
Post a reply

change-username.sh

Sat May 05, 2012 1:03 pm

I've added code to the change-username script to kill the display manager if gdm3, kdm or xdm is running. I can't test that part. I did test it with gdm (in refracta-606) and there was a problem with replacing "/home/$oldname" with "/home/$newname" in the user's config files. Some got changed, some didn't, and I had to manually reset the working directory for several app launchers on the panel. The code for that is around lines 140-150.

Also note that the command-line arguments have been switched from
change-username <newname> <oldname>
to
change-username <oldname> <newname>


Code:
#!/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

Re: change-username.sh

Sat May 05, 2012 7:54 pm

It has been a while that i used "case", and it has been a while that i used bash at all.
That said:
I ran into an error message at line 78 (plus or minus).
 
Code:
  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

I removed the "breaks" (without looking at the docu about "case"), and that solved the problem:
Code:
        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


It might well be that something went south with "copy and paste".
As you could run the script, i guess you already corrected it, or it is a copy/past error, like said.

And now i must eat. So no more info for now.

Re: change-username.sh

Sun May 06, 2012 9:19 am

nope, that let's the script hang forever.
No idea then.

Do me a favor and upload it to a place where i can download it as raw text. Using phpbb with code is a pain in the ***.

Re: change-username.sh

Sun May 06, 2012 10:25 am

http://paste.debian.net/167521/


Edit: Got rid of that error for line 78. The previous version of the script had a break after the pkill. It did not have the if/then that follows. The break has to occur after the conditional statement, like this:
Code:
    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

Re: change-username.sh

Sun May 06, 2012 11:14 am

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/

.

Re: change-username.sh

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/

.

I took that script, ran it, and if i now run
grep -r user /home/newname/.local
grep -r user /home/newname/.config
I don't get any matches:
Code:
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#


I started a couple of apps from the menu, and all seem to work.

Did it the other way around (changed the new name back to the old name, refracta default, which is user, and it worked again, it seems).

-----------------------------------------------------
I am not sure:
It might (!) be i had problems while i was also logged in another tty
(but i think i was logged in as user, and "su" to root).
Let me test that later, and focus on it.

Re: change-username.sh

Sun May 06, 2012 3:03 pm

Logged into console as user, then su to root.
Run the script.
It asks if I want to stop user's processes, say yes.
Script continues, then exits on error:
usermod: user user is still logged in


Test 2:
Console login on tty1 as root. Stop gdm.
Console login on tty2 as user. Run startx.
Drop to tty1 and run the script.
Script exits on xinit seg fault.
Run the script a second time, and it exits on the same usermod error.

To kill the user's login on tty2, this works:
kill -9 $(ps -u $oldname -opid=)

But I still need a way to kill the xserver without causing a segfault.

Re: change-username.sh

Sun May 06, 2012 8:48 pm

Here's the final version (I think). killall5 saved the day.

Code:
#!/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
Post a reply