List, set, and change standard ugo/rwx permissions

Before we start, here is the basic file permission in binary as follow:

n Permission rwx Binary
7 read, write and execute rwx 111
6 read and write rw- 110
5 read and execute r-x 101
4 read only r– 100
3 write and execute -wx 011
2 write only -w- 010
1 execute only –x 001
0 none 000

Therefore we can use chmod command follows by the binary value or ugo/rwx

I have logged in as user1 and create an executable file in /tmp/ directory so it can be access by anyone.

[user1@bali2 tmp]$ pwd
/tmp
[user1@bali2 tmp]$ cat > hello.sh
#!/usr/bin/bash
echo "hello"

List file or directory permission

List with ls command follows by -l option and file name as an argument to see the file’s permission set

[user1@bali2 tmp]$ ls -lh hello.sh
-rw-r--r--. 1 user1 user1 25 Feb 27 02:34 hello.sh

The user1 has only read and write permission follows by Group which in this case is still user1 and Others that have only read permission.

[user1@bali2 tmp]$ which bash
/usr/bin/bash
[user1@bali2 tmp]$ /usr/bin/bash hello.sh
hello
[user1@bali2 tmp]$ ./hello.sh
-bash: ./hello.sh: Permission denied

user1 can execute this command with /usr/bin/bash but unable to execute it with its own set variable.

Add Remove and Modify Permission

If I add x for user permisson then user1 will able to execute the file

[user1@bali2 tmp]$ chmod u+x hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 user1 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ ./hello.sh 
hello
[user1@bali2 tmp]$ groups
user1 holla

user1 also has other group called holla that share the specific permission with other group members. To remove the permission, turn + to -. This permission update also canbe done with Binary format as follow:

[user1@bali2 tmp]$ chmod u-x hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rw-r--r--. 1 user1 user1 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ chmod 744 hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 user1 29 Feb 27 03:08 hello.sh

We can open all permission (execute,write,read / 777) to this fille for owner,groups,other and take it back with these command

[user1@bali2 tmp]$ chmod ugo+rwx hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxrwxrwx. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ chmod ugo-rwx hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
----------. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ cat hello.sh 
cat: hello.sh: Permission denied
[user1@bali2 tmp]$ chmod 744 hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ cat hello.sh 
#!/usr/bin/bash
echo "hello"

Change File Ownership

To change file owner, we user command chown follows by user and group as follow

[user1@bali2 tmp]$ chown user1:holla hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 holla 29 Feb 27 03:08 hello.sh

If I loggin as user2 that share group name holla, I can read the hello.sh content but unable to alter it.

[user2@bali2 tmp]$ whoami
user2
[user2@bali2 tmp]$ groups
user2 holla
[user2@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user2@bali2 tmp]$ echo "# additional comment" >> hello.sh 
-bash: hello.sh: Permission denied
[user2@bali2 tmp]$ cat hello.sh 
#!/usr/bin/bash
echo "hello"

Until group holla has permission to write on the file

[user1@bali2 tmp]$ ls -l hello.sh 
-rwxr--r--. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user1@bali2 tmp]$ chmod 764 hello.sh 
[user1@bali2 tmp]$ ls -l hello.sh 
-rwxrw-r--. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user2@bali2 tmp]$ ls -l hello.sh 
-rwxrw-r--. 1 user1 holla 29 Feb 27 03:08 hello.sh
[user2@bali2 tmp]$ echo "# additional comment" >> hello.sh 
[user2@bali2 tmp]$ cat hello.sh 
#!/usr/bin/bash
echo "hello"
# additional comment

This material is refers to the rhca objectives