$ Linux Commands

Permissions — File ownership and access control

chmod Permissions

Change file permissions

chmod [OPTIONS] MODE FILE...
chmod 755 script.sh
chmod +x script.sh
chmod -R 644 /var/www/html
chmod u+w file.txt
-R apply recursively

Octal: 7=rwx 6=rw- 5=r-x 4=r-- 0=---. Common: 755 for dirs/executables, 644 for files.

chown Permissions

Change file owner and group

chown [OPTIONS] OWNER[:GROUP] FILE...
chown alice file.txt
chown alice:www-data /var/www/html
chown -R alice:www-data /home/alice/myapp
-R apply recursively
id Permissions

Print user and group information

id [USER]
id
id alice
su Permissions

Switch user

su [OPTIONS] [USER]
su -
su - alice
- start a login shell (loads the user's environment)
sudo Permissions

Execute a command as another user (default: root)

sudo [OPTIONS] COMMAND
sudo apt update
sudo systemctl restart nginx
sudo -u postgres psql
-u USER run as specified user
-i open an interactive root shell
-l list allowed commands
umask Permissions

Set default permissions for new files

umask [MASK]
umask
umask 022

umask 022 means new files get 644, new dirs get 755.