Linux File Permissions and Ownership
There are no resources for this lesson.
chmod (Change File Permissions)
File permissions are associated with three distinct user groups:
- Owner: The user who created the file or directory.
- Group: A group of users who share common access permissions to the file or directory.
- Others: All other users who are not the owner or part of the group.
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:
# 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)
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.
Once we chown the file back to our user account, we can exit the root user and execute the script:
# Switch to root user sudo su # Change the owner of the script to your username (update username) chown paulh:paulh restricted_script.sh # Exit root user exit # Show the owner of the script ll ./restricted_script.sh # Execute the script ./restricted_script.sh
Mastering file permissions and ownership in Linux is essential for effective security and access control. File permissions, managed using the
chmod command, dictate read, write, and execute privileges through octal representation. Simultaneously, the
chown command empowers you to modify file ownership, granting crucial control over access and management. See you in the next lecture!