How to set specific file permissions?

QuestionsQuestions4 SkillsProDec, 20 2025
0186

To set specific file permissions in Linux, you use the chmod (change mode) command. Just like with directories, you can use either Symbolic or Numeric methods.

Here is the step-by-step guide for files:

1. The Common Permissions for Files

  • Read (r/4): Open and view the content of the file.
  • Write (w/2): Edit or delete the content of the file.
  • Execute (x/1): Run the file as a program or script.

2. Method A: The Numeric Method (Fastest)

You use three digits: Owner | Group | Others. Each digit is the sum of the permissions: 4(Read) + 2(Write) + 1(Execute).

  • chmod 644 file.txt (The most common file permission)
    • 6 (4+2): You can read/write.
    • 4: Group can only read.
    • 4: Others can only read.
  • chmod 700 script.sh
    • 7 (4+2+1): You can do everything.
    • 0: No one else can even see what's inside.
  • chmod 600 private.key
    • 6: You can read/write.
    • 0: Strictly private.

3. Method B: The Symbolic Method (Most Precise)

Use this when you want to change one specific thing without affecting the other permissions.

  • Make a script executable:
    chmod +x myscript.sh
    
  • Make a file "Read Only" for everyone:
    chmod a-w file.txt
    ``` (The `a` stands for **all**, `-w` removes **write**)
    
  • Give only the group "Write" access:
    chmod g+w file.txt
    

4. Practice in your Lab Environment

Let's try a real example in your /home/labex/project folder:

  1. Create a new file:
    touch secret.txt
    
  2. Check current permissions:
    ls -l secret.txt
    
  3. Set permissions so only you can read and write (Numeric):
    chmod 600 secret.txt
    
  4. Verify the change:
    ls -l secret.txt
    # You should see: -rw-------
    

Summary Tip:

  • Use Numeric (e.g., 644) when you want to define the entire state of the file.
  • Use Symbolic (e.g., +x) when you just want to add or remove one specific permission.

Now that you know how to set permissions, do you want to learn how to change who owns the file using the chown command?

0 Comments

no data
Be the first to share your comment!