Change owner of linux file. Chown - command to change the owner of files and folders in Linux

Home / Laptops

Each file or folder in Linux has its own permissions.

There are 3 types of rights:
read(r),write(w),execute(x)

Rights are defined for 3 types of users:
owner(u), group(g), all others (o)

So, the rights of each file are 9 bits of information (3 types of rights * 3 types of users), respectively, each bit can be set (=1) access is allowed, or reset (=0) access is denied.

When assigning rights, it is customary to use the octal number system.

Text representation of rights (10 characters):
- rwxr- - r - -
0123456789

Character at position 0:
"-" - regular file, "d" - folder, "l" - link

Symbols in position 1-3:
"rwx" - owner rights

Symbols in positions 4-6:
"r - -" - rights for the group

Symbols in position 7-9:
"r - -" - rights for everyone else

Table for determining rights:

As can be seen from the table, rights are easily calculated
simple summation. 1=execute, 2=write, 4=read.
for example, execution(1) and reading(4) are required, we get 1+4=5

Example 644:
- owner(u) read and write
- group(g) read only
- others(o) read only

Example 755:
- owner(s) of all rights
- group (g) read and execute
- other(o) reading and execution

In programming languages ​​such as C,
0 is added to the rights at the beginning of each number, which means that the number is octal, i.e. write 0644 instead of 644. When changing rights from the command line, this is usually not required.

Four-digit representation of rights (4755), additional rights
SUID, SGID, sticky bits:

  • SUID or setuid: change "User ID" at runtime
  • SGID or setgid: change "Group ID" at runtime
  • sticky bit: disables deletion for all users except the owner. Previously, the bit meant "keep" in memory after the process has completed
Table. Numeric representation additional rights:

Textual representation of additional bits:
Additional bits are indicated by the following symbols:
"S","s" - SUID, SGID
"T","t" - sticky bit
the position of these bits coincides with positions (3,6,9) of execution rights "x"
however, if there are no execution rights, " upper case" i.e. large characters. If execution rights are set, " lower case" i.e. small characters.

examples:

SUID set:
- r w s - - - - - - SUID bits set and execution for owner
- r - S - - - - - - SUID bit set, execution bit not set

SGID set:
- r w x r w s - - - SGID bits set and group execution
- r w x r - S - - - SGID bit set, not set. execution for group

sticky bit set:
- r w x r w x r w t sticky bits set and execution for others
- r w x r w x r - T sticky bit is set, but not set. performing for others

Differences in the effect of rights when installing on files and folders:

rightsfilesfolders
reading reading contentgetting a list of files
record content recordingchanging the file list
(creation, deletion, renaming)
executionthe file can be executeduser or process can make the folder "current/working"
SUID Set "User ID" when executing Override user ID when creating file/folder (use root folder owner ID)
SGID Set "Group ID" when executing Override group ID when creating file/folder (use group ID of root folder)
sticky prevent deletion by everyone except the owner

change command chmod right:
chmod [-R] file/folder permissions

"-R" apply permissions recursively to all files and subfolders

Examples:
chmod -R 755 folder/
chmod 644 file
etc.

It is possible to change rights not entirely, but only to add or revoke rights only for certain groups of users
using symbols instead of octal numbers.
"u" - user/owner
"g" - user group
"o" - other users
"a" - all user groups

Permission format [[+-=], ...

Examples:
chmod -R a+x folder/ execute for all groups recursively
chmod a+rx file read and execute for everyone
chmod u+rx,g-rx file add read and execute for owner, and
revoke permissions for a group
chmod a+r,o= file add read for all group types and revoke
all rights for all other users

chown owner change command:

Chown [-R] owner:group file/folder

Examples:
chown -R 0:0 folder/ set owner/group = root recursively
chown myuser:mygroup file set myuser/mygroup for file

Change ownership and permissions of 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 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 these purposes, Unix uses the command chown. It has a number of parameters:

  • 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" parameter.
  • The owner or group of owners (“user:group” or “user”).
  • Paths to folders/files to which the command should be applied. For example, "/mnt/pool1/dts1/backup".

Thus, the command to change the owner (and owner group) 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 in Linux and *BDS systems you can use the command chmod. It is worth noting that Unix has its own peculiarities in the operation 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 same owner and group, as well as all other users, can perform. In Unix, capabilities are divided in the following way - owner, owner group, and others. There is also the following division of rights:

  • The right to run a file (for folders - traversing the directory).
  • The right to modify a file (creating and deleting files and folders in a directory)
  • The right to read a 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

By adding these digital symbols we get a set of rights granted to the owner/group/others. Let's look at this information using an example:

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

chmod 640 /media/fileshare/file.txt

Everyone can create files in the 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 attached files and folders (i.e. recursively), the “-R” switch is used again.

When performing these operations, it is worth understanding that only administrators and the owner of these folders and files can change the rights to files and directories.

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

Help with the chown command in Linux

In order to change the user and/or group of a file and/or folder in Linux, use the chown command. As usual, first of all, let's look at the hint operating system by this command:

Root@server:~# chown --help Usage: chown ... [:] FILE... or: chown ... --reference=RFILE FILE... Change the owner and/or group of each FILE to OWNER and/or GROUP. With --reference, change the owner and group of each FILE to those of RFILE. -c, --changes like verbose but report only when a change is made -f, --silent, --quiet suppress most error messages -v, --verbose output a diagnostic for every file processed --dereference affect the referent of each symbolic link (this is the default), rather than the symbolic link itself -h, --no-dereference affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink) --from= CURRENT_OWNER:CURRENT_GROUP change the owner and/or group of each file only if its current owner and/or group match those specified here. Either may be omitted, in which case a match is not required for the omitted attribute --no-preserve-root do not treat "/" specially (the default) --preserve-root fail to operate recursively on "/" -- reference=RFILE use RFILE"s owner and group rather than specifying OWNER:GROUP values ​​-R, --recursive operate on files and directories recursively The following options modify how a hierarchy is traversed when the -R option is also specified. If more than one is specified, only the final one takes effect. -H if a command line argument is a symbolic link to a directory, traverse it -L traverse every symbolic link to a directory encountered -P do not traverse any symbolic links (default) - -help display this help and exit --version output version information and exit Owner is unchanged if missing Group is unchanged if missing, but changed to login group if implied by a ":" following a symbolic OWNER and GROUP may be numeric. as well as symbolic. Examples: chown root /u Change the owner of /u to "root". chown root:staff /u Likewise, but also change its group to "staff". chown -hR root /u Change the owner of /u and subfiles to "root". GNU coreutils online help: Full documentation at: or available locally via: info "(coreutils) chown invocation"

How to quickly change the user of a file and/or folder in Linux

To reduce the time it takes to familiarize yourself with that command, I’ll immediately give you a ready-made recipe for how to quickly change the user of a file and/or folder in Linux. The format of this command is:

Chown user:group /patch/filename chown -R user:group /patch/foldername

In the first line, the user will be changed to user , the group will be changed to group for the file /patch/filename .

The second line in the example above uses additional option-R which means recursive processing. That is, the folder /patch/foldername will change its owner and group, but this will not end there; all folders and files located in this directory will also undergo the process of changing owner and group. Recursion can be very convenient when making global changes to a policy group in the system.

The remaining options of the chown command are not so often in demand and used, so I will not deal with them for now.

Each file or folder in Linux has its own permissions.

There are 3 types of rights:
read(r),write(w),execute(x)

Rights are defined for 3 types of users:
owner(u), group(g), all others (o)

So, the rights of each file are 9 bits of information (3 types of rights * 3 types of users), respectively, each bit can be set (=1) access is allowed, or reset (=0) access is denied.

When assigning rights, it is customary to use the octal number system.

Text representation of rights (10 characters):
- rwxr- - r - -
0123456789

Character at position 0:
"-" - regular file, "d" - folder, "l" - link

Symbols in position 1-3:
"rwx" - owner rights

Symbols in positions 4-6:
"r - -" - rights for the group

Symbols in position 7-9:
"r - -" - rights for everyone else

Table for determining rights:

As can be seen from the table, rights are easily calculated
simple summation. 1=execute, 2=write, 4=read.
for example, execution(1) and reading(4) are required, we get 1+4=5

Example 644:
- owner(u) read and write
- group(g) read only
- others(o) read only

Example 755:
- owner(s) of all rights
- group (g) read and execute
- other(o) reading and execution

In programming languages ​​such as C,
0 is added to the rights at the beginning of each number, which means that the number is octal, i.e. write 0644 instead of 644. When changing rights from the command line, this is usually not required.

Four-digit representation of rights (4755), additional rights
SUID, SGID, sticky bits:

  • SUID or setuid: change "User ID" at runtime
  • SGID or setgid: change "Group ID" at runtime
  • sticky bit: disables deletion for all users except the owner. Previously, the bit meant "keep" in memory after the process has completed
Table. Numerical representation of additional rights:

Textual representation of additional bits:
Additional bits are indicated by the following symbols:
"S","s" - SUID, SGID
"T","t" - sticky bit
the position of these bits coincides with positions (3,6,9) of execution rights "x"
However, if there are no execution rights, “upper case” is used, i.e. big symbols. If execute permissions are set, "lower case" is used i.e. small symbols.

examples:

SUID set:
- r w s - - - - - - SUID bits set and execution for owner
- r - S - - - - - - SUID bit set, execution bit not set

SGID set:
- r w x r w s - - - SGID bits set and group execution
- r w x r - S - - - SGID bit set, not set. execution for group

sticky bit set:
- r w x r w x r w t sticky bits set and execution for others
- r w x r w x r - T sticky bit is set, but not set. performing for others

Differences in the effect of rights when installing on files and folders:

rightsfilesfolders
reading reading contentgetting a list of files
record content recordingchanging the file list
(creation, deletion, renaming)
executionthe file can be executeduser or process can make the folder "current/working"
SUID Set "User ID" when executing Override user ID when creating file/folder (use root folder owner ID)
SGID Set "Group ID" when executing Override group ID when creating file/folder (use group ID of root folder)
sticky prevent deletion by everyone except the owner

command to change permissions chmod:
chmod [-R] file/folder permissions

"-R" apply permissions recursively to all files and subfolders

Examples:
chmod -R 755 folder/
chmod 644 file
etc.

It is possible to change rights not entirely, but only to add or revoke rights only for certain groups of users
using symbols instead of octal numbers.
"u" - user/owner
"g" - user group
"o" - other users
"a" - all user groups

Permission format [[+-=], ...

Examples:
chmod -R a+x folder/ execute for all groups recursively
chmod a+rx file read and execute for everyone
chmod u+rx,g-rx file add read and execute for owner, and
revoke permissions for a group
chmod a+r,o= file add read for all group types and revoke
all rights for all other users

chown owner change command:

Chown [-R] owner:group file/folder

Examples:
chown -R 0:0 folder/ set owner/group = root recursively
chown myuser:mygroup file set myuser/mygroup for file

© 2024 ermake.ru -- About PC repair - Information portal