Skip to content

Security Operations / Host hardening · Step 3 of 10

Lesson: Least PrivilegeSwitch
← Security Operations

Security investigations

Least Privilege

Give users and programs only the access their task needs.

Least privilege means each person or program gets the access needed for the job, and no more. Mistakes and break-ins then do less harm.

Check what you have

$ id
uid=1000(maria) gid=1000(maria) groups=1000(maria),27(sudo)
$ sudo -l
User maria may run the following commands on web1:
    (ALL : ALL) ALL

sudo -l lists what you may run. This user has full root power. Admin groups are usually sudo on Debian and Ubuntu and wheel on Fedora and RHEL.

Grant narrow sudo rules

Suppose an operator only needs to restart one web server. Edit a separate rule file with visudo, which checks syntax before saving:

$ sudo visudo -f /etc/sudoers.d/web-operator
sam ALL=(root) /usr/bin/systemctl restart nginx.service

Give full paths and exact arguments. Confirm the path with command -v systemctl. Be careful with programs that can start a shell or edit any file, such as editors, pagers and interpreters. Allowing them with sudo can give full root. Wildcards are also easy to get wrong.

Limit file access

Use groups to share files, not world write permission.

$ sudo chgrp webteam /srv/site/index.html
$ sudo chmod 640 /srv/site/index.html
$ ls -l /srv/site/index.html
-rw-r----- 1 root webteam 512 Mar  9 14:00 /srv/site/index.html

640 lets the owner read and write, the group read, and others do nothing. Never use chmod 777 to make an error go away. Find which user needs which permission. Directories along the path need execute permission for access.

New group membership applies at the next login.

Review

Access tends to pile up. Check admin group members with getent group sudo (or wheel) and remove what is no longer needed. Keep a way back in before removing your own rights. Use a normal account for daily work and raise privileges only for the command that needs it.

Try it

In a test environment, create a file, run chmod 600, then chmod 640, and compare ls -l. Run sudo -l and explain each line. Practise in the permissions lab.

Check your understanding

Least Privilege

All 3 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.