Sub topic 14
SELinux and Shared Directories
This chapter assumes you can already configure a Samba share and an NFS export, open the firewall for them, and read basic file permissions. Here you will learn why a correct smb.conf or /etc/exports can still fail on a Red Hat-family system, and how to fix it without turning SELinux off.
Why SELinux matters for sharing
SELinux (Security-Enhanced Linux) is a mandatory access control system. Normal Linux permissions (owner, group, mode bits) are checked first. SELinux then applies a second, separate check based on labels. Both checks must allow the access.
Every process and every file carries a label called a security context. A context looks like this:
system_u:object_r:samba_share_t:s0The part that matters for day-to-day work is the third field, the type (samba_share_t above). A policy rule says which process types may touch which file types. The Samba daemon runs as type smbd_t. By default it may read and write files labeled samba_share_t, but not files labeled default_t or var_t.
That is why a share under /srv/samba/projects can fail with "permission denied" even though chmod 2775 and group ownership are correct. The directory was created under /srv and carries a generic label that the Samba policy does not allow.
SELinux is enabled by default on RHEL, Fedora, Rocky Linux, AlmaLinux and CentOS Stream. Debian and Ubuntu use AppArmor by default, so most of this chapter does not apply there.
Checking the current state
Three commands show what SELinux is doing.
getenforce
sestatus
ls -dZ /srv/samba/projectsgetenforceprintsEnforcing,PermissiveorDisabled. In permissive mode SELinux logs violations but does not block them.sestatusshows the mode, the loaded policy, and the mode set in the configuration file.ls -Zshows the context of files and directories.ps -eZ | grep smbddoes the same for processes.
How a denial happens
The flow below shows where SELinux sits in a file request.
A denial from any layer looks nearly the same to the client. Windows shows "Access is denied" and a Linux CIFS client reports Permission denied. Only the server logs tell you which layer refused.
Labeling a Samba share directory
Use semanage fcontext to record a labeling rule, then restorecon to apply it. The rule survives a filesystem relabel. A plain chcon change does not survive one, so avoid it for permanent fixes.
sudo dnf install -y policycoreutils-python-utils
sudo semanage fcontext -a -t samba_share_t "/srv/samba/projects(/.*)?"
sudo restorecon -Rv /srv/samba/projects
ls -dZ /srv/samba/projectsThe pattern (/.*)? makes the rule cover the directory and everything below it. restorecon -Rv walks the tree, applies the labels and prints each change.
New files created later inherit the label of their parent directory, so you only need to run restorecon again for files that were copied or moved in from elsewhere. A moved file keeps its old label, which is a common source of "this one file will not open" problems.
Samba booleans
A boolean is an on/off switch built into the policy. Booleans cover common non-default uses so you do not need to write custom policy.
| Boolean | What it allows |
|---|---|
| `samba_enable_home_dirs` | Share users' home directories through Samba |
| `samba_export_all_rw` | Read and write any file type (a broad setting) |
| `samba_export_all_ro` | Read any file type |
| `smbd_anon_write` | Write to public directories for anonymous users |
| `samba_share_nfs` | Let Samba share a directory that is an NFS mount |
| `samba_create_home_dirs` | Create home directories for new users |
List the current values and change one:
getsebool -a | grep -E 'samba|smbd'
sudo setsebool -P samba_enable_home_dirs onThe -P flag makes the change permanent. Without it the value reverts at reboot.
Prefer the narrow fix. Labeling one directory as samba_share_t grants access to that directory only. samba_export_all_rw lets Samba write nearly everywhere on the system, which is rarely what you want.
NFS and SELinux
For NFS the server-side policy is more permissive by default. The nfsd_t process reads exports without needing a special label on most setups, and the relevant switches are booleans.
| Boolean | What it allows |
|---|---|
| `nfs_export_all_rw` | Export any file read-write (default on) |
| `nfs_export_all_ro` | Export any file read-only (default on) |
| `use_nfs_home_dirs` | Let local services use home directories mounted over NFS |
| `httpd_use_nfs` | Let the web server read NFS-mounted content |
| `samba_share_nfs` | Let Samba re-share an NFS mount |
On the client side, the files of an NFS mount get a label from the mount itself, typically nfs_t. Local services that read those files, such as a web server or a database, need the matching boolean. For example, a web server serving files from an NFS mount needs httpd_use_nfs.
sudo setsebool -P httpd_use_nfs onThe same pattern applies to CIFS mounts, where the files carry the cifs_t type and booleans such as httpd_use_cifs control access. If you want every file on a mount to carry a chosen label, use the context= mount option:
sudo mount -t cifs //fileserver/projects /mnt/projects \
-o username=alice,vers=3.0,context="system_u:object_r:samba_share_t:s0"The context= option labels the whole mount with one value. You cannot change labels on individual files within it.
Finding the denial
When access fails and you suspect SELinux, read the audit log. Denials are recorded as AVC (access vector cache) messages.
sudo ausearch -m avc -ts recent
sudo journalctl -t setroubleshoot --since "10 minutes ago"An AVC message names the process, the target file, and the missing permission:
type=AVC msg=audit(1700000000.123:456): avc: denied { write } for
pid=1234 comm="smbd" name="report.txt" dev="sda1" ino=5678
scontext=system_u:system_r:smbd_t:s0
tcontext=unconfined_u:object_r:default_t:s0 tclass=file permissive=0Read it from scontext (the process type, smbd_t) to tcontext (the file type, default_t). Here the fix is to relabel the file to samba_share_t.
If the setroubleshoot-server package is installed, sealert -a /var/log/audit/audit.log turns each denial into a plain-language suggestion. Check the suggested command before you run it. It sometimes proposes a custom policy module when a simple label change would do.
A repeatable workflow
- Confirm the problem is not permissions or the firewall by testing with
setenforce 0. If access now works, SELinux is the cause. - Run
setenforce 1again immediately. - Run
ausearch -m avc -ts recentand read the process type and file type. - Choose the narrowest fix: a file context rule first, a boolean second, a custom module last.
- Apply it, then retest from the client.
Common mistakes
- Using
chconand losing the label after a relabel orrestorecon. - Moving files into the share with
mv, which keeps the old label. Copy them instead, or runrestorecon -Rafterward. - Setting
samba_export_all_rwbecause it is quick, which widens access far past the share. - Forgetting
-Ponsetsebool, so the setting disappears at reboot. - Looking only at
smb.confand file modes while the real denial sits in the audit log.
Exercise
A Samba share at /data/reports (not under /srv) returns "Permission denied" to Linux and Windows clients. The mode is 2775, the group is correct, and testparm shows no errors. Running setenforce 0 makes the share work. Give the commands that fix it permanently while leaving SELinux enforcing, and say why chcon is the wrong tool.
Show answer
Run sudo setenforce 1, then sudo semanage fcontext -a -t samba_share_t "/data/reports(/.*)?" followed by sudo restorecon -Rv /data/reports. The first command stores a labeling rule in the policy and the second applies it to the existing files. chcon only changes the label on the files now, and the change is lost when the filesystem is relabeled or restorecon runs, because no rule records it.
Exercise
An AVC denial shows scontext=...:httpdt:s0 and tcontext=...:nfst:s0 with tclass=file. What is happening, and what is the narrowest fix?
Show answer
The web server process (httpdt) is being blocked from reading files on an NFS mount, which carry the nfst label. Enable the matching boolean with sudo setsebool -P httpd_use_nfs on. Relabeling the files is not possible, because the mount supplies the label.