Linux redirection and user and group management

Source: Internet
Author: User
Tags stdin

I/O redirection and piping for bash

1, Linux under the general default input is called the standard input, that is, keyboard input, the default output is called the standard output, that is, the display output. Linux provides three I/O devices for the program:

Standard input (STDIN)--0: Accept input from the keyboard by default

Standard output (STDOUT)--1: Default output to terminal window

Standard error (STDERR)--2: Default output to terminal window

I/O redirection changes the default location.

Output redirection Common syntax:

CMD > File: Redirect stdout to file, the contents of the original file will be overwritten;

CMD >> file: Append the stdout to the file file based on the original files;

CMD 2> File: redirect stderr to file, the contents of the original files will be overwritten

CMD 2>> file: On the basis of the original files, append the stderr to the file;

CMD &> File: Merge stdout and stderr redirect to the file, the original file will be overwritten;

CMD &>> File: The merged stdout and STDERR are appended to the file based on the original file;

cmd &> file equals cmd > file 2>&1

cmd &> file equals cmd > file 2>>&1

(Cmd1;cmd2 ...) > File: Merge stdout of multiple programs to redirect to files;

Input redirection Common syntax:

cmd < file: stdin with the contents of the file as a cmd command;

CMD << EOF: Redirects the multi-line input of the keyboard to stdin until it terminates the input with the EOF Terminator;


2, pipe character (|)

The function of the pipe character is to "|" The standard output of the preceding command serves as the standard input for subsequent commands, and multiple pipe characters can be used at the same time.

It is important to note that:

(1) The pipeline command only handles the correct output of the previous command and does not process the error output

(2) The latter command of the pipeline command must be able to receive the standard input stream.

Application Examples:

]# Ls-l/etc | Less: Pagination shows the contents of the LS output


3. Character Conversion command: TR

Usage: TR [options] ... SET1 [SET2]

Replace, reduce, or delete characters from standard input and write the results to standard output

Common options:

-c,-c: Using SET1 's complement

-D: Delete SET1 matching content, do not replace

-S: A character that matches the consecutive repetition of a SET1 in a single character

-T: Converts the first character set corresponding character to the character corresponding to the second character set

Set is a set of strings, or it can be expressed in one way or another:

\NNN: Expression with octal values (1 to 3 bits)

\ \: Backslash

\a: Alarm Province

\b: Backspace

\f: Page Change

\ n: Line break

\ r: Enter

\ t: Horizontal tab

\v: Vertical tab


Ii. User and Group management

1, the Linux user is divided into the administrator and the ordinary user, the administrator uid is 0, the ordinary user's UID range is 1-65535, in which the ordinary user also divides into the system user and the login user two kinds:

System User: 1-499 (CENTOS6 and previous version system) 1-999 (CENTOS7 system)

Login User: CENTOS6 (previous version system) 1000+ (CENTOS7 system)

The Linux user group also divides into the Administrator group and the General Group, the Administrator group's GID is 0, the general group's GID scope is: 1-65535, in which the common group divides into the system group and the non-system group Two kinds:

System Group: 1-499 (CENTOS6 and previous version system) 1-999 (CENTOS7 system)

Non-system group: 1000+ (CENTOS6 and previous version system) (CENTOS7 system)

For a user, there can be several different groups, the primary group and the additional group, the master group name is the same as the user name, and only one user, also called the private group, one user must belong to one and only one primary group. Groups other than the primary group belong to the user's additional group, and one user can belong to 0 or more additional groups.


2. Linux user and group related profiles are:

/ETC/PASSWD: User and attribute information (name, UID, primary group ID, etc.) are recorded

/etc/group: User groups and their attribute information are logged

/etc/shadow: User password and related attributes are recorded

/etc/gshadow: The group password and its related properties are recorded


The/etc/passwd file contains 7 fields, respectively:

(1) Login name: Login user Name

(2) passwd: password (usually denoted by x, because the password information is stored in another file)

(3) UID: User ID

(4) GID: Group ID

(5) GECOS: User quanming or annotation information

(6) Home directory: User Home

(7) Shell: The shell that the user uses by default


The/etc/group file contains 4 fields, respectively:

Group name, group password, group ID, group member (list of users with the current group as additional groups, comma separated),


The/etc/shadow file contains 9 fields, respectively:

(1) User name: Login user Name

(2) Password: Encrypted password, generally use SHA512 encryption

(3) Date of last password change: from January 1, 1970 to password last changed time

(4) Minimum password age: the password can be changed in a few days (default 0 means it can be changed at any time)

(5) Maximum password Age: Password must be changed in a few days (default 99999 means never expire)

(6) Password warning period: A few days before the password Expires system alert User (default is one week)

(7) Password disable period: The password expires in a few days after the account will be locked

(8) Expiration date of the account: the number of days after which the account expires from January 1, 1970

(9) Reserved fields


The/etc/gshadow file contains 4 fields, respectively:

Group name, group password, group Administrator list, list of users with the current group as additional groups (multiple users separated by commas)


3. Getent command

Usage: getent [Options ...] database [key ...]

Getent passwd: Equivalent to CAT/ETC/PASSWD, which can be followed by a user, to display a user's information separately;

Getent Group: Equivalent to Cat/etc/group, followed by a user group, used to display the information of a user group alone;

Getent Shadow: Equivalent to Cat/etc/shadow, followed by a user, to display a user's password information separately;

Getent Gshadow: Equivalent to Cat/etc/gshadow, followed by a user group, to display the password information of a user group separately;


4. CHFN command

CHFN Username: Set A user's detailed comment information, such as display name, phone number, etc.

5. Finger command

Finger Username: View detailed comment information for a user

6. Chsh command

Chsh [-S Shell] [-l] [-u] [-v] [username]: Set a user's shell

7. Useradd command

Usage: useradd [options] Login name

Common options:

-C: Specify user's annotative description information, need to use quotation mark "";

-D: Specify the user's home directory

-D: Represents the default option for modifying a user (modifying a/etc/default/useradd file)

-G: Specifies the primary group of the new user, using the same group name as the user name if not specified

-G: Specify additional groups for new users, can have multiple, separated by commas, the group must be created beforehand.

-M: Do not create a user's home directory

-N: Do not create a group with the same name

-O: Allow user to be created with duplicate UID

-r: Create a System user

-S: Specify the user's default shell

-u: Specify UID for user



8. Groupadd command

Usage: groupadd [options] Group

Common options:

-F: If the group exits successfully, and if the specified GID already exists, the effect of the-G option is canceled

-G: Specify GID

-O: Allow groups to be created with duplicate GID

-R: Creating a System Group


9. Usermod command

Usage: usermod [options] Login name

Common options:

-C: Modify Comment description information

-D: Specify the user's new home directory, the files in the original home directory will not be moved to the new home directory at the same time, to move need to use the-M option

-U: Specifies the user's new UID

-G: Specify user new GID

-G: Specifies the user's new additional group, the original additional group will be overwritten, to retain the original additional group need to use the-a option

-S: Specify the user's new default shell

-L: Specify the user's new login name

-L: Lock the specified user

-U: Unlock the specified user

-e: Specifies the expiration date of the user account, in the format YYYY-MM-DD


10. Groupmod command

Usage: groupmod [options] Group name

Common options:

-G: Specify a new GID

-N: Change the new group name


11. Userdel Command

Usage: userdel [options] Login name

Common options:

-r: Delete user home directory and mail


12. Groupdel command

Usage: Groupdel [options] Group


13. passwd command

Usage: passwd [Options ...] < account name >

Common options:

-D: Delete the account password, only the administrator user can perform this action

-L: Locks the specified user, only an administrator user can perform this action

-U: Unlocks the specified user for which only an administrator user can perform this action

-N mindays: Specifies the minimum age for which only an administrator user can perform this action

-X maxdays: Specifies the maximum age for which only an administrator user can perform this action

-W Warndays: How many days ahead of warning, only an administrator user can do this

-I inactive-days: The number of days after the password expires the account will be disabled, only the administrator user can do this

--stdin: Accepts a user password from standard input, only an administrator user can perform this action

-S: reports the user's password status, only the administrator user can perform this action


14. GPASSWD command

Usage: gpasswd [options] Group

-A User: Add user to the specified group

-D User: Removes the user from the specified group


15. Groups Command

Usage: Groups [user name] ...

Displays the user group where the specified user is located and, if no user is specified, defaults to the current process user


16. Groupmems command

Usage: groupmems [options] [action]

Options:

-G: Specify user groups

Action:

-A user: To add users to the specified group

-D User: Removes the users from the specified group

-P: Remove all members from the specified group

-L: List all members in a group



17:id command

Usage: ID [options] ... [User Name]

Common options:

-U: Display UID only

-G: Only valid GID is shown

-G: Show all Group IDs

-N: Display group names instead of numbers


18. Su command

Usage: su [options] ... [-] [user [parameters] ...]

Su Username: Non-logon switch, that is, the target user's profile will not be read

Su-username: Login switch, will read the target user's configuration file, completely switch

Su-username-c ' command ': Run a command as a user




This article is from the "Linux Operational Learning path" blog, please be sure to keep this source http://fengliang.blog.51cto.com/3453935/1833598

Linux redirection and user and group management

Related Article

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.