7.3 System Hardening, Privilege Delegation (sudoers) & Mandatory Access Control

Key Takeaways

  • Use visudo to check the installed sudo policy. NOPASSWD and NOEXEC affect different parts of scoped delegation.

  • Calculate permissions by clearing mask bits from the requested mode or applying a parent’s default ACL. Verify group and special-bit behavior on the actual filesystem.

  • File flags and ACLs can affect operations independently of the ordinary mode. Check directory traversal, inheritance and the actual identity.

  • Correlate SELinux denials and inspect default path types before relabeling. Verify the loaded AppArmor profile and its mode when that is the active policy.

  • Check effective SSH authentication and forwarding, and review filesystem dependencies, listeners, Fail2Ban and Secure Boot in their system-specific scope.

Last updated: November 2026

7.3 System Hardening, Privilege Delegation & Mandatory Access Control

Give people and services the access required for specific tasks. Inspect identities, file permissions, mandatory policy and service configuration together when determining what access a process actually has.

Delegate a command with sudo

sudo changes the identity under which an authorized command runs. Check /etc/sudoers and its configured includes, often /etc/sudoers.d/. A sudoers rule identifies the invoking subject, hosts, run-as identity and allowed commands. For example, on a lab host where the verified executable is /usr/bin/systemctl, this rule delegates one named service operation:

%laboperators lab01 = (root) /usr/bin/systemctl restart lab-report.service

The path and arguments constrain the matching command. Also review what that command can do and who can modify its unit, executable or configuration. Avoid granting broad shell or interpreter access for narrow tasks. Inspect the actual policy before assuming membership in wheel or sudo grants a particular right. Those administrative group names are common conventions.

ControlWhat to verify
visudoCoordinates edits and validates syntax. Use visudo -f /etc/sudoers.d/lab-report to edit a drop-in and visudo -c to check the assembled configuration. An included directory can skip filenames containing a period or ending in a tilde. Apply the installed policy's ownership and mode requirements.
NOPASSWDRemoves the usual authentication requirement for the tagged commands. It can increase the effect of a compromised login session. Command scope, target identity and logging are separate concerns.
NOEXECOn a supported build and operating system, constrains execution of further programs from the tagged command. It cannot prevent privileged actions that the command performs itself.
sudo -i and su -Request a login-style shell as the target identity, root by default in usual configurations. Sudo applies its policy, while su - follows the system's su authentication and account policy. A shell is substantially broader than one authorized command.

Run sudo -l to inspect the effective command list for the current user when permitted. For file editing, consider explicitly scoped sudoedit. Verify access through an authorized test while preserving an administrative recovery path.

DAC, permission masks and file attributes

Use ls -ld PATH, stat PATH and getfacl PATH to distinguish actual access from intended access. chown and chgrp change ownership or group where permitted. chmod 0640 report.txt sets read/write for the owner and read for the group. The symbolic command chmod g-w report.txt removes only group write. Directory read lists names, search allows traversal, and creating or deleting an entry normally needs both write and search on its parent.

Special bitTypical Linux effect
SUID, octal 4000, u+sOn an eligible executable, execution may set the effective UID to the file owner. Conditions such as nosuid or no_new_privs can suppress the change. Linux ignores setuid bits on interpreter scripts.
SGID, octal 2000, g+sCan set effective GID on eligible executable files. A directory with SGID can pass its group to newly created entries on common Linux filesystems. New subdirectories can also inherit SGID.
Sticky, octal 1000, +tFurther restricts removal or renaming in a directory to the file owner, directory owner or a suitably privileged process. Other access checks still apply.

For an ext4 lab project directory already owned by group developers, chmod g+s /srv/projects adds group inheritance without reassigning ownership. Verify a newly created object from a member's session. Changing parent group or SGID will not retag existing entries. Inspect unexpected SUID files in an approved scope, for example find /srv/lab -xdev -type f -perm -4000 -print. Verify the owning package and application before removing an unnecessary bit with chmod u-s FILE.

Without a default ACL on the parent, permissions are the requested creation bits with the umask bits cleared. Many tools request 0666 for new files and 0777 for directories. With umask 027, those requests yield 0640 and 0750 respectively. A mask of 001 leaves a 0666 request at 0666 because it only clears other-execute, which was absent. When a parent has a default ACL, the inherited ACL and the requested mode determine the new object's permissions instead.

With filesystem ACL support, setfacl -m u:labreader:r-- report.txt adds a named ACL entry. Check the ACL mask with getfacl report.txt and verify the reader can traverse the parent path. The mask may limit effective group-class access, and changing it may affect other ACL entries.

Inspect flags with lsattr FILE. On supported filesystems, chattr +i sets immutable and chattr +a sets append-only. Flag changes require the relevant privilege and change the permitted operations. Check policy and active writers before changing either flag. A mode change with chmod does not remove these flags.

SELinux and AppArmor

On a RHEL 9 host with SELinux, getenforce reports enforcing, permissive or disabled. Enforcing applies SELinux access decisions. Permissive normally permits operations SELinux would deny and can log the denials. Disabled operation lacks SELinux enforcement and active labeling. setenforce switches between enforcing and permissive when SELinux is active, without changing the boot configuration. Investigate denials while preserving policy. Use the release's documentation for boot-time mode and recovery.

SELinux compares the process domain and the target type under policy. A context such as system_u:object_r:httpd_sys_content_t:s0 includes a type and a level/range in addition to the user and role. Use ls -Z or ps -eZ to inspect the relevant labels. Correlate the denial and application operation with ausearch -m AVC,USER_AVC -ts recent. Audit configuration and policy can affect what is logged.

For a lab Apache site at /srv/labweb, assume policy calls for httpd_sys_content_t, the current type is non-customizable, and no existing local definition matches this path. Establish and apply a persistent path mapping:

semanage fcontext -a -t httpd_sys_content_t '/srv/labweb(/.*)?'
restorecon -nRv /srv/labweb
restorecon -Rv /srv/labweb

Use -m with semanage when modifying an existing matching local definition. The preview shows proposed relabeling. Ordinary restorecon adjusts the type of already labeled files and respects customizable-type rules. It can assign the default context to an unlabeled file. chcon changes the current label directly and does not define a path mapping. A same-filesystem rename can retain the old label. Match the expected mapping and validate both label and service behavior after the change. Container bind labels follow the same principle of checking the actual authorized label and path scope.

getsebool NAME queries a policy boolean. setsebool NAME on changes it at runtime, while -P requests persistence. Review its complete effect before enabling it. Use sealert when available for analysis, and treat audit2allow output as proposed rules for review. First investigate configuration, DAC, labels and existing policy. An observed denial alone does not establish that additional access should be granted.

On Ubuntu systems with AppArmor, inspect loaded profiles with aa-status. Enforce mode applies the profile. Complain mode can log profile violations while allowing otherwise unmatched actions, but explicit deny rules can still be enforced. Use aa-enforce and aa-complain for supported profile changes with an authorized diagnostic plan. Verify the profile applied to the actual program.

Secure remote access and reduce exposed functionality

Check listening services and actual dependencies. Retire unneeded Telnet, FTP and TFTP services where possible, and use SSH or SFTP for authenticated encrypted access and transfers. SFTP uses SSH. Review mount and boot dependencies before disabling an unused filesystem or blocking an unneeded filesystem module. A loaded or built-in driver needs different handling from a loadable unused module. For UEFI Secure Boot, verify the platform and distribution's trust chain, accepted keys and boot state. On Ubuntu it validates components such as the signed bootloader and kernel in the configured chain. It serves a different purpose from data encryption.

In OpenSSH, PermitRootLogin no blocks direct root SSH login. AllowUsers and AllowGroups narrow the permitted subjects. Configure and test key access before changing password-based methods. PubkeyAuthentication yes enables public keys, PasswordAuthentication no disables the password method, and KbdInteractiveAuthentication governs the separate interactive method. Where the intent is explicitly public-key-only, confirm the effective authentication methods and applicable Match blocks. Validate the file with sshd -t and verify a new authorized session. Keep a recovery path during remote configuration.

SSH can forward traffic through encrypted tunnels. Local forwarding opens a listener on the client side, while remote forwarding opens one on the server side. Scope forwarding and listener access to the intended use. X11Forwarding no turns off X11 forwarding on the server, and DisableForwarding yes is a broader policy where supported. ssh-agent provides signing operations for loaded identities to client programs. Restrict agent socket access and forward it only when needed.

Fail2Ban combines a log filter, threshold and action in a jail. On a system with the intended SSH jail enabled, fail2ban-client status sshd shows its status. Check the correct file or journal backend, source-address attribution and action; the ban mechanism depends on the configuration. Prefer local configuration overrides and test how a jail interacts with firewall policy. chroot changes path resolution for a process and its children. It leaves other resources, including open file descriptors, outside that mechanism and should be paired with additional confinement where needed.

Test Your Knowledge

On an enforcing RHEL 9 lab host, Apache’s approved docroot is /srv/labweb. The files have a non-customizable incorrect type and there is no matching local definition. Which sequence adds the persistent mapping and applies the expected Apache content type?

A

chcon -R -t httpd_sys_content_t /srv/labweb

B

Set an HTTP boolean and restart the service

C

restorecon -Rv /srv/labweb

D

semanage fcontext -a -t httpd_sys_content_t '/srv/labweb(/.*)?' followed by restorecon -Rv /srv/labweb

Test Your Knowledge

An ext4 project directory /srv/projects already belongs to group developers. Preserve its current ordinary mode and arrange for new entries to inherit that group. Which command sets the appropriate bit?

A

chmod u+s /srv/projects

B

chmod g+s /srv/projects

C

chmod +t /srv/projects

D

chgrp developers /srv/projects

Test Your Knowledge

Which tool should an administrator use to edit the main sudoers file with its normal locking and syntax checks?

A

nano /etc/sudoers

B

vim /etc/sudoers

C

visudo

D

sudoedit /etc/sudoers

Sections you finish are checked off in the contents.