Rename or Change User Name and UID on Linux Systems.
You might have come across a situation where you want to rename a user name in Linux system, for whatever reasons. In this short tutorial, we will be discussing how to change user name (rename user name) or UID under a Linux operating system using command line tools.
We will see and use the usermod
command to change a username in Unix/Linux distributions. usermod
is used to modify or change any attributes of an already created user account via command line. It is similar to the useradd
or adduser
command only here the login is granted to an existing user.
PS: do not edit /etc/passwd
file by hand or by using a text editor such as nano
or vi
.
Rename/ change by user name
First: You need to log out from the account you are trying to rename. Either by simply logging out or by killing all the processes running for that user. Execute the following command:
$ sudo pkill -u USER
or
sudo pkill -9 -u USER
now, back to our previous example.
The syntax is as follows:
$ usermod -l new_username old_username
ie. If you have a user named 'batman' and want to rename it to 'snubmonkey', execute the following command from your terminal.
$ usermod -l batman snubmonkey
Remember; this will only change the username; everything else, like group, home directory, and UID will remain the same.
Change user UID (user ID)
The syntax is as follows:
$ usermod -u UID username
where, the numerical value of the user’s ID (UID). This value must be unique unless the -o option is used. The value must be non-negative. Values between 0 and 99 are typically reserved for system accounts. Any files which the user owns and which are located in the directory tree rooted, in the user’s home directory will have the file user ID changed automatically. Files outside of the user’s home directory must be altered
manually.
In the example below, you see that the user account snubmonkey holds the UID of 1003, now we want to change it to 777 as a new UID.
$ id snubmonkey
uid=1003(snubmonkey) gid=1003(snubmonkey) groups=1003(snubmonkey),4(adm),24(cdrom),
27(sudo),30(dip),46(plugdev),116(lxd),1004(XX),
1005(PIC),1006(RNC),1008(Q),123(lpadmin)
Now, let’s change the UID for user snubmonkey using -u
(uid) option and verify the changes.
$ usermod -u 777 snubmonkey
uid=777(snubmonkey) gid=1003(snubmonkey) groups=1003(snubmonkey),4(adm),24(cdrom),27(sudo),
30(dip),46(plugdev),116(lxd),1004(XX),1005(PIC),
1006(RNC),1008(Q),123(lpadmin)
the
Rename and change the primary groupname
To rename the group from 'king' to 'queen', we will use the groupmod
command.
Use the following command to rename the group:
$ ugroupmod -n king queen
To use a name that already exists, just apply the following command:
$ ugroupmod -o -n king queen
...with this, we end this tutorial on how to rename users in Linux.