How to Change or Rename User Name and User ID in Linux

This post will guide you how to change or rename a given User name and user ID in your Linux Operating system. How do I change a user name to a new user name using usermod command in the CentOS or RHEL or Ubuntu Linux system.

change rename username uid linux1

Usermod Command


The usermod command modifies the system account files to reflect the changes that are specified on the command line.

The syntax of the Usermod command is as followss:

usermod [options] LOGIN

The Options which apply to the usermod command are:

-l, --login NEW_LOGIN
The name of the user will be changed from LOGIN to NEW_LOGIN. Nothing else is changed. In particular, the
user's home directory or mail spool should probably be renamed manually to reflect the new login name.

-u, --uid UID
The new numerical value of the user's ID.

This value must be unique, unless the -o option is used. The value must be non-negative.

The user's mailbox, and any files which the user owns and which are located in the user's home directory
will have the file user ID changed automatically.

The ownership of files outside of the user's home directory must be fixed manually.

The change of the user ownership of files inside of the user's home directory is also not done if the home
dir owner uid is different from the current or new user id. This is safety measure for special home
directories such as /.

List All User Accounts


If you want to list all available User accounts in your current Linux system, and you can type the following command at the shell prompt:

$ cat /etc/passwd

Outputs:

[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-timesync:x:998:996:systemd Time Synchronization:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
gluster:x:996:993:GlusterFS daemons:/run/gluster:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
nm-openconnect:x:995:990:NetworkManager user for OpenConnect:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
pipewire:x:993:988:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
dnsmasq:x:987:987:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
saslauth:x:986:76:Saslauthd user:/run/saslauthd:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
openvpn:x:985:984:OpenVPN:/etc/openvpn:/sbin/nologin
nm-openvpn:x:984:983:Default user for running openvpn spawned by NetworkManager:/:/sbin/nologin
unbound:x:983:982:Unbound DNS resolver:/etc/unbound:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
colord:x:982:980:User for colord:/var/lib/colord:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
flatpak:x:981:979:User for flatpak system helper:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:980:978::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
chrony:x:979:977::/var/lib/chrony:/sbin/nologin
vboxadd:x:978:1::/var/run/vboxadd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
devops:x:1000:1000:devops:/home/devops:/bin/bash

From the above outputs, you can get the UID or GID number for your User account that you want to rename or change.

For example, The UID of User account “devops” is 1000.

You can also use another command called “id” to print real and effective user and group IDs, type:

$ id devops

outputs:

[root@localhost ~]# id devops
uid=1000(devops) gid=1000(devops) groups=1000(devops),10(wheel)

Rename a Given UserName


If you want to rename a given user account to another new user name, and you can use usermod command with -l option.

For example, you wish to change username “devops” to a new username called “devops01“, you just need to type the following command:

$ sudo usermod -l devops01 devops

Note: if your old user name is currently used by other process, and you will get the following error message:

usermod: user devops is currently used by process 1427

If you want to continue to rename this username, and you have to kill the process 1427 using the following command, type:

$ sudo pkill -9 -u devops

Then you can use “id” command to verify if it is reanmed, type:

$ id devops
$ id devops01

outputs:

[root@localhost ~]# id devops
id: ‘devops’: no such user

[root@localhost ~]# id devops01
uid=1000(devops01) gid=1000(devops) groups=1000(devops),10(wheel)

Change UID for a Given User


If you want to change User IDs for a given user (devops01), and you just need to pass the -u option to the usermod command.

For example, you wish to change UID of user account “devops01” to 1005, just issuing hte following command:

$ sudo usermod -u 1005 devops01

Outputs:

[root@localhost ~]# id devops01
uid=1000(devops01) gid=1000(devops) groups=1000(devops),10(wheel)

[root@localhost ~]# usermod -u 1005 devops01

[root@localhost ~]# id devops01
uid=1005(devops01) gid=1000(devops) groups=1000(devops),10(wheel)

Conclusion


You should know that how to use usemod command to rename or change a given user account and user Ids in CentOS or RHEL or Ubuntu Linux systems.

You might also like:

Sidebar



back to top