Log In | Register | July 21, 2019

Share
 

Linux - June 18, 2012

List, set, and change standard ugo/rwx permissions.

Viewing Permissions Set

You can view the permissions on any file or folder by using the ls command. For instance, assume we have a file named test.txt and we want to see who is the owner as well as the current permissions.

ls -l test.txt

The above command allows you to view those details. The output of the command is below.

[frank@server ~]$ ls -l test.txt
-rw-rw-r--. 1 frank frank 0 Jun 17 08:00 test.txt

The first part, -rw-rw-r–, are the current permissions set. The number 1 is the inode count. Then we’re followed by the user and group which is frank and frank. The 0 represents the size of the file. Jun 17 08:00 represents the modification date of the file. Lastly the test.txt represents the name of the file.

Lets break down the permissions portion. Permissions can be read in sets of 3. The first – informs you that this file is indeed a file. If the first – was replaced by the letter d then you would know that it is instead a directory. For example:

[frank@server ~]$ ls -ld /test
drwxrwxrwx. 9 frank frank 4096 Jun 17 08:12 /test

After the first parameter, you’ll see that you’re left with 9 more spaces. Each space can contain a different letter of rwx. Depending on the space thats filled a numerical value is also assigned. Below is the conversion between the numerical form and rwx.

Conversions
r (read) = 4
w (write) = 2
x (execute) = 1

Each set of 3 spaces equates to a different permission set. For instance

d rwx rwx rwx
file type user permissions group permissions other permissions

If the permissions are rwx that would mean that the numerical value is equal to 4+2+1 = 7.
If the permissions are r-x, then the numerical value would be 4+0+1 = 5.
If the permissions are —, then the numerical representation would equal 0+0+0 = 0.

So for the below permissions:

[frank@server ~]$ ls -ld /test
drwxrwxrwx. 9 frank frank 4096 Jun 17 08:12 /test

You’ll notice that the user frank, the group frank, and anyone else have rwx on the folder /test.

Updating ugo/rwx Permissions

Using the chmod utility will allow you to change the permissions on a file or folder. For instance.

chmod 755 /test

The above command will change the permissions on the folder to have the user owner have full read, write, execute permissions while the group and other only have read and execute. Below is an example of what the output would look like.

[frank@server ~]$ chmod 755 /test
[frank@server ~]$ ls -ld /test
drwxr-xr-x. 9 frank frank 4096 Jun 17 08:12 /test

You can also use the -r flag along with chmod to apply your change recursively through a directory.

chmod -r 755 /test

Setting Special Permissions

There are also 3 special types of permission that you can set as well. The 3 types are SUID, SGID, and the Sticky Bit.

The SUID bit, if set, allows you to have certain programs always run as the user owner. For instance, if I need a particular program to always run as root regardless of the user that I’m currently logged in as I would do the following.

chmod 4755 /usr/bin/program_here

The above command sets the regular permissions as well as the SUID special bit in front. SUID is represented by the number 4.

The SGID bit, if set on a directory forces each file that is created under that directory to be owned by the group of the primary directory. You can also set the SGID on a program to run that program as if you’re apart of that programs group. Below is an example for setting the SGID on our /test directory.

chmod 2775 /test

The above command will now automatically assign any file to the group owner of the primary directory set with the SGID.

The Sticky bit, should only be set on a directory. When set on a directory other users cannot delete or rename the files and/or subdirectories within that directory. Only the owner and the root user can delete and rename the files and directories within that directory. Below is an example for setting the sticky bit on our /test directory.

chmod 1755 /test

Changing the ownership for files and directories.

The chown utility can be used to change the ownership of the file or directory as well as the group owner. If you need to only change the group then you could also use the chgrp utility.

The chown utility

If we wanted to change the owner of the directory test from root to frank we would do the following.

chown frank /test

If we wanted to change the owner and the group using chown we could do it in 2 ways.

1st Method

chown frank.frank /test

The above command changes both the user and the group to frank on the /test directory.

2nd Method

chown frank. /test

The above command also changes both the user and the group since we set the period after the user frank. Since we left the group empty it is automatically assumed to be the default group for that user.

The chgrp utility

If you wanted to change the group owner only on the /test directory from frank to root. We could apply the following.

chgrp root /test

Post By: | FavoriteLoadingAdd to favorites

1 Comments

RHCSA Certification Study Guide | DevBlog.co
Monday, June 25, 2012

[...] List, set and change standard ugo/rwx permissions [...]

Leave a Comment



Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register