A tutorial on backup and Restore file permission settings in Linux systems

Source: Internet
Author: User

You may have heard or encountered something like this: A system administrator accidentally entered the "Chmod-r 777/" so that the great tragedy caused the entire system has been seriously damaged. In day-to-day management, we have a number of tools that can be used to back up file permissions, such as CP, Rsync, Etckeeper, and so on. If you use this backup tool, you really don't need to worry about changing file permissions.

However, if you only want to temporarily back up file permissions (not the file itself), for example, to prevent the contents of some directories from being overwritten, temporarily remove all file write permissions from that directory, or you need to chmod the file for the file permission issue. In these cases, we can back up the original file permissions before they change, and then restore the original permissions when we need it. In many cases, a full file backup is unnecessary if you just want to back up the file's permissions.

On Linux, it is easy to actually backup and restore file permissions using Access control lists (ACLs). ACLs define the permissions of individual files on a POSIX-compliant file system based on different owners and groups.

The following shows how to use the ACL tool to back up and restore Linux file permissions

1. Installing ACL Tools

In Debian, Ubuntu,linux mint.

The code is as follows:
$ sudo apt-get install ACL

On the Centos,fedora,rhel.

The code is as follows:
$ sudo yum install ACL

2. Right to back up all files under the current directory (including subdirectories)

The code is as follows:
[Root@linuxprobe tmp]# Ls-l

Total 8
-rwxr--r--. 1 Root 0 Mar 3 04:40 install.txt
-rwxr-xr-x. 1 Root 0 Mar 3 04:41 linuxprobe.txt

The code is as follows:
[Root@linuxprobe tmp]# Getfacl-r. > Permissions.txt

...
This command writes all ACL information for all files to the named Permissions.txt file.

The following are some of the directory information in the generated Permissions.txt file

The code is as follows:
[Root@linuxprobe tmp]# Cat Permissions.txt

# file:.
# Owner:root
# Group:root
# Flags:--T
User::rwx
Group::rwx
Other::rwx
# File:install.txt
# Owner:root
# Group:root
User::rwx
group::r--
other::r--
# File:linuxprobe.txt
# Owner:root
# Group:root
User::rwx
Group::r-x
Other::r-x
# File:permissions.txt
# Owner:root
# Group:root
user::rw-
group::r--
other::r--
...

3. Modify a file permission, such as: Modify Linuxprobe.txt and Install.txt permissions

The code is as follows:
[Root@linuxprobe tmp]# chmod 733 linuxprobe.txt
[Root@linuxprobe tmp]# chmod 573 install.txt
[Root@linuxprobe tmp]# Ls-l

Total 8
-r-xrwx-wx. 1 Root 0 Mar 3 04:40 install.txt
-rwx-wx-wx. 1 Root 0 Mar 3 04:41 linuxprobe.txt
-rw-r--r--. 1 root 4361 Mar 3 04:41 permissions.txt
......

4. Restore existing Authority

1 cd to the directory where the Permissions.txt was created
2) Execute the following command:

The code is as follows:
Setfacl--restore=permissions.txt

You can see Linuxprobe.txt and install.txt privileges restored.

The code is as follows:
[Root@linuxprobe tmp]# Setfacl--restore=permissions.txt
[Root@linuxprobe tmp]# Ls-l

Total 8
-rwxr--r--. 1 root root    0 mar  3 04:40 install.txt
-rwxr-xr-x. 1 root root     0 mar  3 04:41 linuxprobe.txt
-rw-r--r--. 1 root root 4361 mar  3 04:41 permissions.txt
... ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.