Talk about how to back up and restore Linux file permissions

Source: Internet
Author: User
Tags linux mint

You may have heard or encountered something like this: A system administrator rookie accidentally entered "Chmod-r 777/" resulting in a huge tragedy, causing the entire system to be seriously damaged. In daily management, we have many tools that can be used to back up file permissions such as CP, rsync, Etckeeper, etc. If you use this backup tool, you really don't need to worry about changing file permissions.

However, if you just want to temporarily back up the file permissions (not the file itself), for example, to prevent the contents of some directories from being overwritten by temporarily removing all files written in that directory, or when you are troubleshooting file permissions, you need to perform a chmod command operation on the file. In these cases, we can back up the original file before the permissions 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 permissions.
On Linux, it is easy to actually back up and restore file permissions by using Access control lists (ACLs). ACLs define the permissions of individual files on a POSIX-compatible file system based on different owners and groups.

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

1. Installing the ACL tool

On Debian, Ubuntu,linux Mint.

$ sudo apt-get install ACL

On the Centos,fedora,rhel.

$ sudo yum install ACL

2. Back up permissions for all files under the current directory (including subdirectories)

[Email protected] 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
[Email protected] tmp]# getfacl-r. > Permissions.txt
...

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

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

[email protected] 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 the permissions of a file, such as: Modify Linuxprobe.txt and Install.txt permissions

[Email protected] tmp]# chmod 733 linuxprobe.txt
[Email protected] tmp]# chmod 573 install.txt
[Email protected] tmp]# ls-l
Total 8
-r-xrwx-wx. 1 root root 0 Mar 3 04:40 install.txt
-rwx-wx-wx. 1 root root 0 Mar 3 04:41 linuxprobe.txt
-rw-r--r--. 1 root root 4361 Mar 3 04:41 permissions.txt
......

4. Restore Legacy Permissions

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

Setfacl--restore=permissions.txt

You can see that Linuxprobe.txt and Install.txt permissions are restored.

[Email protected] tmp]# Setfacl--restore=permissions.txt
[Email protected] 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

Ding Feng Xiao Hu
qq.2881064155
[Email protected]

Talk about how to back up and restore Linux file permissions

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.