OS: Linux Users, Groups, and Permissions

Linux is a multi-user operating system, meaning multiple users can share the same machine, each with their own files and permissions. Managing users is essential for both desktop and server systems.

User Management (adduser, passwd, su, sudo)

 `adduser` – Create a New User

The adduser command creates a new user account along with a home directory.

  • Example:

bash
sudo adduser amr

This creates user amr with /home/amr as the home directory.

It will:

  • ask for password
  • create home directory
  • copy default config files
  • set shell
  • adduser → Debian/Ubuntu friendly helper script
  • useradd → Low-level Linux command (works on most distros)

Key Differences

Featureadduseruseradd
TypeHigh-level interactive scriptLow-level core command
Default shell setupYesNo (unless you specify flags)
Creates home dir automaticallyYesNo (unless -m is used)
Password promptYesNo (use passwd <user> after)
User-friendlyVeryLess, needs options
DistroMainly Debian/UbuntuAll Linux

Using useradd (manual):

bash
sudo useradd -m -s /bin/bash amr
sudo passwd amr

Flags explained:

  • -m → create home directory
  • -s /bin/bash → set default shell

Summary

  • If you're on Ubuntu/Debian → use adduser (simpler, interactive)
  • If writing scripts or working on any distro → use useradd with options

If you want to modify (edit) an existing user, the real command is:

`usermod` → edits user properties

Examples:

bash
sudo usermod -l newname oldname        # change username
sudo usermod -d /new/home -m amr       # change home directory and move files
sudo usermod -s /bin/zsh amr           # change default shell
sudo usermod -aG sudo amr              # add user to sudo group (append)
sudo usermod -G group1,group2 amr      # set groups exactly (overwrite group list!)
sudo usermod -c "Software Engineer" amr # change user comment/description

Change password:

bash
sudo passwd amr

Change user info interactively (Debian/Ubuntu only):

bash
sudo chfn amr

This lets you edit:

  • Full name
  • Room number
  • Work phone
  • Home phone
  • Other info

 `passwd` – Change User Password

The passwd command sets or updates a user’s password.

  • For your own password:

passwd

  • For another user (requires root privileges):

sudo passwd amr


 `su` – Switch User

The su (substitute user) command allows you to switch to another user account in the current session.

  • Switch to root (superuser):

su

(then enter root password)

  • Switch to a specific user:

su amr

When finished, type exit to return to your original account.


 `sudo` – Run Commands as Superuser

The sudo command lets authorized users run commands as the root user without fully switching accounts.

  • Example:

sudo apt update

Runs the command with administrative privileges.

  • First time, you may need to add your user to the sudo group:

sudo usermod -aG sudo amr

  • This is safer than su, because:
  • You stay as your normal user.
  • Only specific commands get root privileges.
  • Every sudo action is logged.

Summary

  • adduser → create new users.
  • passwd → set or change passwords.
  • su → switch users (or to root).
  • sudo → run specific commands as superuser (recommended).

File Permissions (chmod, chown)

Every file and directory in Linux has permissions that control who can read, write, or execute it. Understanding and managing permissions is essential for security and collaboration.


Types of Permissions

Each file or directory has three basic permissions:

  • r (read) → view the contents of a file, or list a directory.
  • w (write) → modify a file, or create/delete inside a directory.
  • x (execute) → run a file as a program/script, or enter a directory.

These apply to three categories of users:

  • Owner (u) → the user who owns the file.
  • Group (g) → other users in the file’s group.
  • Others (o) → everyone else.

Viewing Permissions

Usels -lto see permissions:

bash
$ ls -l notes.txt
      -rw-r--r-- 1 amr amr 1200 Sep 25 notes.txt

  • -rw-r--r-- → file type and permissions.
  • rw- → owner (read, write).
  • r-- → group (read only).
  • r-- → others (read only).
  • amr amr → owner and group.

 `chmod` – Change Permissions

chmod changes file or directory permissions.

Symbolic mode:

  • Add execute for owner: chmod u+x script.sh
  • Remove write for group: chmod g-w notes.txt
  • Give everyone read access: chmod a+r report.pdf

Numeric mode (octal):

  • Permissions are represented as numbers:
  • r=4w=2x=1.
  • Example:
  • chmod 755 script.sh

  • Owner: 7 (rwx).
  • Group: 5 (r-x).
  • Others: 5 (r-x).

 `chown` – Change Ownership

chown changes the owner or group of a file.

  • Change owner:

sudo chown amr notes.txt

  • Change owner and group:

sudo chown amr:devteam project/


Summary

  • ls -l → view permissions and ownership.
  • chmod → modify permissions (symbolic or numeric).
  • chown → change file owner or group.
Correct permissions keep your system **secure** and **organized**.

Understanding Root vs Normal Users

Linux is built as a multi-user system, where different users have different levels of control. The two main types are root (superuser) and normal users. Knowing the difference is crucial for both security and productivity.


Normal Users

  • Created using adduser or during system setup.
  • Each has a home directory (/home/username).
  • Can:
  • Create and edit files in their own home directory.
  • Run applications.
  • Use most commands.
  • Cannot:
  • Change system files (in /etc/bin/usr, etc.).
  • Install or remove software (unless granted sudo).
  • Manage other users.

Normal users are restricted for safety — preventing accidental system damage.


Root User (Superuser)

  • Special user with UID 0.
  • Has full control over the system.
  • Can:
  • Install/remove software.
  • Edit any file, including system-critical ones.
  • Manage all users and permissions.
  • Start/stop services.
  • Dangerous if misused:
  • A single wrong command (like rm -rf /) can break the system.
  • Root access should be used only when necessary.

Using `sudo` Instead of Root Login

Modern Linux systems encourage using sudo instead of logging in as root.

  • Safer, because you only elevate privileges when needed.
  • Logs every privileged action for auditing.
  • Example:
  • sudo apt install vim


Summary

  • Normal users → limited privileges, safe for daily work.
  • Root user → unlimited control, dangerous if careless.
  • Best practice → work as a normal user, use sudo only when required.