|

Linux CLI 18 🐧 id, chmod and umask commands

These three commands handle user identity, file permissions, and default permission rules. Together, they form the foundation of Linux access control.


Overview

CommandPurposeBest For
idShow user identityFinding your UID, GID, and groups
chmodChange permissionsSetting who can read/write/execute
umaskSet default permissionsControlling defaults for new files

The id Command

Displays information about a user — their identity and group memberships.

id
id kronos
id -u kronos
id -g kronos
id -n kronos
id -G kronos
CommandDescription
idInfo for the current user
id kronosInfo for a specific user
id -u kronosShow the user ID (UID)
id -g kronosShow the primary group ID (GID)
id -n kronosShow the name instead of numeric ID
id -G kronosShow all groups the user belongs to

Examples

Current user info:

$ id
uid=1000(kronos) gid=1000(users) groups=1000(users),4(adm),27(sudo),1001(developers)

Specific user:

$ id kronos
uid=1000(kronos) gid=1000(users) groups=1000(users),4(adm),27(sudo),1001(developers)

User ID only:

$ id -u kronos
1000

Group ID only:

$ id -g kronos
1000

Group name instead of number:

$ id -n -g kronos
users

All groups (numeric):

$ id -G kronos
1000 4 27 1001

All groups (names):

$ id -Gn kronos
users adm sudo developers

Understanding id Output

uid=1000(kronos) gid=1000(users) groups=1000(users),4(adm),27(sudo),1001(developers)
│              │ │             │ │          │      │       │
│              │ │             │ │          │      │       └── Additional group
│              │ │             │ │          │      └────────── Additional group
│              │ │             │ │          └───────────────── Additional group
│              │ │             │ └──────────────────────────── Primary group name
│              │ │             └────────────────────────────── Primary group ID
│              │ └──────────────────────────────────────────── Username
│              └────────────────────────────────────────────── User ID
└───────────────────────────────────────────────────────────── Identifier type

Key concepts:

TermMeaning
UIDUser ID — a number that identifies a user
GIDGroup ID — a number that identifies a group
Primary groupThe main group a user belongs to
Supplementary groupsAdditional groups a user belongs to

Root User

$ sudo id
uid=0(root) gid=0(root) groups=0(root)
# UID 0 = root (superuser)

Common UIDs:

UIDUserNotes
0rootSuperuser
1–999System usersServices, daemons
1000+Regular usersHuman users

The chmod Command

CHange MODe — changes permissions of a file or directory.

chmod u+rwx,g-wx,o=rx 1.txt
chmod 640 1.txt

Who can use it?

  • The owner of the file
  • The superuser (root)

Two Ways to Change Permissions

MethodExampleStyle
Symbolicchmod u+rwx,g-wx,o=rx fileMore readable
Octalchmod 640 fileMore concise

Method 1: Symbolic Representation

SymbolMeaning
uUser (owner)
gGroup
oOthers
aAll (u+g+o)
+Add permission
-Remove permission
=Set exact permissions
rReadable
wWritable
xExecutable
sSet user ID
SSet group ID

Symbolic Examples

# Add read, write, execute to the owner
chmod u+rwx file.txt

# Remove write and execute from the group
chmod g-wx file.txt

# Set exact permissions for others (read + execute)
chmod o=rx file.txt

# Combine multiple changes
chmod u+rwx,g-wx,o=rx file.txt

# Add execute to everyone
chmod a+x script.sh
# Same as: chmod u+x,g+x,o+x script.sh

# Remove write from group and others
chmod go-w file.txt

# Set specific permissions for owner only
chmod u=rw file.txt

Step-by-Step: chmod u+rwx,g-wx,o=rx 1.txt

Starting state (assume 1.txt has rw-rw-rw-):

ChangeResult
Startrw-rw-rw-
u+rwx — owner gets read+write+executerwxrw-rw-
g-wx — group loses write+executerwxr--rw-
o=rx — others set to read+executerwxr-xr-x

Final: rwxr-xr-x (755)


Method 2: Octal Representation

Three digits, one per group:

640
│││
││└── Others (o)
│└─── Group (g)
└──── Owner (u)
OctalBinarySymbolicMeaning
0000---No permissions
1001--xExecute only
2010-w-Write only
3011-wxWrite + execute
4100r--Read only
5101r-xRead + execute
6110rw-Read + write
7111rwxRead + write + execute

Octal Examples

# Owner: read+write, group: read, others: none
chmod 640 file.txt
# Result: -rw-r-----

# Owner: read+write+execute, group: read+execute, others: read+execute
chmod 755 script.sh
# Result: -rwxr-xr-x

# Owner: read only, group: read, others: read
chmod 444 file.txt
# Result: -r--r--r--

# Owner: read+write+execute, everyone else: nothing
chmod 700 private/
# Result: drwx------

# Owner: read+write, group: read+write, others: read
chmod 664 shared.txt
# Result: -rw-rw-r--

Step-by-Step: chmod 640 1.txt

6    4    0
│    │    │
│    │    └── Others: 0 = --- (no access)
│    └─────── Group:  4 = r-- (read only)
└──────────── Owner:  6 = rw- (read + write)

Result: rw-r----- (640)

chmod Common Patterns

OctalSymbolicUse Case
644rw-r--r--Regular files
755rwxr-xr-xDirectories, executables
600rw-------Private files (SSH keys)
640rw-r-----Shared within group
664rw-rw-r--Group-writable files
700rwx------Private directories
777rwxrwxrwx❌ Avoid (insecure)

Recursive chmod

# Apply to all files and directories in a tree
chmod -R 755 project/

# Apply only to directories
find project/ -type d -exec chmod 755 {} \;

# Apply only to files
find project/ -type f -exec chmod 644 {} \;

The umask Command

User MASK — determines the default permissions applied to new files and directories.

umask
umask 0000
umask
umask 0022
CommandDescription
umaskShow current mask
umask 0000Set no restrictions
umask 0022Set common default

How umask Works

The mask is subtracted from default permissions:

TypeBase PermissionSubtract umaskResult
Files666 (rw-rw-rw-)022644 (rw-r--r--)
Directories777 (rwxrwxrwx)022755 (rwxr-xr-x)

Why base values?

  • Files start at 666 — no execute by default (files aren’t programs)
  • Directories start at 777 — need execute to enter

Examples

Show current mask:

$ umask
0022

Set umask to 0000:

$ umask 0000
$ umask
0000

# Now new files get: 666 - 000 = 666 (rw-rw-rw-)
# New directories:   777 - 000 = 777 (rwxrwxrwx)

Set umask back to 0022:

$ umask 0022
$ umask
0022

# New files get: 666 - 022 = 644 (rw-r--r--)
# New directories: 777 - 022 = 755 (rwxr-xr-x)

Common umask Values

UmaskFilesDirectoriesUse Case
022644755Default — owner writes, others read
002664775Group collaboration
077600700Private — no group/other access
027640750Group read, no other access
000666777❌ Insecure — avoid

Making umask Permanent

Add to ~/.bashrc:

echo "umask 0022" >> ~/.bashrc

Or edit manually:

nano ~/.bashrc
# Add at the end:
umask 0022
# Save: Ctrl+X, then Y, then Enter
source ~/.bashrc

Verify:

$ umask
0022

Testing umask

# Set mask
$ umask 077

# Create a file
$ touch test.txt
$ ls -l test.txt
-rw-------  1 kronos users  0 Jan 15 10:30 test.txt
# Result: 600

# Create a directory
$ mkdir testdir
$ ls -ld testdir
drwx------  2 kronos users  4096 Jan 15 10:30 testdir
# Result: 700

# Reset
$ umask 022

Complete Example Session

# ============================================
# PART 1: ID COMMAND
# ============================================

# Current user
$ id
uid=1000(kronos) gid=1000(users) groups=1000(users),4(adm),27(sudo)

# Specific user
$ id kronos
uid=1000(kronos) gid=1000(users) groups=1000(users),4(adm),27(sudo)

# UID only
$ id -u kronos
1000

# GID only
$ id -g kronos
1000

# Group name
$ id -gn kronos
users

# All groups (names)
$ id -Gn kronos
users adm sudo

# All groups (IDs)
$ id -G kronos
1000 4 27

# Root user
$ sudo id
uid=0(root) gid=0(root) groups=0(root)

# ============================================
# PART 2: CHMOD — SYMBOLIC
# ============================================

# Starting state
$ ls -l 1.txt
-rw-rw-rw-  1 kronos users  1024 Jan 15 10:30 1.txt

# Add execute to owner
$ chmod u+x 1.txt
$ ls -l 1.txt
-rwxrw-rw-  1 kronos users  1024 Jan 15 10:30 1.txt

# Remove write from group
$ chmod g-w 1.txt
$ ls -l 1.txt
-rwxr--rw-  1 kronos users  1024 Jan 15 10:30 1.txt

# Set others to read+execute
$ chmod o=rx 1.txt
$ ls -l 1.txt
-rwxr--r-x  1 kronos users  1024 Jan 15 10:30 1.txt

# Combined command (from rw-rw-rw-)
$ chmod u+rwx,g-wx,o=rx 1.txt
$ ls -l 1.txt
-rwxr-xr-x  1 kronos users  1024 Jan 15 10:30 1.txt

# ============================================
# PART 3: CHMOD — OCTAL
# ============================================

# Set permissions to 640
$ chmod 640 1.txt
$ ls -l 1.txt
-rw-r-----  1 kronos users  1024 Jan 15 10:30 1.txt

# Set to 755 (common)
$ chmod 755 1.txt
$ ls -l 1.txt
-rwxr-xr-x  1 kronos users  1024 Jan 15 10:30 1.txt

# Set to 644 (default for files)
$ chmod 644 1.txt
$ ls -l 1.txt
-rw-r--r--  1 kronos users  1024 Jan 15 10:30 1.txt

# Set to 600 (private)
$ chmod 600 1.txt
$ ls -l 1.txt
-rw-------  1 kronos users  1024 Jan 15 10:30 1.txt

# Recursive
$ chmod -R 755 project/

# ============================================
# PART 4: UMASK
# ============================================

# Show current mask
$ umask
0022

# Set to 0000
$ umask 0000
$ umask
0000

# Create a file — see result
$ touch test.txt
$ ls -l test.txt
-rw-rw-rw-  1 kronos users  0 Jan 15 10:30 test.txt
# 666 - 000 = 666

# Create a directory — see result
$ mkdir testdir
$ ls -ld testdir
drwxrwxrwx  2 kronos users  4096 Jan 15 10:30 testdir
# 777 - 000 = 777

# Set to 0077 (private)
$ umask 0077

# Create a file
$ touch private.txt
$ ls -l private.txt
-rw-------  1 kronos users  0 Jan 15 10:30 private.txt
# 666 - 077 = 600

# Create a directory
$ mkdir privatedir
$ ls -ld privatedir
drwx------  2 kronos users  4096 Jan 15 10:30 privatedir
# 777 - 077 = 700

# Reset to default
$ umask 0022
$ umask
0022

# Make permanent
$ echo "umask 0022" >> ~/.bashrc
$ source ~/.bashrc

# ============================================
# PART 5: PRACTICAL SCENARIOS
# ============================================

# Scenario 1: Setup a web directory
$ mkdir website
$ chmod 755 website
$ cd website
$ touch index.html
$ chmod 644 index.html

# Scenario 2: Protect a private key
$ chmod 600 ~/.ssh/id_rsa
$ ls -l ~/.ssh/id_rsa
-rw-------  1 kronos users  1675 Jan 15 10:30 id_rsa
# SSH requires this permission!

# Scenario 3: Share a file with your group
$ chmod 640 shared.txt
$ ls -l shared.txt
-rw-r-----  1 kronos users  1024 Jan 15 10:30 shared.txt
# Owner can read+write, group can read

# Scenario 4: Group collaboration
$ umask 002
$ touch team.txt
$ ls -l team.txt
-rw-rw-r--  1 kronos users  0 Jan 15 10:30 team.txt
# Both owner and group can write

# Scenario 5: Set restrictive default for new files
$ umask 0077
$ touch secret.txt
$ ls -l secret.txt
-rw-------  1 kronos users  0 Jan 15 10:30 secret.txt
# Only owner can access

Quick Reference

id Options

OptionDescription
-uUser ID
-gPrimary group ID
-GAll group IDs
-nShow names instead of numbers
-rShow real ID (vs effective)

chmod Symbolic

SymbolMeaning
uOwner (user)
gGroup
oOthers
aAll
+Add
-Remove
=Set exactly
rRead
wWrite
xExecute

chmod Octal

OctalSymbolicMeaning
0---None
1--xExecute
2-w-Write
3-wxWrite + execute
4r--Read
5r-xRead + execute
6rw-Read + write
7rwxFull

umask Common Values

UmaskFilesDirsUse Case
022644755Default
002664775Group collaboration
077600700Private
027640750Semi-private

Best Practices

Do This:

# Files — 644
chmod 644 file.txt

# Directories — 755
chmod 755 mydir

# Private files — 600
chmod 600 ~/.ssh/id_rsa
chmod 600 .env

# Executables — 755
chmod 755 script.sh

# Private directory — 700
chmod 700 ~/.ssh

# Group share — 640 or 664
chmod 640 shared.txt

# Set umask in ~/.bashrc for consistency
echo "umask 022" >> ~/.bashrc

# Verify with id
id
id -Gn

Don’t Do This:

# Don't use 777
chmod 777 file.txt          # ❌ Security risk
chmod -R 777 /var/www       # ❌ Very dangerous!

# Don't remove execute from directories
chmod -x mydir              # ❌ Can't cd in!

# Don't forget SSH key permissions
chmod 644 ~/.ssh/id_rsa     # ❌ SSH refuses to use it
chmod 600 ~/.ssh/id_rsa     # ✅ Required

# Don't set umask without understanding
umask 0000                  # ❌ Everyone can read/write new files

# Don't forget to source after changing .bashrc
nano ~/.bashrc
# ... add umask ...
source ~/.bashrc            # ✅ Required to apply

Common Pitfalls

PitfallProblemSolution
SSH key too permissiveSSH refuseschmod 600 ~/.ssh/id_rsa
777 on web filesSecurity hole644 files, 755 dirs
Removed x from directoryCan’t cd inAdd x back
umask not appliedForgot to sourcesource ~/.bashrc
Wrong id flagUnexpected outputCheck id --help
Chmod without permissionOwner or root onlysudo chmod ...

Real-World Examples

1. Setting Up an SSH Key

# Generate key
$ ssh-keygen -t rsa

# Fix permissions
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_rsa
$ chmod 644 ~/.ssh/id_rsa.pub
$ chmod 644 ~/.ssh/authorized_keys

2. Web Server Setup

# Directory
$ mkdir /var/www/mysite
$ chmod 755 /var/www/mysite

# Files
$ touch /var/www/mysite/index.html
$ chmod 644 /var/www/mysite/index.html

# Uploads directory (needs write)
$ mkdir /var/www/mysite/uploads
$ chmod 775 /var/www/mysite/uploads

3. Shared Team Directory

# Set group ownership
$ sudo chgrp developers /shared/project
$ chmod 775 /shared/project

# Set setgid so new files inherit group
$ chmod g+s /shared/project

# Set umask for team
$ echo "umask 002" >> ~/.bashrc

4. Private Workspace

# Restrict everything
$ umask 077
$ mkdir private
$ touch secret.txt
$ ls -la
drwx------  2 kronos users  4096  private
-rw-------  1 kronos users    0  secret.txt

5. Check Current Identity

$ id
uid=1000(kronos) gid=1000(users) groups=...

$ id -Gn
users adm sudo developers
# Shows all groups — useful for permission debugging

Visual: How umask Works

Files:  666 - umask = result
        666 - 022   = 644

Directories:  777 - umask = result
              777 - 022   = 755

umask 022:
    666 (rw-rw-rw-)      777 (rwxrwxrwx)
  - 022 (----w--w-)    - 022 (----w--w-)
  ────────────────     ────────────────
  = 644 (rw-r--r--)    = 755 (rwxr-xr-x)

umask 077:
    666 (rw-rw-rw-)      777 (rwxrwxrwx)
  - 077 (---rwxrwx)    - 077 (---rwxrwx)
  ────────────────     ────────────────
  = 600 (rw-------)    = 700 (rwx------)

Summary

CommandPurposeExample
idShow user identityid -Gn
chmodChange permissionschmod 644 file.txt
umaskSet default permissionsumask 022

Key takeaways:

  • id shows UID, GID, and all groups — use -u, -g, -G, -n for specific info
  • chmod changes permissions in two ways:
    • Symbolicchmod u+rwx,g-wx,o=rx file (readable)
    • Octalchmod 640 file (concise)
  • Octal values: 4=read, 2=write, 1=execute (add them up per group)
  • umask sets default permissions for new files/directories
  • Files start at 666, directories at 777umask is subtracted
  • Common umask: 022 → files 644, dirs 755
  • Make umask permanent in ~/.bashrc

Remember: These three commands are the access control trio:

  • id — “Who am I?”
  • chmod — “What can I do?”
  • umask — “What will new files look like?”

Use chmod 644 for files, chmod 755 for directories, and chmod 600 for secrets. Set your umask in ~/.bashrc so every new file has safe defaults. And never, ever use chmod 777 — it’s the fast track to a security breach!


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!