WIT LAB INC Blog

“ To Impress Others By Works"

Linux User and Permission Management

Linux User and Permission Management

Linux user administration is essential for system administrators to manage user accounts, groups, and permissions. Below is a detailed guide on commands and processes for managing users and groups, along with examples to help you get started.

Commands Explained

adduser
The adduser command creates a new user with a home directory and default settings.
Syntax: adduser [username]
Example: adduser john

addgroup
The addgroup command is used to create a new group.
Syntax: addgroup [groupname]
Example: addgroup developers

usermod / gpasswd

usermod: Modifies user accounts, such as adding users to a group.
Syntax: usermod -aG [groupname] [username]
Example: usermod -aG developers john

gpasswd: Administers groups by adding or removing users.
Syntax: gpasswd -a [username] [groupname]
Example: gpasswd -a john developers

chown
The chown command is used to change the ownership of files or directories.
Syntax: chown [owner]:[group] [file/folder]
Example: chown john:developers /var/www/project

chmod
The chmod command is used to change the permissions of files or directories.
Syntax: chmod [permissions] [file/folder]
Example: chmod 770 /var/www/project

Commands Explained
1. Create a New User
To create a user with a disabled password (useful for specific system configurations):

sudo adduser username --disabled-password

2. Check the User List
To view all existing users:

cat /etc/passwd

Group Management
1. Create a New Group
To create a new group:

sudo addgroup groupname

2. Check the Group List
To list all groups:

cat /etc/group

3. Add a User to a Group
You can add a user to a group using:

sudo usermod -aG groupname username
sudo gpasswd -a username groupname

Note:
-a: Appends the user to the group without removing them from other groups.
-G: Specifies the groups to add.

4. Check User's Groups
To check which groups a user belongs to:

groups username

File and Folder Management
1. Create a New Folder
To create a folder at a specified path:

sudo mkdir /path/to/folder_name

2. Change Ownership
Change owner and group:

sudo chown username:groupname /path/to/folder

Change only the owner:

sudo chown username /path/to/file

Change only the group:

sudo chown :groupname /path/to/folder

3. Set Group Permissions
To allow the group to read, write, and execute (recursively on subfolders and files):

sudo chmod -R g+rwx /path/to/folder

4. Set Group Inheritance with setgid
To ensure new files in a folder inherit the group:

sudo chmod g+s /path/to/folder

Understanding Linux Permissions
Permissions are structured for: owner/group/others.
User symbols to modify:

• u: Owner
• g: Group
• o: Others
• a: All (owner, group, and others)

Octal Representation
Permission values:

• 4: Read (r)
• 2: Write (w)
•1: Execute (x)
• 0: No permission

Example

• 7 (4+2+1): Read, write, execute
• 6 (4+2): Read, write
• 5 (4+1): Read, execute
• 4: Read-only

Change File/Folder Permissions
To set permissions:

sudo chmod options permission /path/to/file_or_directory

Example: Full permission for the owner, read and execute for the group, none for others:

sudo chmod 750 /path/to/file

To recursively set permissions:

sudo chmod -R 750 /path/to/folder

To check the list file or folder permissions:

ls -l
drwxr-xr--  2 aung aung  4096 Dec 30 20:51 testing_one

drwxr-xr--:

• d: Directory
• rwx: Owner has read, write, execute permissions
• r-x: Group has read, execute permissions
• r--: Others have read-only permission

Mastering these commands and permission structures will help you manage Linux environments efficiently while ensuring security and proper access controls. Whether you're managing servers, deploying applications, or organizing files, these tools are indispensable for a developer or system administrator.

Page Top