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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
robinamirbahar profile image
Robina

Interesting

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay