Linux | Alter Permissions

chmod

To change the permissions of a file or directory, we can use the chmod command (change mode).

To use chmod to alter permissions, we need to tell it:

  • Who we are changing permission for?
  • What change are we making? Adding? Removing?
  • Which permissions are we setting?
chmod mode file

who

  • u: user(the owner of the file)
  • g: group(members of the group the file belongs to)
  • o: other(the “world”)
  • a: all of the above

what

  • -: removes the permission
  • +: grants the permission
  • =: set a permission and removes others

which

  • r: the read permission
  • w: the write permission
  • x: the execute permission

Example

Add write permissions to the group

chmod g+w file.txt

Remove write permissions from all

chmod a-w file.txt

Add executable permissions for owner

chmod u+x file.txt

Set permissions to read ONLY for all

chmod a=r file.txt

Add permissions to read and executable to the file

chmod u+wx happy.txt

chmod Octals (base 8)

chmod also supports another way of representing permission patterns: octal numbers(base 8). Each digit in an octal number represents 3 binary digits.

Octal Binary File Mode
0 000
1 001 –x
2 010 -w-
3 011 -wx
4 100 r–
5 101 r-x
6 110 rw-
7 111 rwx

Example

Q: Alter the file permissions to

  • user: rw_
  • group: r__
  • other: r__

A:

User Group Other
Octal 6 4 4
Binary 110 100 100
Permissions rw_ r__ r__
chmod 644 file.txt

Change Identity (su command)

We can use the su command to start a shell as another user within our shell session.

change the currnet shell user to “kitty”
NOTE: - or --login option will change the current directory, it is the recommanded option to prevent side effects caused by mixing environments.

su - kitty

using exit command to exit the enviroment of the subsitube user.

Root User

The root user can run any command and access any file on the machine, regardless of the file’s actual owner.

Based on the reason above, the root user could easily damage the system by running wrong command. Therefore, Ubuntu locks the root user by default.

Using The Sudo Command

Even if the root user is locked by default, we can still run specific commands as the root user by using the sudo command.

Chaning ownwership (Chown command)

The chown command is used to change the owner and/or the group owner of a specific file or directory.

Example

Make “kevin” the owner of file.tx

sudo chown kevin file.txt

Make “test_group” as the group owner of file.txt

chown :test_group file.txt
Python 基礎語法 Linux | Understand Permissions
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×