Linux basics: File permissions
My notes while learning about Linux file permissions
I. Linux file permissions
When you type in ls -l
(listing all files in current directory with details) you usually see something like this:
The first column represents the file type and permissions. The second column is the number of hard links (there’s another section towards the end of this post explaining hard links), third column is the creator of the file (in this case root
user, next to that is the group that owns the file (also called root
). Then, the size of the file in bytes (can do ls -lh
for human readable storage size) is the 5th column. The last 2 columns are date last modified and file name.
We will focus on the first column, which is the file permissions.
Eg: - rwx rw- r—-: type - file (directory would be 'd')rwx: permission for user who created the file - read, write, executerw-: permission for the group the user belongs to - only read, writer--: permission for others - only read
The permissions of read, write, execute also have numeric values.
Looking from a binary perspective, each group is 1 byte or 8 bits — each permission is either being enabled or disabled (1 or 0) and there are 3 kinds of permission (read, write, execute), so in total there are 8 combinations — the octal method. When the value is 0 in decimal, or 000
in binary, no permission is granted.
In the photo above, execute permission, when enabled, will have a decimal value of 1 (001
), for write permission it is 2 (010
)and then 4 (100
) for read permission. When all permissions are enabled, you get number 7 (111
).
Note: for a directory, read/write/execute have slightly different meaning:
- If read is not set, you cannot list (
ls
) what’s inside the directory. - If write is not set, you cannot delete or create the directory or its subdirectories.
- If execute is not set, you can’t run a command with it — such as
cd
orls
orfind.
In order to change the permissions, we use the command chmod
. There are 2 ways that you can modify the permissions — using letters (r,w,x) or numbers.
Using letters — usually used when you want to add or remove permissions, not set or reset:
chmod [ugo]+-r/w/x [file]
[ugo]
represents 3 groups — user who creates the file, group that owns the file, and others (everyone else). You could set multiple groups at a time i.eug
orgo
oruo
. If none of[ugo]
is give, the permission is applied to all user, group, and others.+-
: plus symbol means adding permission(s), minus symbol means removing one(s).r/w/x
: permission(s) to be added or removed — could be multiple permissions, not just single.[file]
: the targeted file.
Example:
chmod ug+rw mylist.txt
This would add read and write permissions to the user and the group that own the mylist.txt
file.
Using numbers — usually used when you want to set or reset permissions:
chmod [number][number][number] [file]
This syntax is quite straightforward, each number is for each group.
Example:
chmod 777 mylist.txt
This gives full permissions (read, write, execute) to all 3 groups for the mylist.txt
file.
chmod 604 mylist.txt
This gives read (4) and write (2) permissions (4 + 2 = 6) to user who creates the file, giving no permission (0) to group who owns the file and giving only read (4) permission to everyone else.
II. Soft & Hard links
What is Index Node (or inode)
- Every file in the system has an inode.
- It is like the ID of a file. It has all information (space, permission, etc) about the file except file name and content of the file.
What are symbolic/soft link and hard link?
Hard link:
- Refers to the physical location of the file.
- Cannot refer to directory.
- Cannot refer to file outside of current filesystem.
- Remain linked if the linked file is moved somewhere else in the current system — because the original file and the link file have the same inode.
- All hard links are like a copy of the file. If you delete the original file, the copy still remains.
Soft link or symbolic link:
- A symbolic path indicating the location of a file — basically it’s just a string of the path to the target (hence, small file size)
- Act like a shortcut — a pointer to the file
- Can link between different filesystem
- Do not get updated when the target file is moved because the original file and the shortcuts have different inode
- If original file is deleted, shortcuts are useless
III. Resources
Linux file types and file permissions
Beginner’s Guide to Bash Terminal (highly recommend this one)
Until next time. Happy reading!