Unix - User & Group Management in Unix
User & Group Management in Unix
Unix is a multi-user system, meaning multiple users can log in and work simultaneously. Each user has:
-
A username (e.g.,
alice
) -
A UID (user ID, a unique number)
-
A primary group and possibly supplementary groups
-
A home directory and shell
File access is controlled through users, groups, and permissions.
1. useradd – Add a New User
Creates a new user account.
sudo useradd alice # Add user "alice"
sudo useradd -m bob # Add "bob" and create home directory
sudo useradd -s /bin/bash charlie # Set shell for "charlie"
Check user details:
id alice
2. passwd – Set or Change Password
Assigns or changes a user’s password.
sudo passwd alice
It will prompt for a new password.
Users can change their own password with just:
passwd
3. groups – Show Group Membership
Lists groups a user belongs to.
groups # Show current user’s groups
groups alice # Show groups of "alice"
To add a user to a group:
sudo usermod -aG developers alice
4. chmod – Change File Permissions
Every file/directory has permissions for:
-
User (owner)
-
Group
-
Others
Permissions:
-
r
= read -
w
= write -
x
= execute
Numeric Mode
-
r=4
,w=2
,x=1
-
Add them up for each level.
chmod 644 file.txt # rw- r-- r-- (owner can read/write, others can only read)
chmod 755 script.sh # rwx r-x r-x (owner can do everything, others can read/execute)
Symbolic Mode
chmod u+x script.sh # Give execute to user (owner)
chmod g-w file.txt # Remove write for group
chmod o+r file.txt # Give read to others
5. chown – Change Ownership
Used to change file/directory owner and/or group.
sudo chown alice file.txt # Make alice the owner
sudo chown alice:developers file.txt # Owner = alice, Group = developers
6. Putting It Together
Example workflow:
# Create a new user
sudo useradd -m devuser
# Set password
sudo passwd devuser
# Add to group
sudo usermod -aG sudo devuser
# Give permissions
chmod 750 project.sh # Owner: rwx, Group: r-x, Others: ---
chown devuser:developers project.sh
Summary
-
useradd
→ create users -
passwd
→ set/change passwords -
groups
→ check group membership -
chmod
→ change file permissions -
chown
→ change file/directory ownership