DEV Community

shun
shun

Posted on

Permissions and Ownership in Linux

In Linux, files and directories have three types of permissions (read, write, execute) for three types of users (owner, group, others).
r (read): Allows reading the file's contents.
w (write): Permits modifying the file's contents.
x (execute): Enables executing the file as a program.

Changing File Permissions with chmod

You can use the chmod command to modify access permissions for files and directories.

  • Symbolic Mode:
  $ chmod u+x file.txt  # Add execute permission to the owner
  $ chmod g-w file.txt  # Remove write permission from the group
  $ chmod o=r file.txt  # Set read-only permission for others
Enter fullscreen mode Exit fullscreen mode
  • Octal Mode:
  $ chmod 755 file.txt  # rwxr-xr-x: Owner has all permissions, group has read and execute, others have read and execute
Enter fullscreen mode Exit fullscreen mode

Changing File Owner and Group with chown

The chown command lets you change the owner and group of files and directories.

$ chown newowner file.txt          # Change the file's owner
$ chown newowner:newgroup file.txt # Change both the owner and group of the file
Enter fullscreen mode Exit fullscreen mode

Changing Group Ownership with chgrp

Using the chgrp command, you can change the group ownership of files and directories.

$ chgrp newgroup file.txt
Enter fullscreen mode Exit fullscreen mode

Viewing File Permissions with ls -l

The ls -l command displays file and directory access permissions, owner, and group.

$ ls -l
-rwxr-xr-x 1 owner group 1234 Jan 1 12:00 file.txt
Enter fullscreen mode Exit fullscreen mode

Understanding and managing permissions and ownership is crucial for maintaining security and proper access control in a Linux system.

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

Interesting