Linux chmod Example

28/12/2020
Chưa phân loại

In this quick tutorial, we will see how we can use chmod command in an Ubuntu machine to find, modify and remove user permissions from specific files which exist on the user’s file system. Let’s play through various conditions so that we can master basic chmod commands which can make our everyday life easier with Ubuntu.

Linux Permissions

Linux Permissions are a great set of rules which are simple to understand if we grasp the basics rights. The three main points which we need to understand to know how Linux Permissions work are:

  • The element for which the permissions was defined
  • What actions can be performed with a permission
  • Who can perform what actions

There are two basic elements in Linux Filesystem:

  1. Directories
  2. Files

There are three actions which can be performed:

  1. Read
  2. Write
  3. Execute. Apart from executing scripts, same actions is needed to create files and other folders inside it

User who can perform these actions are:

  1. Owner of the file
  2. Group of the owner of the file
  3. User which are not associated with owner group or owner itself

To see permissions related to a file, run the following command:

ls -l

Here is what we get back with this command:

Find permission for files

In the output, the first 10 characters presents the permission for the file:

  • First character, which is ‘-‘ in this case signifies that this is a file. For a directory, this would’ve been a ‘d’.
  • Next nine characters represent permissions for the owner, the group of the owner, and others, respectively.

Changing Permissions

Syntax for modifying permission of a file looks like:

chmod permissions file [file 2]

Octal representation for Permissions

We can present permissions as an octal number. For example, for setting read, write & execute permissions for the owner, read & write permissions for its group, and no permission for others, to a hello.txt file, we will execute the following command:

sudo chmod 760 hello.txt

Once we execute the above command and try to read a file with a non-owner account using the following command:

sudo -u notowner-user more hello.txt

We will get the following error:

hello.txt: Permission denied

But where does this number come from? Each digit of that number represents a set of permissions. Let us see how were they derived:

  • 0: Signifies no permission
  • 1: Signifies the execute permission
  • 2: Signifies the write permission
  • 4: Signifies the read permission

For assigning read, write & execute permissions for the owner, we assigned him the number 7(= 4 + 2 + 1). Let us better understand this in a table of digits:

Number Binary Read Write Execute
0 000 NO NO NO
1 001 NO NO YES
2 010 NO YES NO
3 011 NO YES YES
4 100 YES NO NO
5 101 YES NO YES
6 110 YES YES NO
7 111 YES YES YES

Above table is much clear in what each represents in terms of file permissions.

Character representation for Permissions

We can present permissions as an octal number. For example, for setting read, write & execute permissions for the owner, read & write permissions for its group, and no permission for others, to a hello.txt file, we will execute the following command:

sudo chmod u=rwe,g=rw,o-rwx hello.txt

To add permissions to an existing user, we can also do:

sudo chmod g+w hello.txt

Here, the write permission was being assigned to the user group of the owner of the file.

Recursive Permission Changes

We can also change permissions for file contained in a specific directory with a single command. To modify the permissions of each and every file and folder in a provided directory at once, use sudo chmod with -R:

sudo chmod 777 Directory/*

We can see the following output which clearly reflects the change in file permissions:

Recursively changing file permissions

Conclusion

In this lesson, we looked at how we can modify a file permssions and if need be, do it recursively. We understood basic concepts behind how Linux permissions which can help us in our everyday work a lot.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Chronyd dịch vụ thay thế NTPD trên Unix

Chronyd đang được đánh giá là lựa chọn tối ưu hơn NTPD để đồng bộ thời gian  trên Unix...
30/12/2020

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?

Để có thể dễ dàng truy cập internet mà không ảnh hưởng đến quyền riêng tư cũng như khả năng...
27/05/2024

Avidemux 2.6.19 Video Editor released with general UI polish

Avidemux 2.6.19 recently released, is a free open-source program designed for multi-purpose video editing, processing,...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024