Linux is the front end will be skills, the front is a lot of children’s shoes to walk again big front line, the back-end to mention the, small make up to you set the tens of thousands of words (due to space constraints in this paper, has cut) about the practical basis of Linux command, may say that these are must master, of course, these aren’t enough, the small make up will continue to update, You can pay attention to my dynamic, disk, this wave can be bookmarked.

ls

One of the most frequently used commands.

Command format: ls [OPTION]… [FILE]… Pure input:

[root@iz2ze76ybn73dvwmdij06zz /]# ls
bin   dev  home  lib64       media  opt    proc  run   srv  tmp  var
boot  etc  lib   lost+found  mnt    patch  root  sbin  sys  usr  www
Copy the code
Add option-l which shows permission, owner, owner group, size, time, file name
[root@iz2ze76ybn73dvwmdij06zz /]# ls -l
The total amount of 68LRWXRWXRWX 1 root root 7 8 26 22:36 bin -> usr/binDr-xr-xr-x. 5 root root 4096 August 26 22:39 bootDrwxr-xr-x 19 root root 2960 4月 11 12:59 devDrwxr-xr-x. 81 root root 4096 9月 5 17:09 etcDrwxr-xr-x. 2 root root 4096 April 11 12:59 HOMELRWXRWXRWX 1 root root 7 8月 26 22:36 lib -> usr/libLRWXRWXRWX 1 root root 9 8月 26 22:36 lib64 -> usr/lib64DRWX ------. 2 root root 16384 October 15 2017 Lost +foundDrwxr-xr-x. 2 root root 4096 April 11 12:59 mediaDrwxr-xr-x. 2 root root 4096 4月 11 12:59 MNTDrwxr-xr-x. 3 root root 4096 August 26 22:40 optDrwxr-xr-x 2 root root 4096 August 26 23:00 patchDr -xr-xr-x 110 root root 0 4月 11 12:59 procDr-xr-x --. 12 root root 4096 9月 11 20:59 rootDrwxr-xr-x 26 root root 760 9月 7 21:50 runLRWXRWXRWX 1 root root 8 8月 26 22:36 sbin -> usr/sbinDrwxr-xr-x. 2 root root 4096 April 11 12:59 SRVDr-xr-xr-x 13 root root 0 April 11 12:59 sysDrwxrwxrwt. 9 root root 4096 September 15 03:19 TMPDrwxr-xr-x. 13 root root 4096 8月 26 22:36 usrDrwxr-xr-x. 19 root root 4096 8月 26 22:55 varDrwxr-xr-x 6 root root 4096 8月 26 22:39 WWWCopy the code
Ls -a (all) Linux ‘.’ (dot) files are hidden by default. -A can query all files.
[root@iz2ze76ybn73dvwmdij06zz /]# ls -a
.             bin   etc   lib64       mnt    .pearrc  run   sys    usr
. boot home lost+found opt proc sbin .test  var
.autorelabel  dev   lib   media       patch  root     srv   tmp    www
Copy the code
Ls -f displays folders with a “/” ending.
[root@iz2ze76ybn73dvwmdij06zz /]# ls -F
bin@   dev/  home/  lib64@       media/  opt/    proc/  run/   srv/  tmp/  var/
boot/  etc/  lib@   lost+found/  mnt/    patch/  root/  sbin@  sys/  usr/  www/
Copy the code
Display lS-R backwards, the default order is (a, B, C, D) in this order
[root@iz2ze76ybn73dvwmdij06zz /]# ls -r
www  usr  sys  sbin  root  patch  mnt    lost+found  lib   etc  boot
var  tmp  srv  run   proc  opt    media  lib64       home  dev  bin
Copy the code
Ls -r () recursively displays subdirectories
[root@iz2ze76ybn73dvwmdij06zz test]# ls -R
. :data

./data:
img  ./data/img: car product user  ./data/img/car:  ./data/img/product:  ./data/img/user: Copy the code
Ls-ls displays files in descending order by size
[root@iz2ze76ybn73dvwmdij06zz /]# ls -lS
The total amount of 72DRWX ------. 2 root root 16384 October 15 2017 Lost +foundDr-xr-xr-x. 5 root root 4096 August 26 22:39 bootDrwxr-xr-x. 81 root root 4096 9月 5 17:09 etcDrwxr-xr-x. 2 root root 4096 April 11 12:59 HOMEDrwxr-xr-x. 2 root root 4096 April 11 12:59 mediaDrwxr-xr-x. 2 root root 4096 4月 11 12:59 MNTDrwxr-xr-x. 3 root root 4096 August 26 22:40 optDrwxr-xr-x 2 root root 4096 August 26 23:00 patchDr-xr-x --. 12 root root 4096 9月 15 22:03 rootDrwxr-xr-x. 2 root root 4096 April 11 12:59 SRVDrwxr-xr-x 3 root root 4096 9月 15 22:10test Drwxrwxrwt. 9 root root 4096 September 15 03:19 TMPDrwxr-xr-x. 13 root root 4096 8月 26 22:36 usrDrwxr-xr-x. 19 root root 4096 8月 26 22:55 varDrwxr-xr-x 6 root root 4096 8月 26 22:39 WWWDrwxr-xr-x 19 root root 2960 4月 11 12:59 devDrwxr-xr-x 26 root root 760 9月 7 21:50 runLRWXRWXRWX 1 root root 9 8月 26 22:36 lib64 -> usr/lib64LRWXRWXRWX 1 root root 8 8月 26 22:36 sbin -> usr/sbinLRWXRWXRWX 1 root root 7 8 26 22:36 bin -> usr/binLRWXRWXRWX 1 root root 7 8月 26 22:36 lib -> usr/libDr -xr-xr-x 110 root root 0 4月 11 12:59 procDr-xr-xr-x 13 root root 0 April 11 12:59 sysCopy the code
Ls -g Does not output owner information
[root@iz2ze76ybn73dvwmdij06zz /]# ls -g
The total amount of 72LRWXRWXRWX 1 root 7 8 26 22:36 bin -> usr/binDr-xr-xr-x. 5 root 4096 8月 26 22:39 boot-rw-r--r-- 1 root 0 9月 15 22:20cDrwxr-xr-x 19 root 2960 4月 11 12:59 devDrwxr-xr-x. 81 root 4096 9月 5 17:09 etcDrwxr-xr-x. 2 root 4096 4月 11 12:59 HOMELRWXRWX 1 root 7 8月 26 22:36 lib -> usr/libLRWXRWX 1 root 9 8月 26 22:36 lib64 -> usr/lib64DRWX ------. 2 root 16384 October 15 2017 Lost +foundDrwxr-xr-x. 2 root 4096 4月 11 12:59 mediaDrwxr-xr-x. 2 root 4096 4月 11 12:59 MNTDrwxr-xr-x. 3 root 4096 August 26 22:40 optDrwxr-xr-x 2 root 4096 8月 26 23:00 patchDr-xr-xr-x 110 root 0 4月 11 12:59 procDr-xr-x --. 12 root 4096 9月 15 22:18 rootDrwxr-xr-x 26 root 760 9月 7 21:50 runRoot 8 8月 26 22:36 sbin -> usr/sbinDrwxr-xr-x. 2 root 4096 4月 11 12:59 SRVDr-xr-xr-x 13 root 0 4月 11 12:59 SYSDrwxr-xr-x 3 root 4096 9月 15 22:18test Drwxrwxrwt.9 root 4096 September 15 03:19 TMPDrwxr-xr-x. 13 root 4096 8月 26 22:36 usrDrwxr-xr-x. 19 root 4096 8月 26 22:55 varDrwxr-xr-x 6 root 4096 8月 26 22:39 WWWCopy the code
Ls-lg Hides information about all groups (belonging groups)
[root@iz2ze76ybn73dvwmdij06zz /]# ls -lG
The total amount of 72LRWXRWXRWX 1 root 7 8 26 22:36 bin -> usr/binDr-xr-xr-x. 5 root 4096 8月 26 22:39 boot-rw-r--r-- 1 root 0 9月 15 22:20cDrwxr-xr-x 19 root 2960 4月 11 12:59 devDrwxr-xr-x. 81 root 4096 9月 5 17:09 etcDrwxr-xr-x. 2 root 4096 4月 11 12:59 HOMELRWXRWX 1 root 7 8月 26 22:36 lib -> usr/libLRWXRWX 1 root 9 8月 26 22:36 lib64 -> usr/lib64DRWX ------. 2 root 16384 October 15 2017 Lost +foundDrwxr-xr-x. 2 root 4096 4月 11 12:59 mediaDrwxr-xr-x. 2 root 4096 4月 11 12:59 MNTDrwxr-xr-x. 3 root 4096 August 26 22:40 optDrwxr-xr-x 2 root 4096 8月 26 23:00 patchDr-xr-xr-x 110 root 0 4月 11 12:59 procDr-xr-x --. 12 root 4096 9月 15 22:18 rootDrwxr-xr-x 26 root 760 9月 7 21:50 runRoot 8 8月 26 22:36 sbin -> usr/sbinDrwxr-xr-x. 2 root 4096 4月 11 12:59 SRVDr-xr-xr-x 13 root 0 4月 11 12:59 SYSDrwxr-xr-x 3 root 4096 9月 15 22:18test Drwxrwxrwt.9 root 4096 September 15 03:19 TMPDrwxr-xr-x. 13 root 4096 8月 26 22:36 usrDrwxr-xr-x. 19 root 4096 8月 26 22:55 varDrwxr-xr-x 6 root 4096 8月 26 22:39 WWW10. Ls-li Displays the file index[root@iz2ze76ybn73dvwmdij06zz /]# ls -li Copy the code
Ls-li Displays the file index number
[root@iz2ze76ybn73dvwmdij06zz /]# ls -li

The total amount of 72  7628 lrwxrwxrwx    1 root root     7 8月  26 22:36 bin -> usr/bin
1179650 dr-xr-xr-x. 5 root root 4096 August 26 22:39 boot4923 -rw-r--r-- 1 root root 0 9月 15 22:20c1026 drwxr-xr-x 19 root root 2960 April 11 12:59 dev917505 drwxr-xr-x. 81 root root 4096 9月 5 17:09 etc 655362 drwxr-xr-x. 2 root root 4096 April 11 12:59 HOME 17 lrwxrwxrwx 1 root root 7 8月 26 22:36 lib -> usr/lib  13 lrwxrwxrwx 1 root root 9 8月 26 22:36 lib64 -> usr/lib64 11 DRWX ------. 2 Root root 16384 October 15 2017 Lost +found786434 drwxr-xr-x. 2 root root 4096 April 11 12:59 media131074 drwxr-xr-x. 2 root root 4096 4月 11 12:59 MNT393219 drwxr-xr-x. 3 root root 4096 August 26 22:40 opt1966081 drwXr-XR-x 2 root root 4096 August 26 23:00 patch 1 dr-xr-xr-x 110 root root 0 4月 11 12:59 proc 131073 dr-xr-x--. 12 root root 4096 9月 15 22:18 root 218 drwxr-xr-x 26 root root 760 9月 7 21:50 run  15 lrwxrwxrwx 1 root root 8 8月 26 22:36 sbin -> usr/sbin 393220 drwxr-xr-x. 2 root root 4096 April 11 12:59 SRV1 dr-xr-xr-x 13 root root 0 April 11 12:59 sys2097153 drwxr-xr-x 3 root root 4096 September 15 22:18test 393217 drwxrwxrwt. 9 root root 4096 September 15 03:19 TMP1048578 drwxr-xr-x. 13 root root 4096 8月 26 22:36 usr 262145 drwxr-xr-x. 19 root root 4096 8月 26 22:55 var 1441793 drwxr-xr-x 6 root root 4096 8月 26 22:39 WWWCopy the code
Ls-lt Creates files A and B in reverse order by modification time
[root@iz2ze76ybn73dvwmdij06zz test]# ls -lt
The total dosage of 0-rw-r--r-- 1 root root 0 9月 15 22:18b-rw-r--r-- 1 root root 0 9月 15 22:18aCopy the code
ls -version
[root@iz2ze76ybn73dvwmdij06zz test]# ls --version
Ls (GNU coreutils) 8.22Copyright (C) 2013 Free Software Foundation, Inc.
License: GPL + : the GNU general public license version 3 or updated version < http://gnu.org/licenses/gpl.html >.This software is free software: you are free to modify and redistribute it.There are no other warranties within the law. Written by Richard M. Stallman and David MacKenzie.Copy the code

cat

Cat, again, a relatively high frequency command. More officially, commands are used to link files and print them to standard output devices. This text output command can be used to view file contents, create content, etc. Here are some useful uses. Syntax: cat [options]… [file]…

Cat -n views and displays the line number
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat -n a
    1  this is a txt
    2  hello every body
    3  hello world
    4  hahaha
Copy the code
Cat creates an empty file b
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >b <<EOF
> EOF
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# ls
a  b
Copy the code
Cat Clears file contents
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >a<<EOF
> EOF
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a
Copy the code
Cat writes the content (if there was any content, it will be overwritten)
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat >a<<EOF
> today is a good day
> because off work
> hahaha
> EOF
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a today is a good day because off work hahaha Copy the code
Cat displays multiple file contents at the same time
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a > b
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat b
today is a good day
because off work
hahaha
Copy the code
Append content can be seen with a > after the content is appended
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat a >> b
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat b
today is a good day
because off work
hahaha
today is a good day because off work hahaha Copy the code
Cat –help (Try it yourself if you want to learn more)
[root@iz2ze76ybn73dvwmdij06zz linuxdemo]# cat --help
Cat [option]... [file]...Output [file] or standard input combination to standard output.
-A, --show-all = -vet-b, --number-nonblank Specifies the number of the non-blank output lineE equals - vE-e, --show-ends is displayed at the end of each line"$" -n, --number Specifies the number of all lines in the output-s, --squeeze-blank does not output multiple blank lines-t is equivalent to vT-t, --show-tabs displays the TAB character as ^I-u (ignored)-v, --show-nonprinting uses ^ and M- references, except for LFD and TAB --helpDisplay this help information and exit--version Displays the version information and exits If no file is specified, or the file is"-"Is read from standard input. Example:Cat f -g Outputs the contents of f, standard input, and g.Cat copies the contents of standard input to standard output. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Please report to < http://translationproject.org/team/zh_CN.html > cat translation errorsFor complete documentation, run: info coreutils'cat invocation' Copy the code

User permissions

Everyone knows that Linux is a multitasking, multi-user operating system. Multiple users can perform different tasks in the operating system at the same time, and one user can also log in multiple people. Generally speaking, root has the most permissions. The database we often use is usually a mysql user, and we may use a WWW account for web pages. We all know the story of deleting runaways, so some operation permissions should be managed by some special personnel. In order to study systematically, I think it is necessary to understand its basic knowledge.

Linux Role Classification

Super user :root (user ID –UID) 0 Has the highest management rights.

Common users:

System user UID:1-999(RHEL7 centos7) 1-499(RHEL6)    
Local user UID:1000+(rHEL7 centos7) 500+(rHEL6)Copy the code

UID: unique identifier of a user.

Linux group classification

Group:

Group classification:

According to the functions of accounts:
Super user group: root GID:0Common User Group:System user group: GID 1-- 999Local user group: GID 1000+Copy the code
Relationships between Linux users and groups

A user can belong to one group or multiple groups. Multiple users can belong to one group or multiple groups.

Configuration files for Linux users and groups

1./etc/passwd: indicates the user and attribute information.

2./etc/group: indicates the group and attribute information.

/etc/shadow: indicates the user password and attribute information.

/etc/gshadow: indicates the group password and attribute information.

5./etc/default/useradd: add the user rule file.

6./etc/login.defs: Set the limit of user accounts.

Linux user information file

One of the most important user management files, this file is open to all users, each line holds a user’s information, each attribute is separated by a colon.

cat /etc/passwd
root:x:0:0:root:/root:/bin/zsh
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin Copy the code

Example: the root: x: 0-0: root: / root: / bin/ZSH

1. Login user name :root

2. The user password placeholder is X

3. User UID: 0

4. User group GID:0

5. User account description :root

6. User home directory :/root

7. User default shell:/bin/zsh (default: /bin/bash)

Linux password information file

Only user root has the read permission for this file. Each line stores a record of the user, and each attribute is separated by a colon.

cat /etc/shadow
root:$6$LxE7qiaZ$ZZXf/aCO1Zqs1oOS9WDutUk9rFOq4MOoSG9IeXq2f5sduGrgfYvzOWHZmJZFGC.0Of1DjCna0M.oQZTY8r00/.:17769:0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
Copy the code

Example: bin:*:17110:0:99999:7:: 1. Login user name: bin

2. Encrypted password: * indicates that the password is locked

3. Date of last password change: 17110 indicates the number of days between 1970-1-1 when the password cannot be changed

4. Password change period: 99999 indicates that you never need to change the password. If it is any other number, it indicates the number of days from 1970-1-1 that the password must be changed.

5. Maximum password validity days: specifies the maximum password validity days.

6. Password expiration warning: the number of days before the password officially expires (-1, never prompted)

7. How many days after the password expires Disable user: can log in, single cannot operate.

8. User disabled date: how many days after the account expires, can not log in.

9. Reserve parameters

Linux group information file

The group information for the user group is stored here, with a group record in one row and properties separated by colons.

➜ ~ cat/etc/grouproot:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4: tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: Copy the code

Example: the root: x: 0:

1. Group name: root

2. Group password placeholder x

3. Group GID: 0

Installation package management basics

I believe that most people should be like me, the beginning of the use of Windows, it is familiar with the Windows platform installation package, such as.exe, is not too common, generally installed in Windows mysql, I believe that.msi is also seen. Most servers run Linux, so let’s take a look at the basics of the Linux installation package. Different distributions of systems use different tools. At present, most company servers are based on centos, so centos will be used as an example in the future.

Based on the redhat

yum

zypper

rpm

Based on centos

yum

rpm

rpm

Full name: Redhat Package Manager

Download it from http://rpmfind.net


The following uses RPM -5.4.10-10.i586. RPM as an example.

RPM: indicates the software package name

5: indicates the major version number

4: indicates the second version

10: Revision number

I586: CPU architecture platform

RPM: indicates the package suffix

yum

Solve the dependency problem of software package installation, automatically download software package, based on THE C/S architecture. The repo file is a configuration file for the YUM source that defines the repository information. Why is it a C/S architecture? The header of the original RPM software will record the dependency information of the modified software. Therefore, it is possible to analyze the content of the head to know its dependencies and record these information. Then the server is to provide these RPM packages, and then analyze the dependency of the package, the record will be stored in a directory on the server. The client downloads its record dependency files during software installation, analyzes them, and finally downloads them once.

Process Management Basics

Generally speaking, programs are divided into two categories, one is system program, one is application program. A running program is a process. A process occupies memory, and when you kill it, resources are released.

Type of process

User process: the user’s own program, which the user can control to turn on and off.

Interactive process: A process that interacts with users.

Batch process: a collection of processes that start other processes in sequence.

Daemon: a process that is always running. Crond.

Process properties

Process ID: (PID) Unique numeric identifier to distinguish different processes.

Processes have parents and children.

User ID(UID) and owner group of the user that started the process.

There are three states of a process:

  • Run – R

  • Dormancy, S

  • Zombies – Z

Priority of a process: The value ranges from -20 to 19. A smaller value indicates a higher priority. The default value is 0.

The terminal to which the process is linked.

Resources occupied by processes.

The relationship between parent and child processes

If the parent terminates first, the child is adopted by the init process. If the child terminates first and the parent does not call wait or waitPID, the process processes the dead state. If you want to avoid this situation, you need to call wait or waitPID.

Process management tool
Ps: View processes.Top: Displays dynamic process information.    kill: Kill process.Pstree: View the process tree.Pgrep: indicates a search process.Lsof: displays the open files of the process.Copy the code

Process Management practices

ps

Statically displays information about the current process.

Command: ps

Syntax: ps parameter

Common parameters:

A Displays all user processes. R Displays running processes. L Long format output U Displays processes by user name and startup timeCopy the code

Action: BSD format to display the process

➜  ~ ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Root 12 0.0 0.0 0 0? S November 03 0:00 [Migration /1]Root 13 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/1]Root 15 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 1:0hCopy the code

Standard format displays processes unix-style

 ~ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
Root 1 0 0 November 03? 00:00:01 /usr/lib/systemd/systemd --systeRoot 2 0 0 November 03? 00:00:00 [kthreadd]Root 3 2 0 November 03? 00:00:00 [ksoftirqd/0]Root 5 2 0 November 03? 00:00:00 [kworker/0:0H]Root 7 2 0 11月03? 00:00:00 [migration/0]Root 8 2 0 November 03? 00:00:00 [rcu_bh]Root 9 2 0 November 03? 00:00:05 [rcu_sched]Root 10 2 0 11月03? 00:00:00 [watchdog/0]Root 11 2 0 November 03? 00:00:00 [watchdog/1]Root 12 2 0 November 03? 00:00:00 [migration/1]Root 13 2 0 November 03? 00:00:00 [ksoftirqd/1]Root 15 2 0 November 03? 00:00:00 [kworker/1:0H]Root 17 2 0 November 03? 00:00:00 [kdevtmpfs]Root 18 2 0 November 03? 00:00:00 [netns]Root 19 2 0 November 03? 00:00:00 [khungtaskd]Root 20 20 November 03? 00:00:00 [writeback]Root 21 2 0 November 03? 00:00:00 [kintegrityd]Copy the code
Attribute details:

USER: indicates the owner of a process

PID: indicates the ID of a process

%CPU: indicates the percentage of processes in the CPU

%MEM: percentage of memory occupied by processes

VSZ: indicates the size of virtual memory occupied by a process

RSS: fixed memory usage

STAT Process Status

R is running, can pass in queue;
S is in the dormant state; T stop or be tracked; Z Zombie process; N Lower priority process L Some pages are locked into memory; The leader of the S process (there are child processes below it)Copy the code

Copy the code

START Time when the process is started.

TIME Indicates the CPU consumption TIME.

COMMAND Indicates the name and parameters of a COMMAND.

Sort by specified attribute:

Sort by CPU from smallest to largest.

➜ ~ ps aux --sort % CPUUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Copy the code

Sort by CPU from largest to smallest.

➜ ~ ps aux --sort -% CPUUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
Root 21342 0.3 0.4 137184 17112? Ssl 03:24 1:16 /usr/local/aegi
Root 1 0.0 0.0 43364 3596? Ss November 03 0:01 /usr/lib/systRoot 2 0.0 0.0 0 0? S November 03 0:00 [kthreadd]Root 3 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/0]Root 5 0.0 0.0 0 0? S< November 03 0:00 [kworker/ 0:0hRoot 7 0.0 0.0 0 0? S November 03 0:00 [Migration /0]Root 8 0.0 0.0 0 0? S November 03 0:00 [rcu_bh]Root 9 0.0 0.0 0 0? S November 03 0:05 [rcu_sched]Root 10 0.0 0.0 0 0? S November 03 0:00 [watchdog/0]Root 11 0.0 0.0 0 0? S November 03 0:00 [watchdog/1]Root 12 0.0 0.0 0 0? S November 03 0:00 [Migration /1]Root 13 0.0 0.0 0 0? S November 03 0:00 [ksoftirqd/1]Copy the code
top

In contrast to ps, the top command displays dynamic information about a process.

Command: top

Syntax: the top parameter

Common parameters:

Operation:

➜  ~ top
Top-11:38:42 UP 13:15, 2 Users, Load Average: 0.01, 0.03, 0.05Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
%Cpu(s): 0.2us, 0.0sy, 0.0Ni, 99.8ID, 0.0wa, 0.0hi, 0.0Si, 0.0stKiB Mem :  3881688 total,   229264 free,   543932 used,  3108492 buff/cache
KiB Swap: 1049596 total, 1049596 free, 0 used. 3026944 avail Mem   PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  1 root 20 0 43364 3596 2340 S 0.0 0.1 0:01.79 systemd 2 root 20 00 0 S 0.0 0.0 00.00 kthreadd3 root 20 00 0 S 0.0 0.0 0:00.14 ksoftirqd/05 root 0-20 00 0 S 0.0 0.0 00.00 kworker/0:+7 Root rt 00 0 S 0.0 0.0 0:00.33 Migration /08 root 20 00 0 S 0.0 0.0 00.00 rcu_bh9 root 20 0 0 0 S 0.0 0.0 0:05.73 rcu_schedCopy the code
Attribute Explanation:

The first five lines are collated statistics of the data.

The first line:

11:38:42 Current time     
Up 13:15 System running time. The format is minute      
2 user Indicates the number of current login users Load Average: 0.01, 0.03, 0.05 Average value of system load and average task queue length 1 minute,5 minutes, and 15 minutes ago.Copy the code

Line 2 and 3:

98 Total Total number of processes1 Running Number of running processes97 Number of sleeping processes0 Stopped Number of processes that have been stopped0 Zombie Number of zombie processes %Cpu(s): 0.2 us CPU usage of system user processes0.0sy Percentage of CPU usage by kernel processes0.0 ni Percentage of CPU usage in user process space by processes whose priorities have changed99.8 ID Percentage of idle CPUS0.0 wa Percentage of CPU time spent waiting for input and output0.0 HI Hardware CPU interrupt usage percentage0.0 Si Percentage of soft interrupt usage0.0 ST Percentage of VM usageCopy the code

Line 4 and 5:

Mem: (unit K)

3881688 Total Total physical memory229264 Free Total free memory543932 Used Total used physical memory3108492 Buff /cache The amount of memory in the kernel cacheCopy the code

Swap: (unit K)

1049596 Total Total number of switch areas1049596 Free Total number of free switch areas0 Used Indicates the total number of used switch areas3026944 Avail Mem Amount of memory availableCopy the code

Title:

PID: process id

USER: indicates the USER name of the process owner

PR: priority

NI: Process priority, nice value, negative -> high priority, positive -> low priority

VIRT: total virtual memory VIRT =swap + res

RES: actual memory size

SHR: indicates the size of shared memory

S: indicates the process status

D: uninterruptible sleep state R: running S: sleeping t: tracking z: zombie processCopy the code

%CPU: indicates the percentage of CPU usage since the last update

%MEM: percentage of physical memory used by a process

TIME+: indicates the total CPU usage of a process, expressed in 1/100 of a second

COMMAND: a COMMAND line

kill

Command: kill

Grammar:

      kill [-s signal|-p] [-q sigval] [-a] [--] pid...
      kill -l [signal]
Copy the code

Common parameters:

-l Lists all signal names. -s specifies the signal to be sent (default). -u specifies the userCopy the code

Action: Lists all signal names

➜  ~ kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ  VTALRM PROF WINCH POLL PWR SYSCopy the code

Forced termination

➜  ~ kill23423-9Copy the code
Termination of➜  ~ kill22323-15Copy the code

Kills all processes for the specified user

➜  ~ kill -u superh
Copy the code
The/proc directory

This directory contains information about the running status of the kernel and process.

➜ ls/proc1      21320  21628  240   392   7          diskstats    loadavg       swaps
10     21342  21976  242   42    722        dma          locks         sys
10353  21465  21977  243   44    7561       driver       mdstat        sysrq-trigger
10509  21613  21978  244   442   8          execdomains  meminfo       sysvipc
107 21614 22 249 45 803 fb misc timer_list 11 21615 220 26 46 8179 filesystems modules timer_stats 11010 21616 22285 261 466 8307 fs mounts tty 11102 21617 22287 262 467 8327 interrupts mtrr uptime 12 21618 22361 267 469 9 iomem net version 13 21619 22363 268 476 acpi ioports pagetypeinfo vmallocinfo 14461 21620 22396 3 477 buddyinfo irq partitions vmstat 15 21621 22467 31 485 bus kallsyms sched_debug zoneinfo 17 21622 225 3198 5 cgroups kcore schedstat 18 21623 229 32 505 cmdline keys scsi 19 21624 23 33 506 consoles key-users self 2 21625 23884 336 5217 cpuinfo kmsg slabinfo 20 21626 23885 34 5515 crypto kpagecount softirqs 21 21627 24 357 65 devices kpageflags stat Copy the code

Each label above is the directory name named after the PID of our current process.

free

View the used and free memory of the system.

Command: free

Syntax: Free parameter

Common parameters:

-b, --bytes displays -k, --kilo k displays -m, --mega m displays -g, --giga g displaysCopy the code

Operation:

➜ ~ free - m             total        used        free      shared  buff/cache   available
Mem:           3790         536         210           2        3043        2950
Swap:          1024           0        1024
Copy the code

The unit is G.

➜ ~ free - h            total        used        free      shared  buff/cache   available
Mem: 3.7g 538M 207M 2.4m 3.0G 2.9gSwap: 1.0 G 1.0 G 0 bCopy the code

File search

When doing operations related things, we need to look up files like xxx.conf, logs, and the location of an executable.

An overview of
which: View the location of the execution file.
Whereis: Views executable file locations and related files.
Locate: Works with the database cache to quickly view the location of a file. Grep: filter matches, it is a file search tool. Find: You can view files based on conditions.Copy the code
which

Command: which?

Syntax: the which parameter queries the target

Common commands: Generally, this command is used without parameters

--all, -a Displays all matched pathsCopy the code

Operation:

➜  ~ which pwd
/usr/bin/pwd
Copy the code

whereis

Command: whereis

Syntax: the whereis parameter queries the target

Common commands:

Generally, without parameters, -b searches only binary files. -b < directory > Defines the binary file search path. -m Searches only man manualCopy the code

Operation:

➜  ~ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
Copy the code
locate

Command: locate

Syntax: Locate file name

Common parameters: None

Actions: The locate and find commands do the same thing, but are more efficient because locate looks up a database and find looks up directory files.

Database:

➜ ~ the ls/var/lib/mlocate/mlocate db/var/lib/mlocate/mlocate.db
Copy the code

Related configuration files:

➜ ~ the ls/etc/updatedb. Conf/etc/updatedb.conf
Copy the code

Related scheduled tasks:

➜ ~ the ls/etc/cron. Daily/mlocate/etc/cron.daily/mlocate
Copy the code

For example:

➜  ~ touch shafa
➜ to locate shafa➜  ~ updatedb
➜ to locate shafa/root/shafa
Copy the code

Note: If we created the file that day to find, need to manually updatedB.

grep

Command: grep

Syntax: grep parameter target value file

Common parameters:

Take anti - v-i ignores case-n Prints the line number with the output* * begins with **$ends with *^ $a blank lineCopy the code

Operation:

Check the /etc/passwd line that contains root

➜  ~ grep root /etc/passwd
root:x:0:0:root:/root:/bin/zsh
operator:x:11:0:operator:/root:/sbin/nologin
Copy the code

Run the following command to query /etc/passwd without root:

➜  ~ grep -v root /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync Copy the code

Check /etc/passwd for the WWW line and display the line number

➜  ~ grep -n www /etc/passwd
23:www:x:1000:1000::/home/www:/sbin/nologin
Copy the code
find

Command: the find

Syntax: find path parameter output

Common parameters:

Path: For example,. Indicates the current directory, and/indicates the system root directory. -print: "\n" as delimiter, newline -print0: with xargs, "\0" as delimiterCopy the code

Find Common command options:

-name Searches for files by name. Name -perm finds files by file permissions. 666 777 Isodepth When searching for a file, first search the file in the current directory. -user Searches for files by file owner -atime-ctime (day) -mmin-cmin-amin (minute) -size n [c] Searches for files with n blocks in length. -follow If the find command encounters a symlink file, it follows it to the file the link points to.Copy the code

Operation:

View files starting with test. In the current directory

➜ ~ find.-name"test*"
./test.txt
./autojump/tests
./test.1
Copy the code

View files in the current directory starting with test.

➜ ~ find.-name"test*" -print
./test.txt
./autojump/tests
./test.1
Copy the code

View files starting with test. In the current directory. The output result is not displayed on a new line

➜ ~ find.-name"test*" -print0
./test.txt./autojump/tests./test.1
Copy the code

Execute the corresponding command on what you are looking for

The default action is -print

Common actions:

-exec can be followed by a custom shell commandCopy the code

Operation:

Query. TXT files with ls for more properties

test find . -name "*.txt" -exec ls -l {} \;
-rw-r--r-- 1 root root 0 11月 4 14:56./xq1.txt-rw-r--r-- 1 root root 0 November 4 14:56./xq.txt-rw-r--r-- 1 root root 0 11月 4 14:56./xq2.txtCopy the code

Query.txt files and change them to.bak files

test find . -name "*.txt" -exec mv {} {}.bak \;
test ls
xq1.txt.bak  xq2.txt.bak  xq.txt.bak
Copy the code

Logical query:

- a and-o or+ is higher than- belowCopy the code

Action: View files ending in. Sh or. Q.

test find . -name "*.sh" -o -name "*.q"
./book.q
./stop.sh
./start.sh
Copy the code

View files that end in. Sh and begin with s.

test find . -name "*.sh" -a -name "s*"
./stop.sh
./start.sh
Copy the code

View after inserting content into the file.

test ll
Total amount of 12 k-rw-r--r-- 1 root root  9 11月  4 15:10 book.q
-rw-r--r-- 1 root root 13 November 4 15:10 start.sh-rw-r--r-- 1 root root 4 November 4 15:10 stop.sh-rw-r--r-- 1 root root 0 11月 4 14:56xq1.txt.bak-rw-r--r-- 1 root root 0 November 4 14:56xq2.txt.bak-rw-r--r-- 1 root root 0 November 4 14:56xq.txt.bakCopy the code

View files whose value is greater than 40K and less than 50K in /etc

test find /etc/ -size +40k -a -size  -50k
/etc/selinux/targeted/active/modules/100/sysadm/hll
/etc/selinux/targeted/contexts/files/file_contexts.homedirs.bin
Copy the code

Schedule a task

When it comes to planning, it’s time. When it comes to tasks, it’s what to do. What do you do at any time? What time do you go to work every day? Every year’s National Day and New Year’s Day occur at fixed time nodes. Like these, we can call them timed tasks.

For example, sometimes. Quick off work, the leadership to an urgent task, Xiao Wang ah, add a shift. Tomorrow in a good mood, ask for a leave to interview. This is a temporary assignment.

So how do we do both of these things in Linux?

Task type

Timing: To do something repeatedly and continuously at regular intervals.

Temporary: The task ends after one execution, only once.

Command corresponding to the task type

Crontab: You will feel asphyxiated on a regular basis.

At (temporary mission): Perform a mission that ends once.

The use of the at

Command: the at

Grammar: at time

Common parameters:

-l Displays the task list. -c Displays the task detailsCopy the code

Operation: Check whether the service is enabled

[root@iz2ze0ajic0vbv28hcdctpz ~]# systemctl status atd
● ATD. Service-job spooling tools  Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
  Active: active (running) since 二 2018-11-13 11:50:47 CST; 1 day 9h ago
Main PID: 491 (atd)
 CGroup: /system.slice/atd.service └ ─ 491 / usr/sbin/atd - f November 13 11:50:47 IZ2Ze0AJic0VBv28hCDCTpz SystemD [1]: Started Job Spooling Tools.11月 13 11:50:47 IZ2Ze0AJIC0VBV28HCDCTPZ SystemD [1]: Starting Job Spooling Tools...Copy the code

Several ways to set tasks: Create a task (default: today, next day if the time is longer than the current time)

[root@iz2ze0ajic0vbv28hcdctpz ~]# ls
soft
[root@iz2ze0ajic0vbv28hcdctpz ~]# at 21:17
at> mkdir sk
at> <EOT>
job 6 at Wed Nov 14 21:17:00 2018 Copy the code

Create at specified time:

[root@iz2ze0ajic0vbv28hcdctpz ~]# at 20:00 2018-11-19
at> mkdir happy
at> <EOT>
job 7 at Mon Nov 19 20:00:00 2018
Copy the code

Add time from now on:

[root@iz2ze0ajic0vbv28hcdctpz ~]# at now + 3min
at> mkdir hello
at> <EOT>
job 8 at Wed Nov 14 21:41:00 2018
Copy the code

Tomorrow at 11:11

[root@iz2ze0ajic0vbv28hcdctpz ~]# at 11:11 tomorrow
at> mkdir world
at> <EOT>
job 9 at Thu Nov 15 11:11:00 2018
Copy the code

It will be executed at 3.24 PM two days later

[root@iz2ze0ajic0vbv28hcdctpz ~]# at 3:24 pm +2 days
at> mkdir new
at> <EOT>
job 11 at Fri Nov 16 15:24:00 2018
Copy the code

View the task list (shows tasks that have not yet been executed)

[root@iz2ze0ajic0vbv28hcdctpz ~]# atq
5  Thu Nov 15 21:11:00 2018 a root
Copy the code

View the information of a single task :(remove some information due to too much content)

[root@iz2ze0ajic0vbv28hcdctpz /]# at -c 7
#! /bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=243; export XDG_SESSION_ID HOSTNAME=iz2ze0ajic0vbv28hcdctpz; export HOSTNAME SHELL=/bin/bash; export SHELL HISTSIZE=1000; export HISTSIZE SSH_CLIENT=113.46.167.195\ 17211\ 22; export SSH_CLIENT SSH_TTY=/dev/pts/0; export SSH_TTY USER=root; export USER MAVEN_HOME=/usr/local/ apache maven - 3.6.0;export MAVEN_HOME MAIL=/var/spool/mail/root; export MAIL PATH=/usr/local/ apache maven - 3.6.0 / bin: / usr /local/ jdk1.8.0 _191 / bin: / usr /local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin; export PATH PWD=/root; export PWD JAVA_HOME=/usr/local/ jdk1.8.0 _191;export JAVA_HOME LANG=zh_CN.UTF-8; export LANG HISTCONTROL=ignoredups; export HISTCONTROL SHLVL=1; export SHLVL HOME=/root; export HOME LOGNAME=root; export LOGNAME CLASSPATH=.:/lib/dt.jar:/lib/tools.jar; export CLASSPATH SSH_CONNECTION = 113.46.167.195\17211\172.17.187.244\22;export SSH_CONNECTION LC_CTYPE=zh_CN.UTF-8; export LC_CTYPE LESSOPEN=\|\|/usr/bin/lesspipe.sh\ %s; export LESSOPEN XDG_RUNTIME_DIR=/run/user/0; export XDG_RUNTIME_DIR cd /root || {  echo 'Execution directory inaccessible'2 > & exit 1 } ${SHELL:-/bin/sh} << 'marcinDELIMITER550b4fbb' mkdir happy  marcinDELIMITER550b4fbb Copy the code

Parameter Description:

5: indicates the task number

Thu Nov 15 21:11:00 2018: Time

A: the queue

Root: indicates the execution user

Delete a task:

Let’s take a look at what the tasks are

[root@iz2ze0ajic0vbv28hcdctpz /]# atq
7  Mon Nov 19 20:00:00 2018 a root
5  Thu Nov 15 21:11:00 2018 a root
11  Fri Nov 16 15:24:00 2018 a root
9  Thu Nov 15 11:11:00 2018 a root
12 Wed Nov 14 22:11:00 2018 a root Copy the code

Example Delete task 12

[root@iz2ze0ajic0vbv28hcdctpz /]# atrm 12
[root@iz2ze0ajic0vbv28hcdctpz /]# atq
7  Mon Nov 19 20:00:00 2018 a root
5  Thu Nov 15 21:11:00 2018 a root
11  Fri Nov 16 15:24:00 2018 a root
9 Thu Nov 15 11:11:00 2018 a root Copy the code

Scheduled Task Processing

Cron is a scheduled task execution tool for Linux, crondtab is a periodic command execution, and Crond is a scheduled task daemon.

The service start
Check whether the service is startedCopy the code
[root@iz2ze0ajic0vbv28hcdctpz ~]# systemctl status crond
● Crond. Service-command Scheduler  Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
  Active: active (running) since 二 2018-11-13 11:50:47 CST; 1 day 10h ago
Main PID: 489 (crond)
 CGroup: /system.slice/crond.service └ ─ 489 / usr/sbin/crond - n 11月 13 11:50:47 IZ2ZE0AJIC0VBv28hCDCTpz SystemD [1]: Started Command Scheduler11月 13 11:50:47 IZ2ZE0AJIC0VBV28HCDCTPZ SystemD [1]: Starting Command Scheduler...11月 13 11:50:47 IZ2ze0AJIC0VBv28hCDCTpz crond[489]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 7... ed.)11月 13 11:50:47 iz2ze0ajic0vbv28hcdctpz crond[489]: (CRON) INFO (running with inotify support) Hint: Some lines were ellipsized, use -l to show in full. Copy the code
If no, run the commandsystemctl start crond
systemctl enable crond
Copy the code
Crontab command

Syntax: crontab parameter

Common parameters:

Crontab -u # Specify the cron information of the user crontab -l # List the cron information of the current user crontab -u user -l # List the cron information of the specified user crontab -r # Delete the cron service crontab -e # Edit the cron service crontab -r -u user # Delete the scheduled task for the specified userCopy the code

Operation:

Edit the Cron service

[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -e
crontab: installing new crontab
Copy the code

View tasks of the current user

[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -l
22 22 22 * * mkdir /linux/test
Copy the code

Delete the task

[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -r
[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -l
no crontab for root
Copy the code

Check out the tasks for the QA user

[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -u qa -l
22 22 22 * * mkdir /linux/test
Copy the code

Example Delete a scheduled task for user qa

[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -u qa -r
[root@iz2ze0ajic0vbv28hcdctpz ~]# crontab -u qa -l
no crontab for qa
Copy the code
System task

The task scheduling configuration file of the system is in /etc/crontab

[root@iz2ze0ajic0vbv28hcdctpz ~]# ll /etc/crontab
-rw-r--r-- 1 root root 451 Nov 15 07:49 /etc/crontab
Copy the code

You can add system tasks directly under /etc/crontab (not recommended).

[root@iz2ze0ajic0vbv28hcdctpz ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs  # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed Copy the code

Directories related to scheduled tasks

[root@iz2ze0ajic0vbv28hcdctpz etc]# ll cr
cron.d/       cron.deny     cron.monthly/ cron.weekly/
cron.daily/   cron.hourly/  crontab       crypttab
Copy the code

Description of directory functions:

Cron. d/ is a task that the system automatically needs to do periodically (not in hours, minutes, seconds, etc.).Cron. deny Users in the file cannot perform scheduled tasksOther files are executed scripts of different cycles.Copy the code
Scheduled Task Rule

First, let’s look at the format of the task (see figure):


Some special symbols of time:

Here are some examples:

# Nginx reloads at 20:30 per night
30 20 * * * /usr/local/nginx/sbin/nginx -s reload 

# Nginx reloads at 5:30 on the 1st, 10th, 15th of each month
1,10,15 * * /usr/local/nginx/sbin/nginx  -s reload  
 # Nginx is reloaded every 30 minutes between 20:00 and 22:00 every day 0,30 20-22 * * * /usr/local/nginx/sbin/nginx -s reload  Nginx is reloaded every Saturday at 10:00pm0 22* * 6 /usr/local/nginx/sbin/nginx -s reload  Nginx is reloaded every hour 0 */1 * * * /usr/local/nginx/sbin/nginx -s reload Copy the code

expect

Sometimes we may need a tool that can interact with Linux without human intervention. Here we introduce a tool Expect. This summary is mainly about how to use Expect.

Expect to introduce

TCL(Tool Command Language) is a shell-like language that you can use to do a lot of things. Expect builds on it, and Expect offers commands that TCL doesn’t. If you want to write a script that handles input and output automatically, I recommend Expect.

Expect to install

yum install -y expect

Expect to use
1. Define the shell used to execute the script. #! /usr/bin/expect 2. Set the timeout period, in seconds. Timeout -1 indicates that it never times out. 3. The shell spawn needs to run in an Expect environment to pass interactive instructions. 4. The expect command checks whether the output contains a certain string and does not return immediately. Otherwise, it returns after a period of time and uses timeout to set the waiting time. 5. Send Performs the interactive action and input the action to be performed to the interactive command. At the end of the command string, "\r" should be added to check if there is an abnormal waiting state. 6. Exp_continue Continue with the subsequent interaction operations. 7. After interact, interact keeps the interaction state and transfers control to the console. If no interact is added, the interface exits automatically.Copy the code
Log in to the server over SSH without password

Action script

➜  batch cat sf
#! /usr/bin/expect -f
spawn ssh  root@ip
expect {
"yes/no" { send "yes\r"; exp_continue}"*password:" { send "passwordstr\r" } } interact Copy the code

After viewing the result, the login succeeds

➜ batch. / 35spawn ssh root@ip
root@ip's password:
Last login: Sat Jan  5 10:15:56 2019 from 113.46.187.213
 Welcome to Alibaba Cloud Elastic Compute Service ! Copy the code

This is just the tip of the Linux ice, and it’s still on the way. ⛽ ️

This article is formatted using MDNICE