Move home directory Linux

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 it.you 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

[root@fedora-serverA ~]# useradd rksahoo

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

[root@fedora-serverA ~]# grep rksahoo /etc/passwd
rksahoo:x:502:503::/home/rksahoo:/bin/bash

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

[root@fedora-serverA ~]# 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

[root@fedora-serverA ~]# du -sh /home/rksahoo
56K /home/rksahoo

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

[root@fedora-serverA ~]# su - rksahoo
[rksahoo@fedora-serverA ~]$


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

[rksahoo@fedora-serverA ~]$ pwd
/home/rksahoo

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

[rksahoo@fedora-serverA ~]$ touch a b c d e

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

[rksahoo@fedora-serverA ~]$ exit

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

[root@fedora-serverA ~]# 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

[root@fedora-serverA ~]# 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

[root@fedora-serverA ~]# ls -R /export/home/
/export/home/:
rksahoo
/export/home/rksahoo:
a b c d e

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

[root@fedora-serverA ~]# chown -R rksahoo.rksahoo /export/home/rksahoo/

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

[root@fedora-serverA ~]# rm -rf /home/rksahoo

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

[root@fedora-serverA ~]# su - rksahoo
su: warning: cannot change directory to /home/rksahoo: No such file or directory
-bash-3.2$

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

[root@fedora-serverA ~]# usermod -d /export/home/rksahoo rksahoo

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

[root@fedora-serverA ~]# su - rksahoo
[rksahoo@fedora-serverA ~]$

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

[rksahoo@fedora-serverA ~]$ pwd
/export/home/rksahoo

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

[root@fedora-serverA ~]# userdel -r rksahoo

 

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

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