The Ultimate Guide to Migrating a User on Linux: Ownership, Groups, Crontab, Config Files, and More
I recently had a request to replace a user on the Linux server. This Linux user has a lot of files, folders and cronjobs that I need to move to the new user. This may sound like a simple task, but it's not. In this article I will show you the steps to make this change successful.
Let's get started!
Notes:
Old user: OLDUSER
New user: NEWUSER
Replace the name before executing these commands!
- Before starting the process, make sure that the new user (NEWUSER) already exists on the system:
id NEWUSER
- Log in as a root user.
sudo su
- Move files from the OLDUSER user to NEWUSER, if there are any:
mv /home/OLDUSER/* /home/NEWUSER/
Note: Changing file and folder permissions later - Check which groups the OLDUSER user belongs to:
groups OLDUSER
If there are any (except those named OLDUSER), add the NEWUSER user to it/them:sudo usermod -a -G groupName NEWUSER
- Change the ownership of files and folders on the whole system from OLDUSER to NEWUSER:
find / -user OLDUSER 2>/dev/null -exec chown NEWUSER {} \;
find / -group OLDUSER 2>/dev/null -exec chown :NEWUSER {} \;
Note: These commands search for all files and folders owned by OLDUSER, and then change the ownership to NEWUSER one by one. - Check which process is running by OLDUSER user:
ps -ef | grep OLDUSER
- Also check the systemd service files:
grep -rli "OLDUSER" /etc/systemd /usr/local/lib /usr/lib/systemd 2>/dev/null
After modifying at least one service file, reload the systemd daemon:systemctl daemon-reload
- Make sure the old user doesn't have any crontab entries. If there are, migrate them too.
Check and print it out:crontab -l -u OLDUSER
You can edit the crontab of NEWUSER with this command:crontab -e -u NEWUSER
Or simply migrate the crontab with this command:mv /var/spool/cron/olduser /var/spool/cron/newuser
service crond restart
- Then check all crontab entries for the OLDUSER user by running the following command:
grep -rli "OLDUSER" /etc/cron* 2>/dev/null
- Also check the OLDUSER user in the /etc directory:
grep -rli "OLDUSER" /etc 2>/dev/null
If there is a configuration file here that contains OLDUSER, replace it with NEWUSER one at a time. - Reboot and see if everything that worked under OLDUSER works.
That's all.
If you found this article useful and would like to show your appreciation, please consider making a small donation via PayPal. Your support will allow me to continue creating valuable content and make my blog even better. Thank you for your contribution!
Comments