Hostwinds Tutorials

Search results for:


Table of Contents


Understanding Linux File Ownership and Permissions
Checking Current File Ownership and Permissions
Example Output of 'ls -l' Command:
Changing File Ownership
Change the file owner:
Change the file group:
Change both the owner and group:
Modifying File Permissions
Symbolic Mode:
Add execute permission for the owner:
Add read and write permissions for both owner and group:
Remove read permission for others:
Numeric Mode
Set no permissions for anyone:
Set all permissions for everyone:
Set read and write for the owner, and read for the group:

Changing File Ownership and Permissions in Linux

Tags: Linux,  Security 

Understanding Linux File Ownership and Permissions
Checking Current File Ownership and Permissions
Example Output of 'ls -l' Command:
Changing File Ownership
Change the file owner:
Change the file group:
Change both the owner and group:
Modifying File Permissions
Symbolic Mode:
Add execute permission for the owner:
Add read and write permissions for both owner and group:
Remove read permission for others:
Numeric Mode
Set no permissions for anyone:
Set all permissions for everyone:
Set read and write for the owner, and read for the group:

Linux is designed to manage multiple users efficiently, ensuring that only authorized individuals can access or modify specific files. This is done through a system of file ownership and permissions that helps protect system and user data. Understanding these mechanisms helps maintain system security and ensure proper file management.

Understanding Linux File Ownership and Permissions

In Linux, every file and directory is associated with an owner and a group. Access to these files is controlled through permissions, which determine whether a user can read, write, or execute a file.

Key Concepts:

  • Owner: The user who created the file.
  • Group: A set of users who share access to the file.
  • Permissions: Define what actions can be performed on the file:
    • Read (r): Allows viewing the file's contents.
    • Write (w): Permits modifications to the file.
    • Execute (x): Enables running the file as a program.

Checking Current File Ownership and Permissions

To view the ownership and permissions of a file/directory, use the 'ls -l' command:

ls -l filename
Example Output of 'ls -l' Command:
-rwxrw-r-- 1 foo bar 1024 Jan 1 00:00 filename
  • -rwxrw-r--: Represents the file type and permissions.
    • rwx: Permissions for the owner (foo).
    • rw-: Permissions for the group (bar).
    • r--: Permissions for others.
  • foo: Owner of the file.
  • bar: Group associated with the file.
  • 1024: File size in bytes.
  • Jan 1 00:00: Last modification date and time.

To list details of all files in a directory, simply use 'ls -l' without specifying a filename. For directories, this command lists contents with the same detailed output.

For more details on ls, use 'ls --help' or 'man ls' commands.

Changing File Ownership

Only the root user or users with appropriate sudo privileges can modify file ownership. To change ownership, use the 'chown' command:

chown [user]:[group] filename
Change the file owner:
chown newowner filename
Change the file group:
chown :newgroup filename
Change both the owner and group:
chown newowner:newgroup filename

To apply changes to ownership recursively to a directory and its contents, use the '-R 'option:

chown -R newowner:newgroup /path/to/directory

For more information on 'chown', use 'chown --help' or 'man chown'.

Modifying File Permissions

File permissions can be adjusted using the 'chmod' command. There are two main methods:

  • Symbolic Mode

  • Numeric Mode

Symbolic Mode:

Adjust permissions using symbolic representations.

Add execute permission for the owner:
chmod u+x filename
Add read and write permissions for both owner and group:
chmod ug+rw filename
Remove read permission for others:
chmod o-r filename

Numeric Mode

Numeric mode allows you to set permissions using a three-digit number. Each digit represents permissions for the owner, group, and others respectively. The value for each digit is the sum of the numbers representing which permissions are allowed for that role, with a max value of 7 (seven) for each digit. 0 (zero) represents no permissions.

Numeric representation for each permission type:

  • Read: 4
  • Write: 2
  • Execute: 1

For example, to give the user read and write access, the group only read access, and other users no access, the number to represent that would be 740.

Here are a few more examples:

Set no permissions for anyone:
chmod 000 filename
Set all permissions for everyone:
chmod 777 filename
Set read and write for the owner, and read for the group:
chmod 640 filename

Written by Hostwinds Team  /  August 29, 2018