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
- Login as
ubuntu
(default id) - Run
sudo -i
to root - Run
screen
to launch screen session - 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
- Quit from console or ssh session
- After 30 seconds, login to the system using
newname
if succeeded. Othewise, useubuntu
login again. - Then sudo to root and run
screen -r
to check if error or quit screen session.