The chmod command

change file mode bits 24 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.

Options in the examples

-R, --recursive
change files and directories recursively

Examples

chmod 755 script.sh

rwxr-xr-x

chmod 644 file.txt

rw-r--r--

chmod 600 private.txt

rw-------

chmod 777 public/

rwxrwxrwx (dangerous!)

chmod +x script.sh

Add execute for all

chmod u+x script.sh

Add execute for owner only

chmod go-w file.txt

Remove write from group and others

chmod a=r file.txt

Set read-only for everyone

chmod u+x,go-x script.sh

Multiple changes

chmod -R 755 directory/

Apply to all files inside

chmod +x myscript.sh
chmod 4755 file

Setuid bit

chmod 2755 file

Setgid bit

chmod 1755 dir

Sticky bit

sudo chmod -R 755 /var/www/html/
sudo chmod 600 /var/www/html/secret.html
chmod +x ~/scripts/backup.sh

Allow it to run

chmod 600 suspicious-file

Restrict a file to owner-only

sudo chmod 640 FILE

Owner read/write, group read, others nothing

chmod 600 ~/.ssh/config

Only you may write the config

chmod 600 ~/.ssh/id_ed25519

Only you may read the private key

chmod 700 ~/.ssh

The folder itself: yours alone

sudo chmod 644 FILE

rw-r--r--: everyone may read, only the owner may write

sudo chmod 755 FOLDER

rwxr-xr-x: everyone may enter and list it

Lessons that use chmod