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.
$ sudo apt-get install ACL
On the Centos,fedora,rhel.
2. Right to back up all files under the current directory (including subdirectories)
[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
[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
[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
[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:
Setfacl--restore=permissions.txt
You can see Linuxprobe.txt and install.txt privileges restored.
[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
... ....