Skip to main content

Linux Change Username, UID (userid) and GID (groupid)

Assume we would like to change a user's username, uid and gid into some random number.

Take the following username, uid and gid as an example:

  Before Change After Change
Username ubuntu test
UID (userid) 1000 1525141003
GID (groupid) 1000 334330370

First, we need to ensure that there are no process started by user ubuntu. The easy way to do it is reboot your computer and check that there are no process started by the user.

ps -ef | grep ubuntu

If there does show any, we may kill all these processes through the following command:

pkill -u ubuntu "[PID]"

Afterwards, we can start the changes:

# rename the login username: ubuntu -> test
usermod -l test ubuntu
# move and set the home directory
usermod -m --home /home/test test
# rename the group name: ubuntu -> test
groupmod -n test ubuntu
# set the uid, gid of test user and test group
usermod -u 1525141003 test
groupmod -g 334330370 test

That's it. Re-login via the new user and check that it is all set.

It is also very easy to add a new user or remove a user:

# add a new user
useradd --create-home -G "[groupname]" "[username]"
# add a new group
groupadd "[groupname]"
# add a user to a group
adduser "[username]" "[groupname]"

for groupname in adm cdrom sudo dip plugdev lxd lpadmin sambashare; do
  adduser ubuntu "${groupname}"

# delete a user with home directory and mail spool removed
userdel -r "[username]"



Comments powered by Disqus