Este conteúdo não está disponível no idioma selecionado.
5.6.2. Persistent Changes: semanage fcontext
The
semanage fcontext command is used to change the SELinux context of files. When using targeted policy, changes are written to files located in the /etc/selinux/targeted/contexts/files/ directory:
- The
file_contextsfile specifies default contexts for many files, as well as contexts updated viasemanage fcontext. - The
file_contexts.localfile stores contexts to newly created files and directories not found infile_contexts.
Two utilities read these files. The
setfiles utility is used when a file system is relabeled and the restorecon utility restores the default SELinux contexts. This means that changes made by semanage fcontext are persistent, even if the file system is relabeled. SELinux policy controls whether users are able to modify the SELinux context for any given file.
Quick Reference
To make SELinux context changes that survive a file system relabel:
- Run the
semanage fcontext -a options file-name|directory-namecommand, remembering to use the full path to the file or directory. - Run the
restorecon -v file-name|directory-namecommand to apply the context changes.
Procedure 5.7. Changing a File's or Directory 's Type
The following example demonstrates changing a file's type, and no other attributes of the SELinux context. This example works the same for directories, for instance if
file1 was a directory.
- As the Linux root user, run the
touch /etc/file1command to create a new file. By default, newly-created files in the/etc/directory are labeled with theetc_ttype:ls -Z /etc/file1
~]# ls -Z /etc/file1 -rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use thels -dZ directory_namecommand to list information about a directory. - As the Linux root user, run the
semanage fcontext -a -t samba_share_t /etc/file1command to change thefile1type tosamba_share_t. The-aoption adds a new record, and the-toption defines a type (samba_share_t). Note that running this command does not directly change the type;file1is still labeled with theetc_ttype:semanage fcontext -a -t samba_share_t /etc/file1 ls -Z /etc/file1
~]# semanage fcontext -a -t samba_share_t /etc/file1 ~]# ls -Z /etc/file1 -rw-r--r-- root root unconfined_u:object_r:etc_t:s0 /etc/file1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Thesemanage fcontext -a -t samba_share_t /etc/file1command adds the following entry to/etc/selinux/targeted/contexts/files/file_contexts.local:/etc/file1 unconfined_u:object_r:samba_share_t:s0
/etc/file1 unconfined_u:object_r:samba_share_t:s0Copy to Clipboard Copied! Toggle word wrap Toggle overflow - As the Linux root user, run the
restorecon -v /etc/file1command to change the type. Because thesemanagecommand added an entry tofile_contexts.localfor/etc/file1, therestoreconcommand changes the type tosamba_share_t:restorecon -v /etc/file1
~]# restorecon -v /etc/file1 restorecon reset /etc/file1 context unconfined_u:object_r:etc_t:s0->system_u:object_r:samba_share_t:s0Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure 5.8. Changing a Directory and its Contents Types
The following example demonstrates creating a new directory, and changing the directory's file type (along with its contents) to a type used by Apache HTTP Server. The configuration in this example is used if you want Apache HTTP Server to use a different document root (instead of
/var/www/html/):
- As the Linux root user, run the
mkdir /webcommand to create a new directory, and then thetouch /web/file{1,2,3}command to create 3 empty files (file1,file2, andfile3). The/web/directory and files in it are labeled with thedefault_ttype:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - As the Linux root user, run the
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"command to change the type of the/web/directory and the files in it, tohttpd_sys_content_t. The-aoption adds a new record, and the-toption defines a type (httpd_sys_content_t). The"/web(/.*)?"regular expression causes thesemanagecommand to apply changes to the/web/directory, as well as the files in it. Note that running this command does not directly change the type;/web/and files in it are still labeled with thedefault_ttype:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Thesemanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"command adds the following entry to/etc/selinux/targeted/contexts/files/file_contexts.local:/web(/.*)? system_u:object_r:httpd_sys_content_t:s0
/web(/.*)? system_u:object_r:httpd_sys_content_t:s0Copy to Clipboard Copied! Toggle word wrap Toggle overflow - As the Linux root user, run the
restorecon -R -v /webcommand to change the type of the/web/directory, as well as all files in it. The-Ris for recursive, which means all files and directories under the/web/directory are labeled with thehttpd_sys_content_ttype. Since thesemanagecommand added an entry tofile.contexts.localfor/web(/.*)?, therestoreconcommand changes the types tohttpd_sys_content_t:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that by default, newly-created files and directories inherit the SELinux type of their parent directories.
Procedure 5.9. Deleting an added Context
The following example demonstrates adding and removing an SELinux context. If the context is part of a regular expression, for example,
/web(/.*)?, use quotation marks around the regular expression:
semanage fcontext -d "/web(/.*)?"
~]# semanage fcontext -d "/web(/.*)?"
- To remove the context, as the Linux root user, run the
semanage fcontext -d file-name|directory-namecommand, where file-name|directory-name is the first part infile_contexts.local. The following is an example of a context infile_contexts.local:/test system_u:object_r:httpd_sys_content_t:s0
/test system_u:object_r:httpd_sys_content_t:s0Copy to Clipboard Copied! Toggle word wrap Toggle overflow With the first part being/test. To prevent the/test/directory from being labeled with thehttpd_sys_content_tafter runningrestorecon, or after a file system relabel, run the following command as the Linux root user to delete the context fromfile_contexts.local:semanage fcontext -d /test
~]# semanage fcontext -d /testCopy to Clipboard Copied! Toggle word wrap Toggle overflow - As the Linux root user, use the
restoreconutility to restore the default SELinux context.
Refer to the semanage(8) manual page for further information about
semanage.
Important
When changing the SELinux context with
semanage fcontext -a, use the full path to the file or directory to avoid files being mislabeled after a file system relabel, or after the restorecon command is run.