User Permissions Management in Ubuntu
Managing user permissions is crucial for system security. Ineffective access configuration often makes a system vulnerable. This guide will show you how to secure your cloud server with simple yet effective methods for managing user accounts. It is particularly useful for novice system administrators and other IT professionals.
User permissions in Linux significantly impact system security. Proper configuration makes it harder for attackers to access your system.
Basic User Settings Copy link
Commands can be used to manage system access, creating a set of users who can read, edit, or execute data stored on the server. Ubuntu, as other Linux distributions, uses two basic user units: user and group. Let's see how to create and delete them.
Creating a User Copy link
A user is an individual account capable of executing commands and accessing system data. The simplest way to make a user in Ubuntu is:
sudo adduser usernameThe system will prompt you to set a password.
Blocking and Deleting a User Copy link
To block a user, use:
sudo usermod -L usernameTo unblock the user, replace -L with -U.
To delete a user in Ubuntu:
sudo userdel -r usernameThe -r flag also removes the user’s home directory and all their data, a step that is irreversible. To retain the user’s information, omit the -r flag.
Creating a Group Copy link
A group is a collection of one or more accounts that share access to system data. To create a new group, enter:
sudo addgroup groupnameTo check a user’s group memberships, use:
groups usernameTo add a user to a group in Ubuntu:
sudo usermod -aG groupname usernameHere, -a means "add" and -G specifies the group.
Deleting a Group Copy link
To delete a group:
sudo delgroup groupnameListing All Users and Groups Copy link
To see a list of all system accounts, use:
cat /etc/passwdSimilarly, to see all groups:
cat /etc/groupViewing User Groups and Permissions Copy link
The /etc/group file contains information about all system groups and user memberships. To view all groups a user belongs to:
groups usernameTo view permissions for using sudo commands, check if the user belongs to the sudo group.
Changing User Passwords Copy link
To change a user’s password:
sudo passwd usernameYou will be prompted to enter a new password for the specified account.
Usermod and ID Copy link
Every process in the system is associated with an account identifier, indicating the user who initiated the process. By default, User IDs (UID) from 0 to 999 are reserved for system use, while newly created accounts get IDs starting from 1000. To check a user account’s properties:
grep username /etc/passwdTo change a user’s UID:
usermod -u 2025 usernameTo add a comment to an account:
usermod -c "Comment" usernameTo create and change the home directory:
mkdir -p /catalog1/catalog
usermod -d /catalog1/catalog usernameTo change the login shell:
usermod -s /sbin/nologin usernameSetting the login shell to /sbin/nologin prevents the user from accessing the bash shell.
To set a password expiration date:
usermod -e "YYYY-MM-DD" usernameAfter this date, the user cannot log in.
The sudoers File and Root Permissions Copy link
By default, Ubuntu grants root privileges to users for only 15 minutes to minimize security risks. The sudo command allows users to execute tasks with root privileges.
Granting Root Privileges Copy link
There are two main ways to set root privileges to a user in Ubuntu:
-
Add the user to the
sudogroup, allowing them to execute commands with elevated privileges. -
Edit the
sudoersfile to manually assign privileges.
Editing the sudoers File Copy link
The sudoers file defines who has access to sudo. To edit it safely, use:
sudo visudoThe default contents look like this:
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
root ALL=(ALL:ALL) ALL
%admin ALL=(ALL) ALL
%sudo ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
To add a user with root privileges:
username ALL=(ALL) NOPASSWD:ALLSave the file with CTRL + X, then Y, and ENTER.
Switching to Root User Copy link
To switch to the root user:
sudo suThis combines sudo and su, allowing you to operate as the root user without prefacing each command with sudo.
Creating Aliases Copy link
Create user groups for simplified access management:
User_Alias ADMINS = user1, user2Use these aliases to assign permissions in the sudoers file.
Interactive and Non-Interactive sudo Copy link
Use sudo -i to start a shell with root privileges, useful for executing multiple commands:
sudo -iFile Access Management Copy link
User permissions for directories and files in Ubuntu can be controlled using various commands.
Adding and Removing Permissions Copy link
To add permissions:
chmod +rwx filenameTo remove permissions:
chmod -rwx filenameTo allow execution:
chmod +x filenameTo remove write permissions:
chmod -wx filenameChanging File Ownership and Group Copy link
Change file ownership:
chown username filenameChange ownership recursively:
chown -R username:group /path/to/directoryChange group ownership:
chgrp groupname filenameNumerical Permission Codes Copy link
Permissions can also be set using numerical codes:
-
0 = No permission
-
1 = Execute
-
2 = Write
-
4 = Read
Basically, you add up the numbers depending on what level of permissions you want to grant.
-
0 = no
-
1 = --x
-
2 = -w-
-
3 = -wx
-
4 = r-
-
5 = r-x
-
6 = rw-
-
7 = rwx
Example:
chmod 777 directorynameThis grants everyone permission to read, write, and execute.
chmod 700 filenameThis grants read, write, and execute permissions only to the owner.
Conclusion Copy link
This guide covers user permissions management in Ubuntu and also applies to other Linux systems. By following these steps, you can create users, groups and control access to files and root privileges, enhancing your system's security.
Frequently Asked Questions (FAQ) Copy link
How to check user permissions in Ubuntu? Copy link
To view permissions for files and directories, use the "list long" command:ls -l The output displays a string of characters (e.g., -rwxr-xr-x) on the left side. The first character indicates the type (- for file, d for directory), and the next nine characters represent the Read (r), Write (w), and Execute (x) permissions for the Owner, Group, and Others.
What is chmod 777 in Ubuntu? Copy link
chmod 777 sets the permissions of a file or directory so that everyone (Owner, Group, and Public) has full Read, Write, and Execute access.
-
Warning: This is a major security risk. You should rarely use 777, as it allows any user on the system to modify or delete your files.
What are 755 and 644 permissions? Copy link
These are the standard, secure default permissions for web servers and general usage:
-
755 (Directories & Scripts): The Owner has full control (Read/Write/Execute). The Group and Public can only Read and Execute (access the folder or run the script), but cannot edit or delete it.
-
644 (Standard Files): The Owner can Read and Write. The Group and Public can only Read.
User permissions management in Ubuntu example Here is a common scenario: You want to give a user named "john" ownership of a web folder and ensure only he can edit it, while others can only view it. Copy link
-
Change Owner: sudo chown -R john:www-data /var/www/html/site
-
Set Directory Permissions: sudo find /var/www/html/site -type d -exec chmod 755 {} \;
-
Set File Permissions: sudo find /var/www/html/site -type f -exec chmod 644 {} \;
How do I give a user sudo (admin) permissions? Copy link
Add the user to the sudo group using the usermod command: sudo usermod -aG sudo [username] The user must log out and back in for this change to take effect.
How do I view which groups a user belongs to? Copy link
Simply run the command groups [username]. If you run groupswithout a name, it shows the groups for the current logged-in user.