Linux Change or Rename User Name and UID

How do I change user name (rename user name) or UID under a Linux operating system using command line tools? How do I change or rename username in Linux?

You need to use the usermod command to change user name under a Linux operating systems. This command modifies the system account files to reflect the changes that are specified on the command line. Do not edit /etc/passwd file by hand or using a text editor such as vi. This page explains how to change or rename username in Linux using the usermod command.

Linux Change or Rename User Command Syntax


The syntax is as follows to rename by user name:
usermod -l login-name old-name

  • We use the usermod command in Linux to rename user account. The name of the user will be changed from the old-name to login_name. Nothing else is changed. In particular, the user’s home directory name should probably be changed to reflect the new login name.

The syntax is as follows to rename by a UID (user ID):
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 at 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.

List all users in Linux system

Type the following cat command:
cat /etc/passwd
One can use the grep command to filter out only user names:
grep -w '^username' /etc/passwd
grep -w '^jerry' /etc/passwd

Another option is to use the cut command:
cut -d: -f1 /etc/passwd
Sample outputs:

daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
....
..
....
vivek
raj
marlena
tristan
wendy
rob
systemd-coredump
dnsmasq
kernoops
rtkit
whoopsie
usbmux
cups-pk-helper
nm-openvpn
speech-dispatcher
saned
colord
hplip
geoclue
pulse
gnome-initial-setup
gdm
tcpdump
nvidia-persistenced
tss
nm-openconnect
vnstat
lxd
postfix

How to Change or Rename Username and UID in Linux

Let us see how to rename user login. First, make sure user name is not logged into the server and any other process is not running under the same user name. I also recommend that you backup any data or server files before changing user names.

View current user and group membership for user named tom

First get user identity using the id command:
id tom
Next use the grep command to grab login info about user named tom from the /etc/passwd file
grep '^tom:' /etc/passwd
See group info about user named tom using the groups command:
grep 'tom' /etc/group
groups tom

Find home directory permissions for user named tom, run the following ls command:
ls -ld /home/tom/
Finally, see all Linux process owned by user and group named tom using the ps command:
ps aux | grep tom
ps -u tom

Rename and change username from tom to jerry on Linux

Type the usermod command as follows:
# id tom
# usermod -l jerry tom
## Verify ###
# id tom
# id jerry
# ls -ld /home/tom

A note about running process

You may see an error as follows if tom is logged in and running jobs:

# usermod -l jerry tom
usermod: user tom is currently used by process 6886

You need to kill all Linux process owned by user named tom and forcefully logged them out of the system:

# pkill -u tom pid
# pkill -9 -u tom
# usermod -l jerry tom

Rename and change primary groupname from tom to jerry

Type the usermod command as follows:
# id tom
# groupmod -n jerry tom
## Verify it ###
# id tom
# ls -ld /home/tom

How to change user home directory from /home/tom/ to /home/jerry

The syntax is as follows:
# usermod -d /home/jerry -m jerry
# id jerry
# ls -ld /home/jerry

Sample outputs:

uid=1001(jerry) gid=1001(jerry) groups=1001(jerry)
drwxr-xr-x 2 jerry jerry 4096 Apr 21 15:53 /home/jerry/

How to change user tom UID from 5001 to 10000

Type the usermod command as follows:
# id tom
# usermod -u 10000 tom
# id tom

Getting help about usermod command

You can pass the --help option to the usermod command. For instance, type the following command at the shell prompt in Linux:
usermod --help

Options Description
-c OR --comment COMMENT new value of the GECOS field
-d OR --home HOME_DIR new home directory for the user account
-e OR --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f OR --inactive INACTIVE set password inactive after expiration to INACTIVE
-g OR --gid GROUP force use GROUP as new primary group
-G OR --groups GROUPS new list of supplementary GROUPS
-a OR --append append the user to the supplemental GROUPS mentioned by the -G option without removing the user from other groups
-h OR --help display this help message and exit
-l OR --login NEW_LOGIN new value of the login name
-L OR --lock lock the user account
-m OR --move-home move contents of the home directory to the new location (use only with -d)
-o OR --non-unique allow using duplicate (non-unique) UID
-p OR --password PASSWORD use encrypted password for the new password
-R OR --root CHROOT_DIR directory to chroot into
-P OR --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s OR --shell SHELL new login shell for the user account
-u OR --uid UID new UID for the user account
-U OR --unlock unlock the user account
-v OR --add-subuids FIRST-LAST add range of subordinate uids
-V OR --del-subuids FIRST-LAST remove range of subordinate uids
-w OR --add-subgids FIRST-LAST add range of subordinate gids
-W OR --del-subgids FIRST-LAST remove range of subordinate gids
-Z OR --selinux-user SEUSER new SELinux user mapping for the user account

Conclusion

In this tutorial, you learned how to change or rename username and UID in Linux using the usermod command.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

SSH login without password

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login...

How to remove a Linux user

How do I drop or remove old users account from my Linux server? I can login using the user’s SSH...

How to restrict and except SSH access to specific IPs

Once your IP is public it gets attention from so many bots in the internet that do brute force...

How to lock and unlock user account in linux

With the help of two commands you can lock and unlock the user account in Linux. To Lock the...

Adding a user to a group

Synopsis This article describes how to add a Linux user (account) to a group. Environment...

Powered by WHMCompleteSolution