Understanding File Permissions and Ownership in Linux Print

  • linux, vps, file permission, chmod, chown
  • 0

File permissions and ownership are fundamental parts of Linux security and user management. Understanding how they work helps you control who can read, write, or execute files on your VPS.

 

This guide will explain how to view and modify file permissions and ownership using simple Linux commands.

 


 

1. How to View File Permissions

 

Use the ls -l command to view detailed file permissions:

ls -l filename

Example output:

-rw-r--r-- 1 root root 4096 Mar 27 12:00 example.txt

Breakdown:

-rw-r--r-- → File permissions

1 → Number of hard links

root → File owner

root → File group

4096 → File size (in bytes)

Mar 27 12:00 → Last modified date/time

example.txt → File name

 


 

2. Understanding Permission Flags

 

The first 10 characters indicate the file type and permissions.

 

Example: -rwxr-xr--

Position

Meaning

1st

- (file), d (directory)

2-4

Owner permissions (rwx)

5-7

Group permissions (r-x)

8-10

Others permissions (r--)

Permission Types:

r = read

w = write

x = execute

 


 

3. Changing File Permissions with chmod

 

Symbolic Method:

• Add execute permission for owner:

chmod u+x filename

 

• Remove write permission for group:

chmod g-w filename

 

• Add read for others:

chmod o+r filename

 

 

Numeric (Octal) Method:

 

Each permission is assigned a value:

• Read = 4

• Write = 2

• Execute = 1

 

Combine values to set permissions:

chmod 755 filename

This sets:

• Owner: 7 → read (4) + write (2) + execute (1)

• Group: 5 → read (4) + execute (1)

• Others: 5 → read (4) + execute (1)

 

Common permission sets:

755 → typical for scripts or binaries

644 → typical for text files (readable by all, writable by owner)

 


 

4. Changing Ownership with chown

 

Change file owner:

chown username filename

Change owner and group:

chown username:groupname filename

Example:

chown john:john example.txt

 

 


 

5. Changing Group Ownership with chgrp

 

To change only the group:

chgrp groupname filename

 

 


 

6. Recursively Change Permissions or Ownership

 

To apply changes to a directory and all its contents:

chmod -R 755 /path/to/directory
chown -R user:group /path/to/directory

 

 


 

7. Default Permissions with umask

 

umask defines default permissions for newly created files and directories.

 

Check current umask:

umask

Default file permissions are calculated as:

666 - umask value (for files)
777 - umask value (for directories)

 

 


 

Conclusion

 

Understanding and managing file permissions and ownership is critical for keeping your VPS secure and organized. Always assign the minimum permissions needed for a file or directory to function correctly.

 

For help with permissions issues or troubleshooting, feel free to contact Hosteons Support.

 


Was this answer helpful?

« Back