IO redirection and user management commands


- #### IO redirection
```
I: input refers to the standard input descriptor with 0 for
O: output refers to the standard input descriptor with 1 for
         standard error and 2 for the descriptor
```
normal file or The standard input of a command refers to the keyboard, and the standard output is the terminal window.  
We use redirection to change their direction. The  
greater than sign > means standard output: directing the output to a file
```
> filename
 # > operation, will change the file "filename" to an empty file (that is, the size is 0 ). 
 # If the file does not exist, create a 0-length file (same effect as 'touch'). 
```
set -C disables overwriting existing files,  
but appends  
 \>| file to force overwriting   
 set +C allows to overwrite
>> filename  
```
# "operation, if the file does not exist, the file will be created, if the file exists, the output will be appended to the file "filename"
```
2> fliename   
```
2>filename Refers to redirecting the output of standard error to the file "filename"


2>> fliename 
````
2>> filename refers to appending the content of standard error output to the file "filename"
```
2>&1 filename
```
Reset standard error to standard output, send the output
of the error message, send to where stdout points to.
````
 &>filename
redirects both stdout and stderr to file "filename"
```
  Merge STDOUT of multiple programs
( cal 2007 ; cal 2008 ) > all.txt
```
   2>&1
Redirect stderr to stdout. 
< FILENAME
accepts input from a file. 
And ">" are paired commands, and are usually used in combination. 
<< means a multi-line redirect
<< followed by multiple The character at the start and end of line redirection, it can be any character, the start character must be the same as the end character, and the end character must be on a separate line
Example : take multiple lines between two E as the standard input of the cat command
[zhangxiao@zhangxiao ~]$cat <<E


````
> s
> s
> d
> d
> f
> s
> E
s
s
d
d
f f
s
[zhangxiao@zhangxiao ~]$
```


- #### tr
syntax: tr + [options] + parameters
Common options
```
-c or --complerment: replace All characters that do not belong to the character set; (reverse)
-d or --delete: delete all characters belonging to the first character set; (delete)
-s or --squeeze-repeats: replace consecutively repeated characters as a single character Represents; (compresses the same consecutive characters)
-t or --truncate-set1: first delete the characters that are more than the second character set in the first character set. (When the first character set has more characters than the second character set, the excess of the first character set will not take effect)
```
Example: Change the input lowercase letters to uppercase letters
```
[zhangxiao@zhangxiao ~]$tr [az] [AZ]
afdfdfyuc
AFDFDFYUC
sssssss
SSSSSSS
[zhangxiao@zhangxiao ~]$
```
Example: delete abcd in the input letter
```
[zhangxiao@zhangxiao ~]$tr -d "abcd"
ssfaasdasncvj
ssfssnvj
[zhangxiao@zhangxiao ~]$
```
- #### Pipe 
| The standard input
example
```
[zhangxiao@zhangxiao ~]$cat script.log | tr -d "abcde"
This sttmnt is snt to th log fil, "sript.log".
```
- #### tee
usage : cmd1 | tee [-a ]filename | cmd2


saves the standard output of command 1 to filename and serves as the standard input of cmd2
```
[zhangxiao@zhangxiao ~]$echo "hello" | tee pwa1
hello
[zhangxiao@zhangxiao ~ ]$la
-bash: la: command not found
[zhangxiao@zhangxiao ~]$ls
Desktop Documents Downloads helloword Music PAAA Pictures Public pwa1 script.log Templates test1 Videos
[zhangxiao@zhangxiao ~]$
```
- #### User rights management
> First of all, user rights are determined by tokens, which are obtained by logging in to the system , so the modified user permissions can only take effect after re-login to the system.  


Files related to users and groups
```
/etc/passwd: user and its attribute information (name, UID, primary group ID, etc.)
/etc/group: group and its attribute information
/etc/shadow: user password and its related Attributes
/etc/gshadow: Group password and its related attributes
```
passwd format:   
```
Username: password (X is a station character): UID: gid: Remarks: home directory: shell  
shaow format:  
username: password (encryption): mtime (since 1970): minimum password change time (since now): maximum validity period of password: expired days reminder: expired days lock: how long to expire (since 1970.1.1)
```
group format:  
```` Group
name: group password (version 7 is generally in gshaow): gid: list of users whose current group is an additional group```

gshdow:  
group name: group password: administrator list: users whose current group is an additional group


> These four user configuration files are usually not recommended to be modified directly. If modified, it is best to use commands  
> vipw and vigr Modify  
> pwck and grpck to check the modification Is there any error in the syntax of the file


- #### : useradd
syntax: useradd [options] username
Common options:


```
-u UID
-o with the -u option, do not check the uniqueness of the UID
-g GID: indicate the user belongs to Basic group, which can be group name or GID number
-c "COMMENT": user's comment information
-d HOME_DIR: use the specified path (non-existent) as the home directory
-s SHELL: specify the user's default shell program
Available list is in In the /etc/shells file
-G GROUP1[,GROUP2,...]: Specify additional groups for users, the group must exist in advance
-N Do not create a private group as the main group, use the users group as the main group
-r: Create a system user CentOS 6: ID<500, CentOS 7: ID<1000
-m Create a home directory for system users
-M Do not create a home directory for non-system users
```
Relevant files for new users are  
/etc/default/useradd  
/etc/skel/*  
/etc/login.defs 
- #### Create users in batches
- #### newusers  
newusers+parameters (you can create a pass file format file and pipe it to newusers)


- #### chpasswd
usage Same as above, batch modification of user passwords


- #### usermod
- Syntax: usermode [options] username
Common options:
```
-u UID: new UID
-g GID: new primary group
-G GROUP1[,GROUP2,... [,GROUPN]]]: new additional group, the original additional group will be overwritten; if the original one is retained, the -a option must be used at the same time
-s SHELL: new default SHELL
-c 'COMMENT': new comment information
-d HOME: The new home directory will not be created automatically; to create a new home directory and move the original home data, use the -m option
-l login_name: new name;
-L: lock specifies the user, in the /etc/shadow password column Add!
-U: unlock the specified user, remove the /etc/shadow password column!
-e YYYY-MM-DD: specify the expiration date of the user account
-f INACTIVE: set the inactivity
period```
Example: Add user zhangxiao to group xiaoxiao as an additional group```
[
root@zhangxiao ~]# usermod -G xiaoxiao zhangxiao
[root@zhangxiao ~]# id zhangxiao
uid=500(zhangxiao) gid=500(zhangxiao) groups= 500(zhangxiao),501(xiaoxiao)
```
- #### userdel
syntax userdel [option]...username
option - r delete user home directory


- #### id
syntax: id+[option]+username
Common options:
```
-u: display UID
-g: display GID
-G: display the ID of the group the user belongs to
-n: display name, need to be used with ugG``` Example: display the UID of user zhangxiao `
` ` [ root@zhangxiao ~]# id -u zhangxiao 500 [root@zhangxiao ~]# ``` - #### su usage su [-] username ```









su username: non-login switching, that is, the target user's configuration file will not be read, and the current working directory will not be changed
su - UserName: login switching, will read the target user's configuration file, switch to the home directory, and switch completely
`` `
Common usage su [-] UserName -c 'COMMAND'
- #### passwd
- Usage: passwd [OPTIONS] UserName:
```
Common options: -d: delete the specified user password
-l: lock the specified user
-u: Unlock the specified user
-e: Force the user to change the password at the next login
-f: Force the operation
-n mindays: Specify the minimum period of use
-x maxdays: The maximum period of use
-w warndays: How many days in advance to start warning
-i inactivedays: Inactive
period- -stdin: receive user password from standard input
echo "PASSWORD" | passwd --stdin USERNAME
```
- #### chage
change user password time
Syntax chage [OPTION]... LOGIN
```
-d LAST_DAY
-E - -expiredate EXPIRE_DATE
expiration time
-I --inactive INACTIVE
grace period for expiration
-m --mindays MIN_DAYS minimum modifiable time
-M --maxdays MAX_DAYS maximum validity period for passwords
-W --warndays WARN_DAYS prompt time --l
show password policy
```
- ## ## chfn
syntax: chfn+username
specify user information
chsh: specify shell
- #### finger View the logged in user and
description
Example```
[zhangxiao@zhangxiao ~]$finger
Login Name Tty Idle Login Time Office Office Phone
xiaoxiao zhangxiao tty3 Apr 3 10:45 bj 000 zhangxiao
zhangxiao tty2 1:00 Apr 3 08:15 zhangxiao zhangxiao
tty1 2:47 Apr 3 07:59 (:0)
zhangxiao zhangxiao pts/1 1:30 Apr 3 08:16 (vfn795y4y0a3 .mshome.net)
zhangxiao zhangxiao pts/2 Apr 3 09:24 (vfn795y4y0a39yv.mshome.net)
[zhangxiao@zhangxiao ~]$^C
```
- #### groupadd
syntax: groupadd [OPTION]... group_name
```
-g GID: specify the GID number
-r: create a system group
CentOS 6: ID<500
CentOS 7: ID<1000
```
Group attribute modification: groupmod
- #### groupmod [OPTION]... group
-n group_name: new name
```
-g GID: new GID Group
deletion: groupdel
groupdel GROUP
```
- #### groupmems [options] [action]
options:
```
-g, --group groupname Change to the specified group (only root)
Actions:
-a, --add username Specify the user to join the group
-d, --delete username remove user from group
-p, --purge remove all members from group
-l, --list display group member list
groups [OPTION].[USERNAME]... View user's group list
````

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687240&siteId=291194637