09.10.2021

Chown recursively for folders and files. Chown is a command to change the ownership of files and folders in linux. How to change the owner of a file


chown [-fhv] [-R [-H | -L | -P]] owner [:Group ] file... chown [-fhv] [-R [-H | -L | -P]] : group file... The program changes the owner and/or group of a file or directory, i.e. User ID And Group ID for the specified file. If the option is not specified -h, symbolic links passed as arguments are not changed

The following options are available: -H If the option is set -R, follow the links from the command line. (Links found while traversing the directory tree are not traced) -L If the option -R installed, follow all symbolic links. -P If option -R installed, do not follow the links. Default behavior. -R Recursion. Change User ID and/or Group ID for the entire tree of directories and files starting from the specified one. Beware of matching a hard link to the ".." parent directory when using the ".*" pattern. -f Do not report failed attempt to change ownership and group, and do not change the exit code of the operation to signal failure. -h If the file is a symbolic link, change User ID and/or Group ID just for the link itself. -v Mode for displaying messages about the progress of the program. If the option is specified more than once, chown will output the filename after old and new UserID/GroupID. Options -H, -L And -P, will be ignored if the option -R not installed. In addition, all these options override each other, and the behavior of the command chown, will be determined by the last option specified. Options owner And Group are optional, but at least one of them must be specified. If only specified Group, preceded by the sign : (colon). Parameter owner file, can be represented both as a numeric UserID expression, and as a symbolic name. If the username matches its UserID, the operand is used as "username". The same applies to the parameter Group. The ownership of a file can only be changed by the superuser, for security reasons. Program chown returns 0 on success and >0 in case of an error. SEE ALSO chgrp(1), find(1), chown(2), fts(3), symlink(7)

Change Ownership and Permissions on Files and Folders on Linux and *BSD Systems

The information below will be useful for beginners. linux And * BSD system administrators. By and large, in any operating system, be it Linux, Windows or FreeBSD, the correct operation of applications, services and the security of the system as a whole directly depends on the access rights to files and folders. Using the tools below, you can solve almost 90% of the problems that arise when setting access rights.

Change owner for all folders and files. For this purpose, Unix uses the command chown. It has a number of options:

  • Options. For example, in order for changes to affect not only this directory, but also all subdirectories, you need to apply the command recursively, i.e. with the "-R" option.
  • Owner or group of owners ("user:group" or "user").
  • Paths to folders/files to apply the command to. For example, "/mnt/pool1/dts1/backup".

Thus, the command to change the owner (and group of owners) for directories and all subfolders and files will look like this:

sudo chown -R user:group /home/user/dir/

Change permissions on a file or folder on Linux and *BDS systems, you can use the command chmod. It is worth noting that Unix has its own peculiarities in the work of this command and the principle of separation of rights. So, in addition to the owner of a file or folder, you can configure what operations this very owner and group, as well as all other users, can perform. In Unix, there is a separation of capabilities in the following way - the owner, the group of owners, and others. There is also the following division of rights:

  • The right to launch a file (for folders, navigate through the directory).
  • The right to modify a file (create and delete files and folders in a directory)
  • The right to read the file (for folders, this is the right to view the contents of the directory)

For ease of use and memorization, the following encoding is used:

  • launch - 1
  • change - 2
  • reading - 4

Adding up these digital designations, we get a set of rights granted to the owner / group / others. Let's analyze this information with an example:

Only the owner has the right to change the file, the owner group can read, the rest cannot do anything, respectively, the command will be like this:

chmod 640 /media/fileshare/file.txt

Everyone can create files in a folder, but only the owner can see them:

chmod 622 /media/fileshare/messages/

So that everyone can run a program, but no one except the owner can edit it:

chmod 711 /media/share/exec

To change the rights to all nested files and folders (i.e. recursively), the "-R" key is used again.

When performing these operations, you should understand that only administrators and the owner of these folders and files can change the rights to files and directories.

Change permissions for files only or folders only- command find. In this case, files are designated as file (f), and directories, respectively, as d. For example:

In this tutorial, you will learn how to change Linux/Unix permissions and set a new owner of a file/folder via command line. There are 2 basic commands that can be used for this purpose: chmod And chown.

Before you start this guide, you will need the following:

  • Command line access

Step 1 - How to change permissions in Linux via the command line

chmod- This command is used to change the permissions of a file or folder. Each file has user types that can interact with it:

Command ls-l, can be used to display permissions and owner. For example, the command ls -l file1.txt will display:

  • "-rwxr–rw-"- this part of the line shows access rights. There are 4 main letters that you need to pay attention to: r,w,x,d. d means the file type is a directory. In our example, there is no such letter (it would be the first in the line), here instead of it is the character “-“ (which basically means “no”). Letter x means permission to performance file or folder (this permission is required for entrance to a folder). Letter w means permission to write a file or folder (editing, deleting, etc.) And finally the last letter r, which means reading. If you have read permissions on the file, you can read the contents of the file, but you won't be able to take other actions (for example, you can read the script code, but you won't be able to execute it).
  • 1 is the number of hard links. Simply put, a hard link is an additional name for an existing file.
  • user user- This value shows the owner of the file and its group.
  • 0 - This value indicates the size of the file.
  • Jan 19 12:59 pm- Displays the date of the last modification.
  • file1.txt- Provides the name of a file or folder.

Okay now let's get back to the team chmod. This command allows us to change the permissions of a file or folder. We will teach you how to do it by simply adding numbers. Each of the access rights has its own room:

  • r(reading) - 4
  • w(record) - 2
  • x(execution) - 1

For example, if we want to change the permissions of a file file1.txt on those:

We have to write the following command :

Chmod 746 file1.txt

Each number in this command represents the rights for one of the user types(owner, group, and others). The first number is 7. Now, using the explanation of the numerical values ​​above, the only way to get the number 7 is by adding the numbers 4, 2 and 1, we get 4+2+1=7. Simply put, this means ALL access rights (read, write, execute - rwx). The first number sets the permissions for owner file. The second number 4, which stands for permissions -r (read), sets the permissions for owner group. The third number 6, using the same method as with the first number, 4+2=6, gives the group other, rights to reading(4) and entry(2) files. The third part of the team file1.txt) is the name of the file for which we are changing the permissions.

One more example:

Chmod 777 file2.txt

this command will give all rights for each user type ( owner, group And other).

Here is a list of the most common file permissions:

Meaning Numerical value Explanation
-rw--- 600 The owner can read and write the file.
-rw-r–r– 644 The owner can read and write the file, the group and others can only read.
-rw-rw-rw- 666 Owner, group and others can read and write.
-rwx-- 700 The owner can read, write and execute, the group and others do not have any access rights.
-rwx–x–x 711 The owner can read, write and execute, the group and others can only execute.
-rwxr-xr-x 755 The owner can read, write and execute, the group and others can read and execute.
-rwxrwxrwx 777 All types of users can read, write and execute.

The most common permissions for directories are:

There are other ways to change permissions in Linux using the command chmod. But our personal recommendation would be to study one of these commands and its use (in this case, the method with numbers). If you want to know more about other ways to change permissions, you can read about it.

Step 2 - Change Ownership of a File or Folder via Command Line

chown– the command is used to change the owner of a file or folder. The most standard syntax for this command is:

Chown [owner/owner group] [filename]

For example, if we have a file " demo.txt” and we want to change the owner to “ jerry” and the owner group to “ clients”, we need this command:

Chown jerry:clients demo.txt

As you can see, we have separated the owner and owner group with the symbol “ : ” (colon). If we want to change only the owner of the file, we use this code:

Chown jerry demo.txt

We simply removed the owner group and added a new file owner, in which case the owner group will remain unchanged.

Another similar example, if we want to change only the owner group:

Chown:clients demo.txt

In this case, the owner group will change to clients(the owner will remain the same).

Step 3 - Using Additional Options with the chmod and chown Commands

One of the main options that works with both commands is -R which means recursive. This option allows you to change the permissions and ownership of a file, a given folder, and ALL other files inside it.

IMPORTANT! Be very careful with this option, if you use it incorrectly you can change the permissions and the owner ALL files on your system, leading to errors in operation and spending a huge amount of time rolling back changes.

Other options for "chmod" and "chown":

  • “-f” – silent or power option. This option will hide most error messages.
  • "-v" - makes diagnostics of each file affected by the command.

Different users in the operating system have ownership and permissions to keep files secure and impose restrictions on who can change the contents of files. On Linux, there are different users who use the system:

  • With every user some properties are associated, such as user ID and home directory. We can add users to a group to simplify the user management process.
  • Group may have zero or more users. The specified user may be associated with a "default group". It can also be a member of other groups in the system.

Ownership and permissions. To protect and protect files and directories in Linux, we use permissions to control what a user can do with a file or directory. Linux uses three types of permissions:

  • Reading: this permission allows the user to read files and directories, it allows the user to read the directories and subdirectories stored in it.
  • Recording: this permission allows the user to modify and delete the file. It also allows the user to change its contents (create, delete and rename files in it) for directories. If execute permission is not given for directories, changes do affect them.
  • Run: write permission to the file allows you to execute it. For example, if we have a file named php.sh so if we don't give it execute permission it won't run.

File Types Permissions:

  • User: This type of file permission affects the owner of the file.
  • Group: This file permission type affects the group that owns the file. Instead of group permissions, user permissions will apply if the owner user is in that group.
  • Other: these types of file permissions affect all other users of the system.

Note. To view permissions we use:

Command chown used to change the owner of a file or group. Whenever you want to change ownership, you can use the chown command.

Syntax:

chown [OPTION]… [OWNER] [:[GROUP]] FILE… chown… –reference=RFILE FILE…

Example: to change the owner of a file:

Chown owner_name filename

In our case, we have files like this:

Now if I use file1.txt in my case, to change ownership, I will use the following syntax:

Chown master file1.txt

where master- another user in the system. Suppose if you are a user named user1 and want to change ownership to root (where your current directory is user1). use "sudo" before the syntax.

Sudo chown root file1.txt

Options:


Examples:



adsense2code6

My personal notes arrow_drop_up

Sometimes hosting, for one reason or another, may need to change the rights to folders and files. For example, if Apache turned out to be their owner when unpacking the archive directly on the server. Below we have given a list of commands for changing the rights and ownership of folders and files.

1. How to change permissions for such files and folders

chmod command using SSH access. For example, with the following UNIX shell command:

chmod -R 755 /var/www/isbircom/data/temp

permissions 755 will be set recursively on all subdirectories and files of the directory /var/www/sitenamecom/data/temp

If you need to change the permissions only for files, but not folders, or vice versa, you can do this with the following Unix commands:

find /www/ -type f -exec chmod 644 () \; - only changes files

find /www/ -type d -exec chmod 755 () \; - changes only folders

2. How to change the owner of files and folders

The chown command in the UNIX shell. For example with the command:

chown -R admin:adminweb /var/www/sitenamecom/data/www/

will be set recursively by the owner of files and directories user admin and group adminweb to all subdirectories and files of the directory /var/www/ sitenamecom /data/www/

If you want to change the owner of files only, but not folders, or vice versa, you can do this with the following Unix commands:

find /www/ -type f -exec chown user:group () \; - only changes files

find /www/ -type d -exec chown user:group () \; - changes only folders

3. Is it possible to change the priority of rights for the user and apache

No, since by default the Apache web server is launched in Plesk under the apache user and group of the same name, this cannot be changed. To temporarily change the rights to the directories you need, you can create a Cron job with the same UNIX shell command.


2022
maccase.ru - Android. Brands. Iron. news