Sub topic 17
File Permissions and System Maintenance Checks
Assume you can use sudo, read Linux permission strings, and work on a test server. Before changing permissions, record the current state and confirm which application owns each file.
File permissions and system maintenance checks
Linux permissions control access for the file owner, the owning group, and everyone else. A mode such as 640 gives the owner read/write access, the group read access, and everyone else no access. Ownership and special bits also matter. The set-user-ID bit, for example, runs a program with the file owner's privileges.
CIS checks commonly include these conditions:
| Check | Why it matters | Example |
|---|---|---|
| Sensitive files have restricted modes | Prevents unauthorized disclosure or changes | `/etc/shadow` should not be readable by ordinary users |
| Files have valid owners and groups | Prevents orphaned access control | No unknown user owns a system file |
| Unneeded SUID or SGID files are removed | Limits privilege escalation paths | Review unusual privileged executables |
| World-writable directories are controlled | Prevents tampering with shared content | `/tmp` should use a sticky bit |
Start with an audit rather than a recursive permission change:
# Review sensitive files and their numeric modes
sudo stat -c '%A %a %U:%G %n' /etc/passwd /etc/shadow /etc/group /etc/sudoers
# Find world-writable files on local filesystems, excluding virtual mounts
sudo find / -xdev -type f -perm /0002 -printf '%m %u:%g %p\n' 2>/dev/null
# Find files whose user or group no longer exists
sudo find / -xdev \( -nouser -o -nogroup \) -ls 2>/dev/null
# List SUID and SGID files for review
sudo find / -xdev \( -perm -4000 -o -perm -2000 \) -type f -ls 2>/dev/nullDo not automatically remove every match. Some files are required by installed packages. Compare findings with the server role, package records, and your approved baseline.
Package verification can detect changed files. On an RPM-based system, rpm -Va reports differences in size, digest, permissions, ownership, and other attributes:
# Verify installed RPM files. Expect output only for differences.
sudo rpm -VaAutomating remediation and detecting drift
Automation should make approved changes repeatable and should report unexpected changes. A safe pattern is:
- Scan and save results.
- Review exceptions.
- Apply narrowly scoped remediation.
- Scan again.
- Alert when later scans differ from the approved baseline.
A file-integrity tool such as AIDE stores cryptographic checksums and metadata for selected paths. It detects change; it does not decide whether the change was authorized.
flowchart TD
A[Run permission and integrity scan] --> B{Expected change?}
B -->|Yes| C[Record change and update baseline]
B -->|No| D[Investigate owner, process, and account]
D --> E[Remediate or approve exception]
C --> F[Schedule the next scan]
E --> F
F --> Acaption: Permission and drift-management cycle alt: A flowchart showing scans leading to review, investigation, baseline updates, and scheduled rescans.
Use configuration management to enforce only known values. For example, an Ansible task can set a specific file mode without changing unrelated files:
- name: Protect the shadow password database
ansible.builtin.file:
path: /etc/shadow
owner: root
group: root
mode: "0000"Check the distribution's default before applying a rule. A remediation that satisfies one benchmark can break authentication or package management on another system.
Exercise
A scan finds a world-writable configuration file owned by an installed package. What should you do first?
Show answer
Confirm the package and service that use it, compare the mode with the approved baseline, inspect recent changes, and test a narrowly scoped correction before enforcing it automatically.