Level 1
0 / 100 XP

Linux File Permissions and Ownership

chmod (Change File Permissions)

File permissions are associated with three distinct user groups:

  1. Owner: The user who created the file or directory.
  2. Group: A group of users who share common access permissions to the file or directory.
  3. Others: All other users who are not the owner or part of the group.

The chmod command is used to change the permissions (read, write, execute) of files and directories. The command is followed by a octal code or symbolic representation that specifies the desired permissions. See the example below:

chmod 644 filename.txt

An example of making a script that is only executable, readable and writable by the owner would be below:

Bash
# The sudo command below will complete the commands as the root user sudo su # Create the file echo 'echo "The script is working!"' > restricted_script.sh # Change the permissions chmod 700 restricted_script.sh # Exit root user exit # Show the owner of the script ll ./restricted_script.sh # Attempt to run the script ./restricted_script.sh # This will fail because only the root user can run the script due to the chmod script

chown (Change File Ownership)

The chown command is used to change the ownership of files and directories. It can change both the owner and group owner of a file.

In the example above, the file restricted_script.sh script cannot be run by our user because its owner by the root user and has restrictive permissions. The change this, we should SU (switch user) to the root user, and chown the script back to our user account.

__

Be sure to update the chown command below with your username:username. My username and group is paulh:paulh, but yours likely is different.

Once we chown the file back to our user account, we can exit the root user and execute the script:

# Switch to root user…