Linux: chgrp/chmod/chown/ln command details

Source: Internet
Author: User

Preface

Groupadd test create test user group

Useradd user1 create user1 user

Passwd user1 set user1 Password

Useradd user2 create user2 user

Passwd user2 set user2 Password

Gpasswd-A user1 test add user1 to the test user group

Gpasswd-A user2 test same as above

 

1. chgrp (Change Group)
Function Description:The group to which the file or directory belongs.
Syntax:Chgrp [-cfhrv] [-- help] [-- version] [group] [file or directory...] or chgrp [-cfhrv] [-- help] [-- reference = <reference file or directory>] [-- version] [file or directory...]
Note:In the UNIX system family, the control of file or directory permissions is managed by the owner and Its group. You can use the chgrp command to change the group to which the file and directory belong. You can set the group name or group ID.
Parameters:
-C or -- changes has a similar effect to the "-V" parameter, but only the changed part is returned.
-F or -- quiet or -- silent Do Not Display error messages.
-H or -- no-dereference only modifies the file connected by the symbol, without changing any other related file.
-R or -- Recursive recursive processing: all files and subdirectories under the specified directory are processed together.
-V or -- verbose displays the command execution process.
-- Help Online Help.
-- Reference = <reference file or directory> sets all groups of the specified file or directory to the same group as that of the reference file or directory.
-- Version: displays the version information.

For example:

# Chgrp groupname file/Dir
# Chgrp-r groupname file/DIR <recursively process all files and subdirectories in the specified directory together>
# Chgrp-RV groupname file/DIR <display Command Execution Process>

2. chmod (Change Mode)
Function Description: Change the permissions of a file or directory.
Syntax: Chmod [-cfrv] [-- help] [-- version] [<permission range> +/-/= <permission settings...>] [file or directory...] or chmod [-cfrv] [-- help] [-- version] [digit code] [file or directory...] or chmod [-cfrv] [-- help] [-- reference = <reference file or directory>] [-- version] [file or directory...]
Note: In the UNIX system family, the control of file or directory permissions is divided by reading, writing, and executing three general permissions, and three special permissions are available, then, combine the owner with the management permission range of the group. You can use the CHMOD command to change the permissions of files and directories. The permission of the symbolic connection cannot be changed. If you modify the permission of the symbolic connection, the permission change will be applied to the connected original file. The permission range is expressed as follows:
U: user, the owner of a file or directory.
G: group, that is, the group to which the file or directory belongs.
O: other. All Users except the file or directory owner or group belong to this range.
A: All, that is, all users, including owners, groups, and other users.
The permission code is listed below:
R: Read Permission, number code: "4 ".
W: Write Permission. The number is "2 ".
X: execute or switch the permission. The number is "1 ".
-: You do not have any permission. The digit code is "0 ".
S: Special? B> Function Description: Change the file or directory permissions.
Parameters:
-C or -- changes has a similar effect to the "-V" parameter, but only the changed part is returned.
-F or -- quiet or -- silent Do Not Display error messages.
-R or -- Recursive recursive processing: all files and subdirectories under the specified directory are processed together.
-V or -- verbose displays the command execution process.
-- Help Online Help.
-- Reference = <reference file or directory> sets all the permissions of the specified file or directory to the same as those of the reference file or directory.
-- Version: displays the version information.
<Permission range> + <permission settings> enable this permission setting for files or directories in the permission range.
<Permission range>-<permission settings> disable this permission setting for files or directories in the permission range.
<Permission range> = <permission settings> specify the permission range for this permission setting for files or directories.

For example: 

# Chown username file/Dir
# Chown-r username file/DIR <recursively stores all files and subdirectories in the specified directory

Set the file file1.txt to accessible to all: chmod Ugo + R file1.txt

Set the file file1.txt to accessible to all: chmod A + R file1.txt

Set file1.txt and file2.txt as the owner of the archive, which can be written to the same group to which the archive belongs, but not to others: chmod ug + W, o-w file1.txt file2.txt

Set ex1.py to chmod U + x ex1.py only.

Set all files and sub-projects in the current directory to anyone who can read: chmod-r a + R *

When another user implements the Oracle sqlplus program, his identity is temporary ORACLE: chmod U + S sqlplus due to this program.

In addition, chmod can also use numbers to imply permissions such as chmod 777 file.

Syntax: chmod ABC File

In this example, each A, B, and C is a number, which means that the permissions of user, group, and other are exclusive.

R = 4, W = 2, x = 1

If you want the rwx attribute, 4 + 2 + 1 = 7;

If the RW-attribute is required, 4 + 2 = 6;

If you want the R-x attribute, 4 + 1 = 5.

Chmod A = rwx file and chmod 777 file have the same results

Chmod ug = rwx, O = X file and chmod 771 file have the same results

If you use chmod 4755 filename, you can grant root permissions to this program.

If it is a root user, the Administrator has the right to check all files.

 

3. chown (change owner)
Function Description: Change the owner or group of files or directories.
Grammar: Chown [-cfhrv] [-- dereference] [-- help] [-- version] [owner. <group>] [file or directory...] or chown [-chfrv] [-- dereference] [-- help] [-- version] [. group] [file or directory...] or chown [-cfhrv] [-- dereference] [-- help] [-- reference = <reference file or directory>] [-- version] [file or directory...]
Note:In the UNIX system family, the control of file or directory permissions is managed by the owner and Its group. You can use the CHOWN command to change the owner or group of the file and directory. You can use the user name or user identification code to set the group, and use the group name or group identification code to set the group.
Parameters:
-C or -- changes has a similar effect to the "-V" parameter, but only the changed part is returned.
-F or -- quite or -- silent Do Not Display error messages.
-H or -- no-dereference: Modify the file connected to the symbol without changing any other related file.
-R or -- Recursive recursive processing: all files and subdirectories under the specified directory are processed together.
-V or -- version displays the command execution process.
-- Dereference has the same effect as the "-h" parameter.
-- Help Online Help.
-- Reference = <reference file or directory> set the owner of the specified file or directory and the owner of the group to be the same as that of the reference file or directory.
-- Version: displays the version information.

For example: 

# Chmod + R file/DIR <Add read permission>
# Chmod + W file/DIR <write permission>
# Chmod + X file/DIR <add execution permission>
# Chmod-R + rwx file/DIR <add all permissions and recursively process all files and subdirectories in a specified directory>
# Chmod-r 777 file/DIR <add all permissions and recursively process all files and subdirectories in the specified directory> cancel Permissions
# Chmod-r file/DIR <cancel read permission>
# Chmod-W file/DIR <cancel write permission>
# Chmod-X file/DIR <cancel execution permission>
# Chmod-r-rwx file/DIR <cancel all permissions and recursively process all files and subdirectories in a specified directory>
# Chmod-R 0 file/DIR <cancel all permissions and recursively process all files and subdirectories in a specified directory>

4. ln (Link)
Function Description: Connect to a file or directory.
Syntax: Ln [-bdfinsv] [-S <character tail backup string>] [-v <backup mode>] [-- help] [-- version] [source file or directory] [target file or directory] or Ln [-bdfinsv] [-S <tail backup string>] [-v <backup mode>] [-- help] [-- version] [source file or directory...] [destination Directory]
Note: Ln commands are used to connect files or directories. For example, if you specify more than two files or directories at the same time, and the final destination is an existing Directory, all the previously specified files or directories will be copied to this directory. If multiple files or directories are specified at the same time and the destination is not an existing Directory, an error message is displayed.
Parameters:
-B or -- backup is deleted to overwrite the backup before the target file.
-D,-F, or -- directory creates a directory hard connection.
-F or -- force forcibly establish a connection to a file or directory, regardless of whether the file or directory exists.
-I or -- Interactive ask the user before overwriting existing files.
-N or -- no-dereference treats the target directory of the symbolic connection as a general file.
-S or -- symbolic creates a symbolic connection to the source file instead of a hard connection.
-S <suffix backup string> or -- suffix = <suffix backup string> after you use the "-B" parameter to back up the target file, a backup string is added at the end of the backup file, the default backup string at the end of the character is the symbol "~ ", You can change it through the"-s "parameter.
-V or -- verbose displays the command execution process.
-V <backup mode> or -- version-control = <backup mode> use the "-B" parameter to back up the target file. A backup string is added at the end of the backup file, this string can be changed not only by the "-s" parameter. When you use the "-V" parameter <backup mode> to specify different backup modes, different backup strings are generated at the end of the string.
-- Help Online Help.
-- Version: displays the version information.

These commands must be learned and used flexibly. users and groups in FreeBSD have strict permission management. We often use these commands. Therefore, you must be skilled in using it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.