Linux & the Command Line

Permissions and Ownership

30 دقيقة

Linux is multi-user. Every file has an owner, a group and a set of permissions. Understanding them fixes most "Permission denied" errors and is essential for server security.

Reading permissions

ls -l deploy.sh
-rwxr-x--- 1 panda devs 512 Oct 20 09:12 deploy.sh
Characters Meaning
- type: - file, d directory, l link
rwx owner (panda) can read, write, execute
r-x group (devs) can read and execute
--- everyone else: nothing

For directories, r lets you list contents, w lets you create and delete files inside, and x lets you enter it.

Changing permissions with chmod

Symbolic form:

chmod +x deploy.sh        # everyone may execute
chmod u+x deploy.sh       # only the owner (u=user, g=group, o=others)
chmod go-w config.ini     # remove write from group and others

Numeric form: r=4, w=2, x=1, added per group.

Number Permissions Typical use
755 rwxr-xr-x programs, public folders
644 rw-r--r-- normal files
600 rw------- secrets: .env, SSH keys
700 rwx------ private folders like ~/.ssh
chmod 600 .env
chmod 700 ~/.ssh && chmod 600 ~/.ssh/id_ed25519

SSH refuses to use a private key that others can read. That error message is permissions protecting you.

Ownership with chown

sudo chown www-data:www-data /var/www/app/uploads
sudo chown -R panda:panda ~/projects

sudo: borrowing root

sudo command runs one command as root after asking for your password. Use it only when needed, never "just in case". A mistyped sudo rm can destroy the system, and a script run with sudo that you did not read can do anything.

Never fix a permission error with chmod 777. It lets every user and every compromised process modify the file. Find out which user needs access and grant exactly that.

Practice

Complete the Make it executable mission, then run ls -l ~/scripts to see the x appear.

Check yourself

  • What does 640 allow?
  • Why should .env be 600?
  • What is wrong with chmod -R 777 /var/www?

يُحفظ تقدّمك في هذا المتصفح فقط.

إرسال للمراجعة (10 نقطة)