Change user name in ubuntu

Change user name in ubuntu

To change default user name ubuntu created in Raspberry Pi image, without adding a temporary user or enable root id, can be done using background job.

Commands

Change user name and home directory

usermod -l newUsername -d /home/newUsername oldUsername

Change group name

groupmod -n <newgroup> <oldgroup>

Job

The actual execution should be triggered as background job. I used screen command as below

  1. Login as ubuntu (default id)
  2. Run sudo -i to root
  3. Run screen to launch screen session
  4. Run following command, and press Ctrl-A d detach screen session immediately, without command executed.
newname=<new_user_name>
sleep 30; ps -ef | grep ^ubuntu | awk '{print $2}' | xargs kill -9 ; usermod -l $newname -d /home/$newname ubuntu; groupmod -n $newname ubuntu; mv /home/ubuntu /home/$newname
  1. Quit from console or ssh session
  2. After 30 seconds, login to the system using newname if succeeded. Othewise, use ubuntu login again.
  3. Then sudo to root and run screen -r to check if error or quit screen session.

References

How do I change my username?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


The reCAPTCHA verification period has expired. Please reload the page.