Sub topic 14
SELinux and Shared Directories
Assume you know basic Linux permissions, can edit configuration files, and understand the Samba and NFS examples from earlier chapters. This chapter focuses on a second permission system: SELinux.
SELinux is a mandatory access control system. Linux file permissions answer, “Does this user have access?” SELinux also answers, “Is this service allowed to access this type of file?” A directory can have correct owners and modes but still be denied by SELinux.
Labeling a Samba directory
SELinux assigns a security context to every file. The context includes a type, such as samba_share_t. Samba is allowed to read and write files with this type.
For a custom Samba directory, add a persistent file-context rule and then apply it:
sudo semanage fcontext -a -t samba_share_t '/srv/samba/projects(/.*)?'
sudo restorecon -Rv /srv/samba/projects
ls -Zd /srv/samba/projectsThe (/.*)? pattern includes the directory and everything below it. restorecon applies the rule. Do not use only chcon for permanent configuration. chcon changes the current label but does not create a policy rule, so a later relabel can remove the change.
If Samba must write to the directory, verify both layers of permission:
sudo chgrp staff /srv/samba/projects
sudo chmod 2775 /srv/samba/projectsThe Unix group permissions and the SELinux type must both allow the operation.
Labeling an NFS export
A custom directory exported by the NFS server normally needs the nfs_t type:
sudo semanage fcontext -a -t nfs_t '/srv/nfs/projects(/.*)?'
sudo restorecon -Rv /srv/nfs/projects
ls -Zd /srv/nfs/projectsThe export still needs a suitable /etc/exports entry and a reload with exportfs -ra. SELinux labeling does not replace NFS client restrictions, file ownership, or root_squash.
Some systems use SELinux booleans when a service needs broader access. A boolean is a named policy switch. Prefer a specific label first. Use a broad boolean only when you understand its larger effect:
sudo setsebool -P nfs_export_all_rw on
sudo setsebool -P samba_export_all_rw onThe -P option makes the setting persistent. These switches can allow access to files beyond the carefully labeled share, so they increase the policy scope.
Finding SELinux denials
When a request fails unexpectedly, inspect recent denials:
sudo ausearch -m AVC -ts recent
sudo journalctl -t setroubleshoot --since "10 minutes ago"An AVC denial identifies the process, operation, and file type involved. Confirm that the denial belongs to your sharing service before changing policy.
Exercise
A Samba share has correct Unix permissions but clients receive “access denied.” What should you check first?
Show answer
Run ls -Zd on the share and its contents, confirm the type is samba_share_t, and inspect recent AVC records with ausearch -m AVC -ts recent.