The chown
command is used to change the owner and/or group of a file or directory.
To see the ownership of a file:
ls -l filename
Example output:
-rw-r--r-- 1 user1 group1 1234 Feb 14 10:00 file.txt
- First
user1
→ File owner - Second
group1
→ File group
To assign a new owner to a file:
chown newuser filename
To change only the group:
chown :newgroup filename
To change both owner and group:
chown newuser:newgroup filename
To apply changes to all files inside a directory:
chown -R newuser:newgroup directory/
The chmod
command modifies read (r), write (w), and execute (x) permissions for the user (u), group (g), and others (o).
Symbol | User | Permission Type | Numeric Value |
---|---|---|---|
r |
Read | Can read the file | 4 |
w |
Write | Can modify the file | 2 |
x |
Execute | Can execute the file | 1 |
To check permissions:
ls -l filename
Example output:
-rwxr-xr-- 1 user group 1234 Feb 14 10:00 script.sh
rwx
→ Owner can read, write, executer-x
→ Group can read and executer--
→ Others can only read
chmod 755 script.sh
- Owner (
7
) →rwx
(4+2+1) - Group (
5
) →r-x
(4+0+1) - Others (
5
) →r-x
(4+0+1)
chmod u+rwx,g+rx,o+r script.sh
u+rwx
→ User gets read, write, executeg+rx
→ Group gets read and executeo+r
→ Others get read-only
Command | Meaning |
---|---|
chmod 777 file |
Everyone can read, write, and execute ( |
chmod 755 file |
Owner can read, write, execute; others can read and execute |
chmod 644 file |
Owner can read/write, others can only read |
chmod 600 file |
Only owner can read/write |
chmod 400 file |
Only owner can read (no write/execute) |
chmod 000 file |
No one can access the file |
chmod -R 755 directory/
- Applies permissions to all files and subdirectories inside
directory/
.
- Use
chown
to change file ownership. - Use
chmod
to modify file permissions. - Use octal (
755
) or symbolic (u+x
) notation for permission changes. - Be careful with
chmod 777
—it gives everyone full control! - Use
-R
for recursive changes.