Kahibaro
Discord Login Register

Users, Permissions, and Ownership

Understanding Users in Linux

Linux is a multi-user system: many users can exist (and even be logged in) on the same machine at the same time. Understanding users is the foundation for understanding permissions and ownership.

User Accounts and IDs

Internally, Linux does not work with your username directly; it works with numeric IDs.

Viewing Your User Information

Common commands:

  $ whoami
  alice
  $ id
  uid=1000(alice) gid=1000(alice) groups=1000(alice),27(sudo)
  $ groups
  alice sudo

You rarely need to think about your numeric UID directly, but it’s useful to recognize special ones:

Root vs Regular Users

You can check if you are root with:

$ id -u
0   # you are root

or:

$ whoami
root

Ownership of Files and Directories

Every file and directory in Linux has:

  1. An owner user
  2. An owner group
  3. A set of permission bits that apply differently to:
    • the owner user
    • the owner group
    • everyone else (others)

You can see this information using ls -l.

$ ls -l
-rw-r--r-- 1 alice alice  1200 Dec 12 10:23 notes.txt
drwxr-xr-x 2 alice staff  4096 Dec 12 11:00 projects

Breakdown of the first file line:

For the projects directory:

The owner and group determine which permission set will be applied when a file is accessed.

Permission Bits: Read, Write, Execute

Linux permissions are represented with three basic flags:

And they apply to three categories:

  1. User (often abbreviated u) — the owner of the file
  2. Group (often abbreviated g) — members of the file’s group
  3. Others (often abbreviated o) — everyone else

So a typical 10-character permission string:

Another example, for a directory:

Meaning of r, w, x for Files vs Directories

For regular files:

For directories:

A common pattern for shared directories:

Numeric (Octal) Permission Notation

Permissions can also be represented using numbers, where:

For each of user/group/others, you add the values:

So a full permission like:

You’ll use this numeric form frequently with chmod.

Changing Permissions with chmod

chmod (change mode) modifies the permission bits on files and directories.

There are two main ways to use it:

  1. Symbolic mode (u, g, o, a, +, -, = with r, w, x)
  2. Numeric (octal) mode (chmod 755 file)

Symbolic Mode

Basic structure:

Where:

Examples:

# Add execute for user (owner)
chmod u+x script.sh
# Remove write for others
chmod o-w file.txt
# Give read+write to group
chmod g+rw shared.txt
# Remove all permissions from others
chmod o-rwx secret.txt
# Set exact permissions: user=rw, group=r, others=r
chmod u=rw,g=r,o=r document.txt

You can combine multiple operations:

chmod u+x,g-w script.sh

Numeric Mode

When you already know the desired numeric permissions:

# user: rwx, group: r-x, others: r-x
chmod 755 script.sh
# user: rw-, group: r--, others: r--
chmod 644 document.txt
# user: rwx, group: ---, others: ---
chmod 700 secret

Common patterns (for regular files):

For directories, similar numeric values, but you typically need x to enter them:

Recursive Changes

To change permissions for a directory and everything inside it:

# Apply 755 to directory and all its contents
chmod -R 755 mydir

Be very cautious with -R (recursive). It will affect all nested files and subdirectories, which may break things if misused (e.g. on /, /etc, or your entire home directory).

File Ownership: chown and chgrp

Permissions are evaluated based on who owns the file and which group it belongs to. Sometimes you need to change these.

Changing Owner with chown

chown (change owner) sets the owner and optionally the group of a file.

Basic forms:

Examples:

# Change owner to 'alice'
sudo chown alice report.txt
# Change owner to 'alice' and group to 'staff'
sudo chown alice:staff project/
# Change group only to 'www-data'
sudo chown :www-data /var/www/html/index.html

You usually need to be root (or use sudo) to change ownership to another user.

Recursive Ownership Changes

Like chmod, chown also supports recursion:

# Make 'alice' the owner of everything in /srv/project
sudo chown -R alice:staff /srv/project

Again, be very careful with -R; changing ownership system-wide can break services.

Changing Group with chgrp

chgrp just changes the group owner, leaving the user unchanged.

# Change group of file.txt to 'staff'
chgrp staff file.txt
# Change group of directory and all contents
sudo chgrp -R www-data /var/www/html

You can change a file’s group if:

How Linux Decides Which Permissions Apply

When a process (run by some user) tries to access a file, Linux checks permissions in a specific order:

  1. Is the process’s UID the same as the file’s owner UID?
    • If yes, apply the user permission bits.
  2. Else, is the process in the file’s group?
    • If yes, apply the group permission bits.
  3. Else:
    • Apply the others permission bits.

Only one of these three sets is used for a given access check.

Example

File:

-rw-r----- 1 alice staff  4096 Dec 12 10:00 report.txt

Users:

So:

Practical Permission Scenarios

Making a Script Executable

You have a shell script backup.sh:

$ ls -l backup.sh
-rw-r--r-- 1 alice alice  512 Dec 12 12:00 backup.sh

It’s not executable (x is missing). To run it directly:

chmod u+x backup.sh
./backup.sh

Or to make it executable by everyone:

chmod 755 backup.sh

Creating Private Files and Directories

For a private notes file:

touch secrets.txt
chmod 600 secrets.txt   # rw-------, only owner can read/write

For a private directory:

mkdir private
chmod 700 private       # rwx------, only owner can enter/list/use

Shared Project Directory

Suppose you want a group-based shared directory for multiple users in group project.

Basic steps (high-level):

  1. Ensure group exists and users are members (covered in the dedicated user/group chapter).
  2. Create the directory and set group:
   sudo mkdir /srv/project
   sudo chown root:project /srv/project
  1. Set permissions so group can read/write:
   sudo chmod 770 /srv/project   # rwxrwx---

Now:

(For more advanced shared-directory behavior, like preserving group ownership on new files, special bits like setgid are used, which are typically covered in more advanced chapters.)

Quick Reference

Views: 23

Comments

Please login to add a comment.

Don't have an account? Register now!