14th October 2024
One common problem that can be very confusing for new Linux users, and even experienced ones as well, is how the file permissions work. One scenario could be that a user plugs in an external harddrive, but are not able to view, open or copy any of the files.
This is because the files may have come from an older computer and put there by a different user.
Note: This problem is specific for the ext4 file system. Usually this shouldn’t happen if the harddrive has FAT32 or NTFS.
You can verify the owner of the files by right-click on the file > Properties > Permissions. However, this may vary between different GUIs.
Interpret and understand file permissions with CLI
The universal way to verify file permissions is to use the CLI. Right-click on an empty space inside the folder and choose Open Terminal. Then perform the
“ls -l”
command.
wl@computer:/media/wl/47a96c58-0066-4442-8b89-e26702473100/Music$ ls -l
total 5272
-rw-r----- 1 frank frank 1027690 feb. 22 2018 AHHHHH.mp3
-rw-r----- 1 frank frank 2104590 sep. 14 2013 'MI1 Main theme...
-rw-r----- 1 frank frank 2263922 sep. 14 2013 'MI2 Introduction...
Now I have to explain how to interpret this. Down below is a drawing from another example file:
Filetype:
The first square from the left is the Filetype. It can either be one of three things:
“-” represents a regular file.
“d” represents a directory.
“l” represents a symbolic link. (think of it like a shortcut icon in windows)
File Permissions:
There are 4 kinds of permissions: Read(r), Write(w), Execute(x) and None(-).
The file permissions are represented with 9 letters and are divided into 3 parts with 3 letters each. The order of permission in each part is always the same, from the left;
1st letter is read
2nd letter is write
3rd letter is execute
The second square marking the first 3 letters in the drawing represents what the owner are allowed to do with the file. In this case it has Read and Write privileges.
The Third square represents what members of the assigned group is allowed to do with the file. In this case the group members are only allowed to read the file.
The fourth square represents what everybody else (other) are allowed to do with the file. In this case anyone are allowed to read the file.
Hard links:
The fifth square represents the number of hard links. A file usually only have one, but a directory will have a minimum count of 2;
one for "." (link to itself) and;
another for the entry in its child's directory: ".." (link to parent).
Note: If you don’t know what I’m talking about, don’t worry, it’s not important for this. Just know that it will be a number 1 for files, and 2 or more for a directory.
Owner and group:
The Sixth square shows the owner of the file.
The Seventh square shows the group. In this case the group name is the same name as the owner. That is the users Primary group as mentioned in Part 5 about users and groups.
The rest:
The Eighth square shows the filesize in kilobits, (unless the
-h
flag is added to thels
command).The ninth and last square is the date last modified.
The rightmost text is the filename (counter.sh).
Modifying File Permissions
If you have studied the output above, you can see that only the owner of the files is allowed to read and write, group members are allowed to read and everybody else are not allowed to do a damn thing.
So how are we going to fix the problem? One quickfix that I used to do when I didn’t know better is to simply use the “sudo chmod 777 *
” inside the folder. Then everybody have full access to the files. However, that is not best practice.
Changing the group and the owner
Usually the file permissions are fine, it’s just the owner that needs to be changed. To change owner and group, you can use this command:
sudo chown -R user:group *
wl@computer:/media/wl/47a96c58-0066-4442-8b89-e26702473100/Music$ sudo chown -R wl:wl *
[sudo] password for wl:
wl@computer:/media/wl/47a96c58-0066-4442-8b89-e26702473100/Music$ ls -l
total 5272
-rw-r----- 1 wl wl 1027690 feb. 22 2018 AHHHHH.mp3
-rw-r----- 1 wl wl 2104590 sep. 14 2013 'MI1 Main theme (Amiga).mp3'
-rw-r----- 1 wl wl 2263922 sep. 14 2013 'MI2 Introduction (Amiga).mp3'
Explanation:
The -R flag means Recursive. It was not necessary in this case but if there were any sub-directories, all files inside them would have had the owner and group changed as well.
The asterisk (*) targets all files in the current directory.
Alternatively, if you only want the change the owner or the group:
sudo chown -R frank *
(change owner)
(change group)
sudo chgrp -R frank *
And now we are back at where we started.
Changing the file permissions
If these files are intended to be shared, you may consider changing the permissions instead. The command used to change the file permissions is “chmod”.
There are two ways of setting the file permissions with chmod:
Symbolic Privileges. They look like this:
“chmod ugo+rw-”
Absolute Privileges. They look like this “chmod 644”.
To cover both of them it requires a Part 2 of this tutorial. In the meantime, you can study this chmod calculator: