CentOS / RHEL: Change / Copy File SELinux Security Context Command

CentOS / RHEL: Change / Copy File SELinux Security Context Command

To change the SELinux security context of a file on a CentOS or Red Hat Enterprise Linux (RHEL) system, you can use the chcon command.

For example, to change the SELinux security context of a file named file.txt to the system_u:object_r:httpd_sys_content_t type, you can use the following command:

chcon -t httpd_sys_content_t file.txt
Source‮tual.www:‬turi.com

To copy the SELinux security context of a file to another file, you can use the -c option with the chcon command. For example, to copy the SELinux security context of a file named file1.txt to a file named file2.txt, you can use the following command:

chcon -c file1.txt file2.txt

Keep in mind that you will need to have root privileges (or be a member of the sudo group) in order to use the chcon command.

You can also use the semanage command to change the SELinux security context of a file. This command allows you to specify the SELinux context in the form of a user, role, and type, rather than just a type.

For example, to change the SELinux security context of a file named file.txt to the system_u:object_r:httpd_sys_content_t type, you can use the following command:

semanage fcontext -a -t httpd_sys_content_t "/path/to/file.txt"

To copy the SELinux security context of a file to another file, you can use the semanage fcontext -a command and specify the source file and the destination file. For example, to copy the SELinux security context of a file named file1.txt to a file named file2.txt, you can use the following command:

semanage fcontext -a -e "/path/to/file1.txt" "/path/to/file2.txt"

Keep in mind that you will need to have root privileges (or be a member of the sudo group) in order to use the semanage command.

Created Time:2017-10-27 14:56:36  Author:lautturi