Move home directory Linux

Sometimes in the course of administration you might have to move a user and its files around. This section will cover the process of moving a user’s home directory. In this section, you are going to move the user named “rksahoo” from his default home directory /home/rksahoo to /export/home/rksahoo. You will also have to set the proper permissions and ownership of the user’s files and directories so that the user can access will need superuser privileges to perform the steps in this exercise.

1. Log into the system as root and launch a virtual terminal.

2. Create the user that will be used for this project. The username is “rksahoo.” Type

[[email protected] ~]# useradd rksahoo

3. Use the grep command to view the entry for the user you created in the /etc/passwd file. Type

[[email protected] ~]# grep rksahoo /etc/passwd

4. Use the ls command to display a listing of the user’s home directory. Type

[[email protected] ~]# ls -al /home/rksahoo
total 48
drwx------ 3 rksahoo rksahoo 4096 2010-10-08 13:12 .
drwxr-xr-x 7 root root 4096 2010-10-08 13:12 ..
-rw-r--r-- 1 rksahoo rksahoo 33 2010-10-08 13:12 .bash_logout
-rw-r--r-- 1 rksahoo rksahoo 176 2010-10-08 13:12 .bash_profile
-rw-r--r-- 1 rksahoo rksahoo 124 2010-10-08 13:12 .bashrc

5. Check the total disk space being used by the user. Type

[[email protected] ~]# du -sh /home/rksahoo
56K /home/rksahoo

6. Use the su command to temporarily become the user. Type

[[email protected] ~]# su - rksahoo
[[email protected] ~]$

7. As user rksahoo, view your present working directory. Type

[[email protected] ~]$ pwd

8. As user rksahoo, create some empty files. Type

[[email protected] ~]$ touch a b c d e

9. Go back to being the root user by exiting out of rksahoo’s profile. Type

[[email protected] ~]$ exit

10. Create the /export directory that will house the user’s new home. Type

[[email protected] ~]# mkdir -p /export

11. Now use the tar command to archive and compress rksahoo’s current home directory (/home/rksahoo) and untar and decompress it into its new location. Type

[[email protected] ~]# tar czf - /home/rksahoo | (cd /export ; tar -xvzf -)

The dashes (-) you used here with the tar command force it to first send its output to stdout and then receive its input from stdin.

12. Use the ls command to ensure that the new home directory was properly created under the /export directory. Type

[[email protected] ~]# ls -R /export/home/
a b c d e

13. Make sure that rksahoo has complete ownership of all the files and directories in its new home. Type

[[email protected] ~]# chown -R rksahoo.rksahoo /export/home/rksahoo/

14. Now delete rksahoo’s old home directory. Type

[[email protected] ~]# rm -rf /home/rksahoo

15. Try to temporarily assume the identity of rksahoo again. Type

[[email protected] ~]# su - rksahoo
su: warning: cannot change directory to /home/rksahoo: No such file or directory

Here… one more thing left to do. We have deleted the user’s home directory (/home/rksahoo), as was specified in the /etc/passwd file, and that is why the su command complained.

16. Exit out of rksahoo’s profile using the exit command. Type

-bash-3.00$ exit

17. Now we’ll use the usermod command to automatically update the /etc/passwd file with the user’s new home directory. Type

[[email protected] ~]# usermod -d /export/home/rksahoo rksahoo

18. Use the su command again to temporarily become rksahoo. Type

[[email protected] ~]# su - rksahoo
[[email protected] ~]$

19. While logged in as rksahoo, use the pwd command to view your present working directory. Type

[[email protected] ~]$ pwd

This output shows that our migration worked out well.

20. Exit out of rksahoo’s profile to become the root user.

Note: If you want to deletee the user called rksahoo from the system. Type

[[email protected] ~]# userdel -r rksahoo


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.