Some time ago, when I installed Ubuntu, I chose a rather stupid username for my account that I do not want to use anymore.
How do I change this (including the name of my home directory, and the name in the terminal) without losing settings for applications?
How do I keep permissions and my keys for various authentification (e.g. email, SSH, GPG and more)?
What settings could possibly get lost if I changed my username?
Unix-like operating systems decouple the user name from the user identity, so you may safely change the name without affecting the ID. All permissions, files, etc are tied to your identity (uid), not your username.
To manage every aspect of the user database, you use the
usermod
tool.To change username and user's groupname (it is probably best to do this without being logged in):
This however, doesn't rename the home folder.
To change home-folder, use
after you changed the username.
For instance, you could logout, drop to a console (Ctrl+Alt+F1), and
sudo su -
to become true root (as opposed tosudo -s
, where $HOME is still /home/yourname.) Maybe you also have to kill some still running processes from this user first. To do so, enterps -u username
, look for the matching PID and kill them bykill PID-number
.Update: as arrange mentioned, some files may reference your old home directory. You can either keep a symlink for backward compatibility, e g
ln -s /home/newname /home/oldname
or you can change the file contents withsed -i.bak 's/*oldname*/*newname*/g' *list of files*
It creates a backup for each file with a .bak extension.Some additional information for not so experienced users like me:
As I only have ONE user account (administrator), it would not let me change the username ("you are already logged in" was the response in TTY1 (Ctrl+Alt+F1). To get around this:
Login with your old credentials and add a new user, e.g. "temporary" in TTY1:
set the password. 2. Allow the temporary user to run sudo by adding the user to sudo group:
Log out with the command
exit
.Return to tty1: Login with the 'temporary' user account and password. Change your username and folder as mentioned above.
exit
(until you get the login prompt)Go back to TTY7 (Ctrl+Alt+F7) to login on the GUI/normal desktop screen and see if this works.
Delete temporary user and folder:
To put it all together:
Log out of your session or restart your computer to get back to the start screen.
At the start screen go to a console mode tty. Press Ctrl+Alt+F1 (on some Ubuntu revisions this may be Ctrl+Alt+F2 instead)
Log in using your username and password.
Set a password for the "root" account.
Log out.
Log in using the "root" account and the password you have previously set.
Change the username and the home folder to the new name that you want.
"newfullname" is the fifth column in /etc/passwd
Change the group name to the new name that you want.
Lock the "root" account.
If you were using ecryptfs (encrypted home directory). Mount your encrypted directory using
ecryptfs-recover-private
and edit<mountpoint>/.ecryptfs/Private.mnt
to reflect your new home directory.Log out.
Press Ctrl+Alt+F7 to go back to the Ubuntu graphics mode login screen (on some Ubuntu revisions this may be Ctrl+Alt+F1).
And now you can log in using your new username.
Restart in recovery mode and go to the root prompt shell ("Drop to root shell prompt")
First remount the root
To change the username and home folder name,
For group name,
On Ubuntu 13.10, 14.04, 16.04:
When receiving
usermod: can't change /etc/password
just run the following commands:In the root recovery console run:
Then rerun:
when you do
usermod -l <newname> -d /home/<newname> -m <oldname>
you will getuseradd: can't change /etc/passwd
error message to avoid this just addsudo --
to above command likeand
The answer listed here by @karthick87 works perfectly fine but here is what I would do just to be safe and avoid all glitches:
Since not all the linuces (however Ubuntu must have it) have the
usermod
app, there is the way you can do it manually. As of root open /etc/passwd to edit withvim
or any other editor presening in the system:and change the user's name at the beginning of a line:
to:
then if you worked of root just login, and if you have been logged in as a user, logoff, and relogin.
Of course you have to fix /etc/shadow, and /etc/group also to the system works properly. Thanx to @JohanBoulé
NOTE: You should use this approach carefully, to not break the system.
NOTE: This approach is common, not only for Ubuntu, but and for ubuntu it will work, however google search will show it event for non ubuntu search, for example for embedded linux