Mastering Linux: Control Access With User Groups & Permissions

Overseeing users, groups, and permissions is essential for upholding system security and guaranteeing that only authorized personnel can access designated resources. This article outlines the fundamentals of this management process and provides guidance on how to effectively implement it for your Linux distribution.

User Accounts
  • A user account is a unique identity for a user on the system
  • Users can be created, deleted, and modified using the useradd and userdel commands
  • Passwords are set with the passwd command

Example:

Create new user called user1

Set initial password for user1

Delete user1

Groups
  • A group is a collection of users who share common access
  • Users can be added to or removed from groups using the usermod and gpasswd commands

Example:

Add user1 to group1

Remove user1 from group1

Permissions
Listing file permissions

Permissions can be viewed using “ls” with this syntax

Common Options:

  • l: list files in long format
  • a: list all files and folder, including hidden items

Examples:

List all files and folders in long format

List details of file1.txt in long format

Terminal output will be formatted like this:

This output is interpreted as:

  • file1.txt is not a directory
  • user1 can read and write to file1.txt
  • group1 can read and write to file1.txt
  • other users and groups can read file1.txt
Permissions explained:
d = directory
rwx = read, write, execute
The three groups of rwx are for user, group and other respectively.
  • User : Ownership (who has access)
  • Group : Shared access with others in the same group
  • Other : Access from outside the group
Changing Permissions

Permissions are set using the chmod command. Use chmod with the following syntax to change the permissions of a file or directory:

Common Options:

  • R : Set permissions recursively

Permissions:

  • (u)ser
  • (g)roup
  • (o)ther
  • (a)ll users and groups.
  • (+)Add
  • (-)Remove
  • (r)ead,
  • (w)rite
  • e(x)ecute

Examples:

Add execute permissions to file.txt for user

Add read and remove write and execute permissions on file.txt for group and others

Remove write permissions for all items in a directory for everyone

Changing File Ownership

The chown command is used to change the ownership of a file or directory. It allows you to specify both the new owner and group IDs using the following syntax:

Options:

  • R : Recursively apply changes to all files in a directory
  • h : Change the owner of a symbolic link
  • –reference : Change the owner of a file/directory to match a reference file

Examples:

Change ownership of file.txt to owner user1 and group1

Change the ownership of a symbolic link to owner user1 and group1

Change ownership of file1.txt to match file2.txt

Closing Remarks

The key principle to keep in mind when setting permissions is the “Least Privilege” principle:

“Deny by Default, Grant as Necessary”

This means that unless explicitly allowed, a user or group should not have access to a resource or file. Instead, only grant necessary access on a need-to-know basis.

By doing so, you minimize the attack surface and reduce the risk of unauthorized access or malicious activity.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.