Set read permission linux

How To Set Readonly File Permissions On Linux / Unix Web Server DocumentRoot

How to set files in read-only mode

The syntax is:
## use only for files ##
chmod 0444 /var/www/html/*
chmod 0444 /var/www/html/*.php

How to to set directories in read-only mode

TO set directories in read-only mode, enter:
## use only for dirs ##
chmod 0444 /var/www/html/
chmod 0444 /path/to/your/dir/
# ***************************************************************************
# Say webserver user/group is www-data, and file-owned by ftp-data user/group
# ***************************************************************************
# All files/dirs are read-only
chmod -R 0444 /var/www/html/
# All files/dir owned by ftp-data
chown -R ftp-data:ftp-data /var/www/html/
# All directories and sub-dirs has 0445 permission (so that webserver user www-data can read our files)
find /var/www/html/ -type d -print0 | xargs -0 -I <> chmod 0445 «<>»
To find all files (including sub-directories in /var/www/html) and set read-only permission, enter:

However, you need to set set read-only and execute permission on /var/www/html and all sub-directories so that web server can enter into your DocumentRoot, enter:

A warning about write permission

Please note that write access on a directory /var/www/html/ allows anyone to remove or add new files. In other words, you may need to set a read-only permission for /var/www/html/ directory itself:

In some cases you can change file owner and group to set tight permissions as per your setup:

Источник

Unix / Linux — File Permission / Access Modes

In this chapter, we will discuss in detail about file permission and access modes in Unix. File ownership is an important component of Unix that provides a secure method for storing files. Every file in Unix has the following attributes −

Owner permissions − The owner’s permissions determine what actions the owner of the file can perform on the file.

Group permissions − The group’s permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.

Other (world) permissions − The permissions for others indicate what action all other users can perform on the file.

The Permission Indicators

While using ls -l command, it displays various information related to file permission as follows −

Here, the first column represents different access modes, i.e., the permission associated with a file or a directory.

The permissions are broken into groups of threes, and each position in the group denotes a specific permission, in this order: read (r), write (w), execute (x) −

The first three characters (2-4) represent the permissions for the file’s owner. For example, -rwxr-xr— represents that the owner has read (r), write (w) and execute (x) permission.

The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr— represents that the group has read (r) and execute (x) permission, but no write permission.

The last group of three characters (8-10) represents the permissions for everyone else. For example, -rwxr-xr— represents that there is read (r) only permission.

File Access Modes

The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which have been described below −

Grants the capability to read, i.e., view the contents of the file.

Write

Grants the capability to modify, or remove the content of the file.

Execute

User with execute permissions can run a file as a program.

Directory Access Modes

Directory access modes are listed and organized in the same manner as any other file. There are a few differences that need to be mentioned −

Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.

Write

Access means that the user can add or delete files from the directory.

Execute

Executing a directory doesn’t really make sense, so think of this as a traverse permission.

A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions

To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod — the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

Adds the designated permission(s) to a file or directory.

Removes the designated permission(s) from a file or directory.

Sets the designated permission(s).

Here’s an example using testfile. Running ls -1 on the testfile shows that the file’s permissions are as follows −

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes −

Here’s how you can combine these commands on a single line −

Using chmod with Absolute Permissions

The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Sr.No. Chmod operator & Description
1
Number Octal Permission Representation Ref
0 No permission
1 Execute permission —x
2 Write permission -w-
3 Execute and write permission: 1 (execute) + 2 (write) = 3 -wx
4 Read permission r—
5 Read and execute permission: 4 (read) + 1 (execute) = 5 r-x
6 Read and write permission: 4 (read) + 2 (write) = 6 rw-
7 All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 rwx

Here’s an example using the testfile. Running ls -1 on the testfile shows that the file’s permissions are as follows −

Then each example chmod command from the preceding table is run on the testfile, followed by ls –l, so you can see the permission changes −

Changing Owners and Groups

While creating an account on Unix, it assigns a owner ID and a group ID to each user. All the permissions mentioned above are also assigned based on the Owner and the Groups.

Two commands are available to change the owner and the group of files −

chown − The chown command stands for «change owner» and is used to change the owner of a file.

chgrp − The chgrp command stands for «change group» and is used to change the group of a file.

Changing Ownership

The chown command changes the ownership of a file. The basic syntax is as follows −

The value of the user can be either the name of a user on the system or the user id (uid) of a user on the system.

The following example will help you understand the concept −

Changes the owner of the given file to the user amrood.

NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own.

Changing Group Ownership

The chgrp command changes the group ownership of a file. The basic syntax is as follows −

The value of group can be the name of a group on the system or the group ID (GID) of a group on the system.

Following example helps you understand the concept −

Changes the group of the given file to special group.

SUID and SGID File Permission

Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.

As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.

As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow.

Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program’s owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

The SUID and SGID bits will appear as the letter «s» if the permission is available. The SUID «s» bit will be located in the permission bits where the owners’ execute permission normally resides.

For example, the command −

Shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.

If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users −

  • The owner of the sticky directory
  • The owner of the file being removed
  • The super user, root

To set the SUID and SGID bits for any directory try the following command −

Источник

Linux permissions: making sense of 755 and rwxr-xr-x

Understanding Linux permissions might seem like a near-impossible task—what does 755 or u=rwx,g=rw,o=r mean, anyway?—but it’s actually easier than you think. Let’s take a look.

Linux is a multi-user operating system that can be accessed by many users simultaneously. This might make you to think that a user can manipulate files and directories of another user, but all Linux operating systems protect filesystems under two levels of authorization—ownership and permission—to prevent unauthorized access to the filesystem in an effective and easy manner.

Owners of files, directories, and processes

Before we try to explore who are the owners of files and directories, let’s get an overview of user types in Linux. In Linux, there are two types of users, system users and regular users. System users are created by the operating system itself and are used to manage background processes.

Only $122/yr for 48GB RAM and 480GB 960GB NVMe and free backups for LIFE!
Celebrating our 9 year anniversary! Capacity is limited and some deals will sell out. Get yours before they are gone!

We generally create regular users to create and run processes interactively through a GUI or terminal. Besides these two types of users, there is a superuser by the name root , which has access to entire system to manage and override any settings in the system.

In Linux, the owners of the files, directories and processes will be assigned to these three types of users: regular, system, or root. Before we try to explore what permissions can be assigned to these three types of users, let’s try to understand the types of permission that are available in Linux.

What’s the BEST DEAL in cloud hosting?

Develop at hyperspeed with a Performance VPS from SSD Nodes. We DOUBLED the amount of blazing-fast NVMe storage on our most popular plan and beefed up the CPU offering on these plans. There’s nothing else like it on the market, at least not at these prices.

Score a 16GB Performance VPS with 160GB of NVMe storage for just $99/year for a limited time!

What Linux permissions types are there?

There are two levels of permissions assigned to the files, directories, and processes in Linux. The first one is permission groups, which is otherwise referred to as the ownership. The second one is permission types, which can be read , write , or execute .

Permission groups

For every file and directory in Linux, there are the sets of users for whom we specify permissions. They are:

  • Owners
  • Groups
  • Others

Owners: The user who creates a file, folder, or process is the owners .

Groups: Groups refers to anyone who is in the same group as the owner.

Others: Any user who is neither the owner of the file/directory and doesn’t belong to the same group is assigned to others group.

Permission types

What operations can each of the above three user groups can do is defined by permission types . There are three basic permission types that can be assigned to three groups of users and they are read (r) , write (w) , and execute (x) .

What do read, write and execute mean for files and directories ?

For files:

  • Read is the ability to view the contents of a file.
  • Write is the ability to edit or delete a file.
  • Execute is the ability to run a file as an executable program.

For directories:

  • Read is the ability to read the contents of a directory.
  • Write is the ability to write into the directory, like creating files and sub-directories inside a directory.
  • Execute is the ability to cd into the directory and to view the metadata of the files inside the directory using ls command.

What’s the BEST DEAL in cloud hosting?

Develop at hyperspeed with a Performance VPS from SSD Nodes. We DOUBLED the amount of blazing-fast NVMe storage on our most popular plan and beefed up the CPU offering on these plans. There’s nothing else like it on the market, at least not at these prices.

Score a 16GB Performance VPS with 160GB of NVMe storage for just $99/year for a limited time!

How do I find the permissions of a file?

Let’s try to find the permissions of files and directories. To find the permissions that is already assigned to files or directories, use ls command with -l switch.

The first ten characters in the format drwxrwxrwx , represents the permissions for all the three classes of users. Let’s try to understand what each of these letters means. The first character, d , signifies that the file is a directory. This position can be blank(-) or any of the following characters:

Then the next three characters (drwxr-xr-x) represent the permissions that have been assigned to the owners of the file. The owner dd can read, write, and execute to the folder Pictures .

Moving on to the next three characters (drwxr-xr-x), which is r-x , represents the group permissions. The users from users group can access the file according to the group permissions, which specify they can read and execute in the directory but cannot write into it. The hyphen signifies that the permission is not granted.

The last three characters (drwxr-xr-x) represents the permissions for other groups who are neither the owner nor a member of the group users and the permissions are set to read and execute only.

The 11th character is a number that represents the number of hard links for the file and is not related to permission for a file. The two columns next to this number (drwxr-xr-x 3 dd users) represents the owner and group of the file.

To find the permissions for a particular file or directory, specify the name of the file in the ls command like below.

Permissions in numeric notation

Two notations are used to represents the permissions for files and folders. The one that we already came about (r,w,x) is known as symbolic notation. The other one is numeric notation. In this notation, a number (0,1,2,4) represents a permission and are as follows:

  • 0: No permission
  • 1: Execute (x)
  • 2: Write (w)
  • 4: Read ®

Now, how to calculate permissions for users and groups in numeric notation? Just add the permission’s value to get the value of user, group, and other permissions respectively.

read(4), write(2) and execute(1) permission rwx translated to 7 (4+2+1)
read(4) and write(2) permission rw- translated to 6 (4+2)
write(2) and execute(1) permission -wx translated to 3 (2+1) etc.

Therefore the permission rwxrwxrwx is same as 777 , rwxr-xr-x is same as 755 , and so on.

Changing Linux permissions using symbolic notation

Using the chmod command, one can add or remove permissions from a file or a directory. The letters u (owner/user), g (group) and o (other) are used to add or remove permissions for each of the three user types along with following three signs.

  • the minus sign ( — ), which means “remove these permissions”
  • the plus sign ( + ), which means “add these permissions”
  • the equals sign ( = ), which means “change the permissions to exactly these”.

Add permissions

To add permissions, use chmod command along with plus sign ( + ), which means “add these permissions”.

So if you want to add execute permission for all three types of users for a script file, use the following chmod command.

To add execute permission for owner of the file only, use the following chmod command.

Similarly, you can use +r to add the read permissions, and +w to add the write permissions.

You may also assign permissions to users, groups and others or by combining them selectively. Just specify the classes of users (u, g, or o) and the permission (r, w, or x) that you want to assign. For example, the following chmod command will add execute and write permission to the owner of the file.

To add write permission to both the owners and groups use the following command.

You can also add permissions for multiple classes of users at one go. The following example will add read, write and execute permission for owner and for the group and others, permission are sets to read and execute.

Remove permissions

In some situations, you may need to remove permissions rather than to add them. Just change + to — to remove permissions for any of the three classes of users. Below are the few examples that shows how to remove permissions using chmod .

Changing Linux permissions using numeric notation

You can also set permissions using numeric notation instead of symbolic notation. Permissions set in this way use up to four digits. Now you may ask why 4 digits since there are only three classes of users for which you want to set the permissions. The first digits signifies value for set user id (4) OR set group id (2) OR sticky bit(1). The rest of the three digits are used for setting permission for three classes of users.

It is also possible to set permission using 3 digits only leaving the permission for user id, group id and stick bit unset. So the permission 0755 and 755 are the same.

Set user id

If a file with set user ID permission is set, then the file is executed as if by the owner of the file rather than the user who is executing the file. For example, /bin/mount is commonly owned by root and has permissions 4755 where the digit 4 signifies that, even if the file is executed by a normal user, it will run with the owner’s (root’s) privileges since the file is owned by root . The following example will show how to set the suid bit for a file.

Set group id

SGID can be set to both files and directories and is represented symbolically by g and numerically by 2 . When a directory has the sgid bit set, any files or directories created inside it will inherit the group ID of the directory. To set the sgid bit for a directory, use the following chmod command.

Find if the sgid bit is set for the directory using the ls command.

The seventh character in the group permission section ( ‘s’ ) signifies that the sgid bit is set for groups.

Sticky bit

The next access mode bit is called the sticky bit and is represented symbolically by t and numerically by 1 . This bit works on directories only. With sticky bit set on a directory, anyone can create files or directories inside it. Files owned by other users cannot be deleted except his own files and directories.

To add a sticky bit to other types of users, use +t option in the chmod command.

To test if the sticky bit is set for the directory use the ls command:

There will be a t in the x bit section of other users. Also a lowercase t implies that the executable bit is also present, otherwise you would see a capital T

To remove the sticky bit use — sign in the chmod command:

Using chown to change ownership

There may be situations when you need to change the ownership of files and directories. The chown command as described below changes the owner and groups of files and directories.

To change the group ownership, specify a colon or dot followed by group name right after owner name with no spaces between them, the group ownership of the files is changed as well.

If no group name is mentioned after colon or dot followed by OWNER, then the user is made the owner of the files and the group of the files is changed to owners login group.

If the owner name is omitted right before colon or dot and a group name is mentioned afterwards then the group ownership is changed. In this case, chown performs the same function as chgrp .

To change the owner and group of a directory recursively use -R switch:

Now that you have a basic idea of permissions in Linux and its usage through chmod and chown , you can now implement a proper permissions policy to secure your system.

Источник

Читайте также:  Astra linux как примонтировать дисковод
Оцените статью