By example
-
Change the uid of
gert
(now1000
) to1002
:usermod -u 1002 gert
Changing user IDs with
usermod
will also automagically change the ownership of files within the user's home directory. For files on other locations, please refer to the section below. -
Change the username of
gert
togvandijk
:usermod -l gvandijk gert
-
Change the gid (primary group) of
gert
(now1000
) to1002
:usermod -g 1002 gert</code>
-
Change the gid of the group
thegroup
(now1500
) to1501
:groupmod -g 1501 thegroup
-
Change the groupname of
thegroup
tomygroup
:groupmod -n mygroup thegroup
-
Changing the user ID and group ID can also be combined in one command:
usermod -u 1002 -g 1002 gert
-
Changing the user's home directory to the new location (move the directory manually!):
usermod -d /newhomes/gvandijk gvandijk
Most usermod
commands can only be executed when the user is not logged in.
Fix ownership of files
Files on a Linux filesystem are stored with metadata like owner and group using numeric IDs. When you change these IDs, you will have to fix the IDs on the files.
For files outside the home directory, you'll have to fix all the ownerships of the user's files:
find / -user 1000 -exec chown -h 1002 {} \;
Explanation: Find all kind of files/directories with user ID 1000
from the base /
, then execute the command chown -h 1002 <entry>
for every entry found.