How to change userid or group id in Linux ?

In this post today, we will look how to change the userid and group id in linux operating system. You might be thinking that this is a very easy task and can easily be done by usermod command. 
 
	No the above statement is not completely correct. There is one more and the most important thing we have to modify when we are changing the userid or group id of the user. 
 
	Let us take an example where we have to modify the userid and groupid of a user jack. Suppose the new userid is you want to assign this user is 499 and the group id to some group id say 599. 
 
	  
 
	1. So the first step is to check the current user and group id of the user. 
 
	 bash-3.00# id -a jack 
 
	uid=136(jack) gid=1(other) groups=1(other) 
 
	  
 
	2. Now lets see if the userid and group id are available on our system. Check local as well as ad/ldap file for the user account. 
 
	 bash-3.00# cat /etc/passwd | grep -i :499: 
 
	bash-3.00# getent passwd | grep -i :499: 
 
	Now we can see no result and hence this userid is available. 
 
	  
 
	3. Now we will check whether the new group in which we want to assign the user is available on the system or not. 
 
	bash-3.00# cat /etc/group | grep -i 599: 
 
	jackgp::599: 
 
	Here we can see that the group id is available, we can also add the group the group by using below command. 
 
	# groupadd -g 599 jackgp 
 
	  
 
	4. Now we are ready to change the user id and group id of the user.  Simply use usermod command to change the group as well as userid of jack like below: 
 
	bash-3.00# usermod -u 499 -g 599 jack 
 
	bash-3.00# id -a jack 
 
	uid=499(jack) gid=599(jackgp) groups=599(jackgp) 
 
	  
 
	5. Now you have changed the userid and group id of the user. Now simply look for files earlier owned by user jack on the system except his home directory whose ownership automatically gets updated. 
 
	 bash-3.00# ls -ld "/var/tmp/jack" 
 
	drwxr-xr-x   2 136      root         512 Oct 16 00:46 /var/tmp/jack 
 
	bash-3.00# 
 
	Here we can see that the earlier user id  of user jack is 136 and it is not changed. One more thing becasue of this user id change any of the running processes by this user will all became unusable and because of this any running applications by this user, all would be stopped. 
 
	So always make sure that no critical process is running while doing this change, or just kill and restart the process again after the change. 
 
	Now after changing the user id and group id, we have to make sure that these permissions are modified for all files and directories owned by this user on our system. So just run below commands to modify the owner and group id of files earlier owned by jack. 
 
	# find / -xdev -user 136 -exec chown -H 499 {} ; 
 
	# find / -xdev -user 136 -group 1 -exec chgrp -H 599 {} ; 
 
	"-H" causes each symlink to be parsed and updated.  
 
	The above command is very important to update our userid and group id change successfully and to avoid any issues. 
 
	But always remember to kill the processes with old user id and restart any restart the processes with new user id to avoid any issue while change. 
 
	 Do post your comments in case you find anything else or if you have any questions. 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments