The Problem
If accidentally the context or file permission of files under user’s home directory had been changed, permission errors or unexpected application behavior might be encountered after this user login system.
For instance, if the file permission of /home/user1/.bash_profile is wrong, login user1 will get prompt “/home/user1/.bash_profile: Permission denied”:
login as: user1 user1@geeklab's password: Last login: Mon Dec 15 15:08:20 2014 from geeklab2.example.com -bash: /home/user1/.bash_profile: Permission denied -bash-3.2$
This post instructs how to restore files/subdirectories under user’s home directory to default.
The Solution
There are 2 important files/directories which are required to restore the user’s home directory to default. They are mainly:
1. /etc/skel direcory
2. /etc/default/useradd
The skel directory
Directory /etc/skel/ (skel is derived from the “skeleton”) is used to initiate home directory when user is first created. A sample layout of “skeleton” user files:
# ls -lart /etc/skel total 32 drwxr-xr-x 4 root root 4096 Feb 4 2016 .mozilla -rw-r--r-- 1 root root 124 Feb 15 2017 .bashrc -rw-r--r-- 1 root root 176 Feb 15 2017 .bash_profile -rw-r--r-- 1 root root 18 Feb 15 2017 .bash_logout drwxr-xr-x. 3 root root 4096 Aug 22 2017 . drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..
# cat /etc/default/useradd # useradd defaults file GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel CREATE_MAIL_SPOOL=yes
Restore a file under home directory
1. For instance, if the .bash_profile file is removed from the user’s home directory as shown below.
$ rm ~/.bash_profile # ls -lart /etc/skel total 32 drwxr-xr-x 4 root root 4096 Feb 4 2016 .mozilla -rw-r--r-- 1 root root 124 Feb 15 2017 .bashrc -rw-r--r-- 1 root root 18 Feb 15 2017 .bash_logout drwxr-xr-x. 3 root root 4096 Aug 22 2017 . drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..
2. To restore the original .bash_profile file, copy default file from “skeleton” directory:
$ cp /etc/skel/.bash_profile ~/ # ls -lart ~/ total 32 drwxr-xr-x 4 root root 4096 Feb 4 2016 .mozilla -rw-r--r-- 1 root root 124 Feb 15 2017 .bashrc -rw-r--r-- 1 root root 176 Feb 15 2017 .bash_profile -rw-r--r-- 1 root root 18 Feb 15 2017 .bash_logout drwxr-xr-x. 3 root root 4096 Aug 22 2017 . drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..
Restore a sub directory under home directory
For instance, to restore an sub directory .mozilla, copy it with –recursive (-r) option:
$ cp -r /etc/skel/.mozilla/ ~/
Restore whole home directory from scratch
Let us see how we can restore the entire home directory for a user. For the purpose of this example, we will delete the home directory of user1.
1. Check user UID and GID:
$ id user1 uid=54324(user1) gid=54325(user1) groups=54325(user1)
2. Delete user home directory and the user via root privilege:
# rm -rf /home/user1
3. Copy all the files from /etc/skel directoy in user’s home directory.
# cp -r /etc/skel/* ~/
# ls -lart /home/user1/ total 32 drwxr-xr-x 4 root root 4096 Feb 4 2016 .mozilla -rw-r--r-- 1 root root 124 Feb 15 2017 .bashrc -rw-r--r-- 1 root root 176 Feb 15 2017 .bash_profile -rw-r--r-- 1 root root 18 Feb 15 2017 .bash_logout drwxr-xr-x. 3 root root 4096 Aug 22 2017 . drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..