• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to restore files under user’s home directory to default in Linux

By admin

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 ..
Note: “skeleton” directory is defined in /etc/default/useradd file.
# 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 ..
Note: Copy the file under corresponding user privilege. If copied the file via root user, you need to manually change ownership and file permission afterwards.

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

Filed Under: Linux

Some more articles you might also be interested in …

  1. How to Install Apache, MariaDB, and PHP (FAMP) stack on FreeBSD 11
  2. How to kill Processes in Linux using kill, killall and pkill
  3. How to Calculate Memory Usage in Linux using sar, ps, and free
  4. Volume “test_vg/lvol0” is not active locally – Error while running lvcreate
  5. UNIX / Linux : Examples of bash history command to repeat last commands
  6. RHEL 7 – RHCSA Notes – System documentation including man, info, and files in /usr/share/doc
  7. Linux / UNIX : How to find files which has SUID/SGID set
  8. BTRFS: too many missing devices, writeable mount is not allowed
  9. Extend volume on non-partitioned disk (XFS) under VMware guest
  10. What happens in the Background when you execute the “useradd” command under Linux

You May Also Like

Primary Sidebar

Recent Posts

  • How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  • How to Capture More Logs in /var/log/dmesg for CentOS/RHEL
  • Unable to Start RDMA Services on CentOS/RHEL 7
  • How to rename a KVM VM with virsh
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary