meanii

meanii

# 6 Beginners - File permissions and ownership

# 6 Beginners - File permissions and ownership

Subscribe to my newsletter and never miss my upcoming articles

Every file and directory in the Linux file system has permissions and an owner. Permissions are who is allowed to do what with the file. To see the permissions on a file, use the command ls -l <filename>. You’ll see something like this in the left-most column:

-rw-r--r--

This is a little hard to read, so let’s break it down in the example below:

..own grp oth
-|---|---|---

The dash on the far left will be replaced with a d if the file is a directory. The next three groups of three dashes represent permissions for the owner of the file, the group of the file, and all others. The ‘owner’ of a file is the user who initially created it, though ownership can be changed (more on that shortly). The ‘group’ that owns a file will be the group that its owner belongs to, though this can also be changed. The permissions for ‘others’ apply to any user who is not the owner of the file and not in the group that owns the file. One exception is the ‘root’ user, which has full access to every file on the system.

Here is an example of a file where the owner has full permissions but nobody else can read, write or execute the file:

-rwx------

You may occasionally get a ‘Permission denied’ or ‘Username is not in the sudoers file’ error when trying to do something with a file or directory. This generally means your user does not have the correct permissions for what you are trying to do. You will need to change to a user who does, for example

meanii ✓ su sudo

To re-run your previous command as root, you can use sudo !!, where the two exclamation marks will be replaced with your previous command.

You will occasionally need to change the permissions on a file:

meanii ✓ chmod u=rwx,g=rx,o=r hello.txt

In the above example, we set read, write and execute permissions for the user, read and execute permissions for the group, and read permissions for other users.

If you’re game to learn it, there’s an even simpler shorthand for setting permissions:

meanii ✓ chmod 766 hello.txt

The 7 represents owner permissions, the 6 represents group permissions, and the last 6 represents permissions for the group. But where do these numbers come from?

Each permission is represented by a digit. The permissions for each user type are added together to form the final number.

  • 4 is “read”,
  • 2 is “write”,
  • 1 is “execute”
  • 0 is “no permissions”

So, 7 represents 4 (read) + 2 (write) + 1 (execute). 6 represents 4 (read) + 2 (write), and so on.

You’ll need read permissions to inspect the contents of a file, write permissions to make changes to the file, and execute permissions to run scripts or executables.

You can change the owner and group of a file with the chmod command. For example, let’s say you have a file with the following permissions:

drwxr-xr-x  32 root  root   4096 16 Jul 17:48 cowsay.sh

You decide that you want your user account to be the owner of the file, and its group to be your group. As root, you can run the following command to change the file’s owner and group:

meanii ✓ chown <your_user>:<your_group> hello.txt

When you run ls -l on the file, you’ll see that its owner and group have changed:

drwxr-xr-x  32 your_user  your_group   4096 16 Jul 17:48 cowsay.sh
 
Share this