Linux Administration

Linux Users and Permissions — id, chmod, chown, and sudo

Every file on a Linux system has an owner, a group, and nine permission bits that say who can read it, write to it, and run it. Four commands cover the whole model. The id command shows which user and groups a shell runs as. The chmod command changes the bits. The chown command changes the owner. And sudo is how an ordinary user runs one of those commands with the rights of the root user. This guide works through each command on files the current user owns, so every example runs without root, and it shows the two failures a beginner meets first: a directory that lists its files but will not open them, and a chown that the system refuses.

Written November 12, 2026 · 12 min read

By the end of this guide
  • The main sequence: identity, then bits
  • id: who the shell runs as
  • umask: where new files start
  • Reading the bits: octal and letters

Written November 12, 2026

Tested with GNU coreutils 9.4

What this guide is

A Linux system decides who may touch a file from three facts stored with the file itself: the user who owns it, the group it belongs to, and a set of nine permission bits. The bits come in three groups of three. One group applies to the owner, one to members of the file's group, and one to everyone else. Within each group, one bit allows reading, one allows writing, and one allows execution. Every worked example in this guide runs on files owned by the current user, in the directory the commands run in, so none of them needs root. The names in the example output are the current user's name and group; on your system your own names appear in their place.

The main sequence: identity, then bits

Bash
#!/bin/bash
id -un
id -gn
umask
touch report.txt
chmod 640 report.txt
stat -c '%a %U %G' report.txt
ls -l report.txt
chmod 600 report.txt
stat -c '%a' report.txt

The exact output:

Output
alex
alex
0002
640 alex alex
-rw-r----- 1 alex alex 0 Nov 12 04:20 report.txt
600

The first three lines answer three identity questions. The current user is alex, the primary group is also alex, and the umask is 0002. The chmod command then sets the new file's mode to 640, and stat reports that mode back along with the owner and group names. The ls line shows the same mode in its letter form, which the next sections decode. A second chmod tightens the file to 600, and stat confirms the change. The sections below take each part in turn.

id: who the shell runs as

The id command prints the real user and group of the shell that runs it:

Bash
#!/bin/bash
id

The exact output:

Output
uid=1000(alex) gid=1000(alex) groups=1000(alex),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),100(users),993(kvm),1001(docker)

Three fields matter for permissions. The uid field is the user number and name the shell runs as; this is the identity the owner bits are compared against. The gid field is the primary group. The groups field lists every group the user belongs to; a file whose group matches any entry in that list applies its group bits to this user. The two short forms from the main sequence, id -un and id -gn, print just the user name and just the primary group name. Group membership is the quiet half of the permission model: most access that is not owner access arrives because the user sits in the right group, and id is how you check before blaming the bits.

umask: where new files start

A new file does not start with full permissions. The system starts from a base mode and subtracts the bits set in the umask. The base is 666 for a file and 777 for a directory. With the umask of 0002 from the main sequence, the subtraction leaves a file at 664 and a directory at 775:

Bash
#!/bin/bash
umask
touch default.txt
mkdir defaultdir
stat -c '%a %n' default.txt defaultdir

The exact output:

Output
0002
664 default.txt
775 defaultdir

The umask digits sit in the same owner, group, other order as a mode, and each digit masks the bits it names. In 0002 only the last digit is set, and it masks write in the other position alone. The owner and group positions are untouched, so both keep the base values. For the file, other drops from 6 to 4, read only. For the directory, other drops from 7 to 5, read and execute. That is the whole difference between the base modes and the two results above. On a system with a umask of 0022, the same two creations land at 644 and 755 instead, because the 2 now masks the write bit in the group position as well as in the other position. The practical point is that a loose starting mode is a umask setting, not a chmod habit: fix the umask when every new file starts too open, and use chmod for the files that need a specific mode after creation.

Reading the bits: octal and letters

The mode 640 is three digits, one per class, in the order owner, group, other. Each digit is a sum: read counts 4, write counts 2, and execute counts 1. So 6 means read plus write, 4 means read only, and 0 means no access at all. The full digit table:

Digit Bits Meaning
7 rwx read, write, execute
6 rw- read, write
5 r-x read, execute
4 r-- read only
3 -wx write, execute
2 -w- write only
1 --x execute only
0 --- no access

The letter form in an ls -l line carries the same information, with one leading character first. In -rw-r----- the leading dash says the entry is a plain file; a directory shows a d there instead. The next three letters are the owner bits, rw- for the 6. The middle three are the group bits, r-- for the 4. The last three are the other bits, --- for the 0. Reading a mode in either direction becomes mechanical: 750 is rwxr-x---, and rwxr-x--- is 750.

chmod: numeric and symbolic forms

The numeric form replaces the whole mode at once, as the main sequence did with 640 and 600. The symbolic form names a change instead: a class letter (u for owner, g for group, o for other, a for all three), then + to add bits or - to take them away, then the bit letters. A symbolic change suits a file whose current mode is mostly right:

Bash
#!/bin/bash
touch notes.txt
chmod 600 notes.txt
stat -c '%a' notes.txt
chmod u+x notes.txt
stat -c '%a' notes.txt
chmod g+r,o+r notes.txt
stat -c '%a' notes.txt
chmod u-x notes.txt
stat -c '%a' notes.txt

The exact output:

Output
600
700
744
644

The file starts at 600, owner read and write only. Adding execute for the owner raises the first digit from 6 to 7, giving 700. Adding read for the group and for other raises the second and third digits to 4, giving 744. Removing the owner's execute bit drops the file back to 644. One rule prevents most chmod confusion: the numeric form is absolute, so it discards whatever was there, while the symbolic form is relative, so it keeps every bit it does not mention. Setting a mode from scratch calls for the numeric form. Adjusting one class calls for the symbolic form.

Directories: read lists names, execute opens files

On a directory, the three bits mean different things than on a file. Read on a directory allows listing the names inside it. Execute on a directory allows passing through it to open a file whose name you already know. Write on a directory allows creating and deleting entries in it. A directory mode of 750, owner rwx and group r-x with nothing for other, is the usual shape for a shared working directory. The read-without-execute combination is the trap:

Bash
#!/bin/bash
mkdir vault
echo "secret" > vault/data.txt
chmod 400 vault
ls vault
cat vault/data.txt
echo "cat status=$?"
chmod 500 vault
cat vault/data.txt

The exact output:

Output
data.txt
cat: vault/data.txt: Permission denied
cat status=1
secret

At 400 the directory grants its owner read and nothing else. The ls command lists the file's name, because listing needs only read. The cat command fails with Permission denied, because opening the file needs execute on every directory along the path, and the status line records the failure as 1. Adding execute with chmod 500 fixes the open without changing anything else, and the same cat then prints the file's contents. When a directory seems to show its files yet refuse to open them, the missing bit is execute on the directory, not any permission on the file itself.

Ownership: chown, chgrp, and the root line

Changing the bits is work the owner may do. Changing the owner is not. The chown command reassigns a file to a different user, and the system allows that only to the root user. An ordinary owner who tries gets a refusal, and the file keeps its owner:

Bash
#!/bin/bash
touch owned.txt
stat -c '%U %G' owned.txt
chgrp "$(id -gn)" owned.txt
echo "chgrp status=$?"
chown root owned.txt
echo "chown status=$?"
stat -c '%U %G' owned.txt

The exact output:

Output
alex alex
chgrp status=0
chown: changing ownership of 'owned.txt': Operation not permitted
chown status=1
alex alex

The chgrp command is the partial exception. An owner may move a file into a group the owner belongs to, and the move into the owner's own primary group above succeeds with status 0. The chown attempt fails with Operation not permitted and status 1, because giving a file away, or taking one, would let any user dodge quotas and hand files to other accounts. The rule to carry is simple: bits belong to the owner, ownership belongs to root.

sudo: one command with root rights

Sudo is the bridge across that line. Prefixing a command with sudo runs that one command as the root user, after the system asks for the caller's own password to prove the caller is entitled to do so. The form an administrator uses to hand a file to root is sudo chown root report.txt, and the same prefix works for a chmod on a file the caller does not own. Two habits keep sudo safe. Run the smallest command that does the job, one command at a time, rather than opening a root shell for a whole session. And read the command once before pressing enter, because a root chmod or chown applies with no permission check and no undo. Which users may use sudo at all is set in the sudoers file, a separate topic with its own checker on this site, linked below.

Which command for which job

Job Command Why
See which user and groups this shell runs as id It prints the uid, the primary group, and every group membership in one line
Make a private file readable only by its owner chmod 600 file The numeric form sets owner read and write and clears both other classes at once
Let the group read a file the owner controls chmod 640 file Group read is added while other stays at no access
Make a script runnable by its owner chmod u+x script The symbolic form adds one bit and leaves the rest of the mode untouched
Share a working directory with a group chmod 750 dir The group can list and pass through, and other gets nothing
A directory lists names but its files will not open chmod u+x dir (or restore 500 or 700) Opening a file needs execute on the directory, which read alone does not grant
Move your file into a group you belong to chgrp group file An owner may regroup a file into any of the owner's own groups
Give a file to another user sudo chown user file Ownership changes are reserved to root, so the command runs under sudo
Fix modes on a file you do not own sudo chmod mode file The owner-only rule for chmod is bypassed only with root rights

Checklist

  • Before debugging an access failure, run id and confirm the shell's user and groups are the ones you expect.
  • Before quoting a mode, decode it digit by digit: owner, group, other; read 4, write 2, execute 1.
  • Before setting a mode from scratch, use the numeric form; before adjusting one class, use the symbolic form.
  • Before loosening a directory, check that execute is present wherever a file inside must be opened.
  • Before assuming a file's own bits are at fault, check the execute bit on every directory above it.
  • Before running chown, expect it to need sudo; a plain chown to another user fails by design.
  • Before pressing enter on a sudo command, read it once in full; root ignores the permission model that catches ordinary mistakes.

Sources

Keep Exploring