Linux Notes 7-System Management

Process and service

Service Management Command

Red Hat 6 uses commands such as service chkconfig to manage system services. The commands used by Red Hat 7 are systemctrl.
The start, restart, stop, reload, and status view commands of the service are compared as follows (the two commands tested in CentOS7 support both):
Insert picture description here
Insert picture description here
service settings start, do not start, view service start at each level The status commands are compared as follows:
Insert picture description here

Monitor resources and management processes

There are many processes running in the Linux system at all times, and proper management of them helps optimize system performance. The system process has five states:
R running: running or waiting in the queue
S interrupt: sleeping, waiting for a certain condition to form or receive a signal.
D
Uninterruptible: The process must wait until an interrupt occurs. Z Zombie: The process has terminated, but the process descriptor still exists, until the parent process calls wait4() and releases it.
T Stop: The process receives SIGSTOP Stop running after SIGSTP SIGTIN SIGTOU signal

ps command

The ps command is used to view the process status in the system, the format is ps [parameter]
View process and status: ps -aux
view specific process information: ps -aux|grep 进程名
parameter
a displays all processes (including other users)
x displays processes that do not control the terminal
u displays users and others details

top command

The top command is used to monitor process activity and system load. The top
top command can be regarded as an enhanced version of the windows task manager. The execution effect is as follows

Insert picture description here
The information is explained as follows:
Insert picture description here

pidof command

Used to query the pid value of a specific program, format:pidof[参数][程序名称]

kill command

Used to terminate the process of a specific pid. The format is:kill [参数][进程pid]

[root@localhost ~]# ps -ef | grep sshd
root       1118      1  0 20:49 ?        00:00:00 /usr/sbin/sshd -D
root       8360   1118  0 20:49 ?        00:00:00 sshd: root@pts/0
root       9718   9666  0 21:05 pts/0    00:00:00 grep --color=auto sshd
[root@localhost ~]# kill -9 8360   

killall command

Used to terminate all processes with a specific name, format:killall [参数][进程名称]

Ctrl+c和Ctrl+z

When a command is running in the terminal, the command process can be terminated by the ctrl+c key combination.
A command is running in the terminal, and the process can be paused by the ctrl+z key combination (mounted to the background)

Operation example

Execute the script [root@localhost ~]# (while true ;do echo -n " working " >> ~/jobs.txt;sleep 1 ;done;)to see a one second data write ~ / jobs.txt every file.

[root@localhost ~]# (while true ;do echo -n " working  " >> ~/jobs.txt;sleep 1 ;done;)   --~/jobs.txt  开始每秒写入一个 working
^Z   --组合按键  Ctrl+z 后 进程被挂起,同时 ~/jobs.txt 内容写入暂停
[1]+  Stopped                 ( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done )
[root@localhost ~]# jobs --调用jobs 可以查看被挂起的进程
[1]+  Stopped                 ( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done )
[root@localhost ~]# bg  --执行 bg 可以让挂起的进程 在后台执行, 文件中又开始写入数据
[1]+ ( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done ) &
[root@localhost ~]# fg  --fg让后台挂起的进程在前台来执行
( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done )
^C -- 直接退出进程
[root@localhost ~]# 

When there are multiple processes in the background, you can operate a specific process through the task number in bg/fg +jobs

[root@localhost ~]# jobs
[1]-  Stopped                 ( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done )
[2]+  Stopped                 ( while true; do
    echo -n " working222  " >> ~/jobs.txt; sleep 1;
done )
[root@localhost ~]# bg 1
[1]+ ( while true; do
    echo -n " working  " >> ~/jobs.txt; sleep 1;
done ) &
[root@localhost ~]# fg 2
( while true; do
    echo -n " working222  " >> ~/jobs.txt; sleep 1;
done )

If the terminal executing the command exits, then the tasks that are executing in the background will stop.
If we want a certain command to be executed directly in the background, without displaying the output and waiting in the foreground, we can directly add the "&" symbol after the command.
as follows:

[root@localhost ~]# (while true ;do echo -n " working  " >> ~/jobs.txt;sleep 1 ;done;)&
[1] 13477

Remote control service

sshd service

SSH (SecureShell) is a protocol that can provide a secure remote login session and can execute Linux commands remotely.
ssh provides two security verification methods:
password-based security verification: log in to the remote host through account password security verification of the
expected secret key: After generating the secret key pair locally, the public key is transmitted to the server and the public secret key is compared .
The configuration file location of the sshd service is /etc/ssh/sshd_config, as follows:

[root@localhost ~]# cat /etc/ssh/sshd_config
#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
.......

The main configuration items are described as follows: After the
Insert picture description here
Insert picture description here
configuration is modified, the service must be restarted to take effect.

systemctl restart sshd.service

ssh command

Used for remote management of linux host, format:ssh [参数] 主机

-p 指定连接端口 默认 22
-v 显示连接过程详细信息 

Use the password to log in to the host 192.168.204.12 as follows

[root@localhost ~]# ssh 192.168.204.12
The authenticity of host '192.168.204.12 (192.168.204.12)' can't be established.
ECDSA key fingerprint is SHA256:P3koD/MoKyAms7BYfEvHnUb9EhIwHpHF5S2eU6nbpBU.
ECDSA key fingerprint is MD5:2e:fc:c1:98:71:3e:af:d4:d7:75:e8:19:60:50:e4:b4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.204.12' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Mon Nov 30 21:54:27 2020 from server03

SSH security key verification

The use of password authentication is in danger of brute force cracking and sniffing. You can use the secret key to verify the
first step: the local host generates a secret key pair and transmits the public key to the remote host

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LmSIbB7TgtRkfzwkiiDpYLyJogSF4za1fz9hB2P4Xcs [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|++.o . .         |
|Oo=.o +          |
|B++o.. +.        |
|+@ + ....+   .   |
|* O + o S + o .  |
|.o + + o + o E   |
|  .   o + o      |
|       . o       |
|          .      |
+----[SHA256]-----+
[root@localhost ~]# ssh-copy-id 192.168.10.12
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: ERROR: ssh: connect to host 192.168.10.12 port 22: Connection refused

[root@localhost ~]# ssh-copy-id 192.168.204.12
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.204.12'"
and check to make sure that only the key(s) you wanted were added.

Step 2: Modify the remote host, enable ssh service configuration to enable secret key authentication, disable password authentication, and restart the service

vi  /etc/ssh/sshd_config
PubkeyAuthentication yes
PasswordAuthentication no    不关闭也可以,关闭更安全

Step 3: The local host executes the ssh command to connect to the target host, you can connect directly

[root@localhost ~]# ssh  192.168.204.12
Last login: Mon Nov 30 21:55:29 2020 from server01
[root@server03 ~]# 

scp remote transmission command

If you want to transfer files to other linux hosts, you can use this command.
format:scp [参数] 本地文件 远程账号@目标ip:远程目录

[root@localhost ~]# scp -r test [email protected]:/root
[root@localhost ~]# ssh 192.168.204.12
Last login: Mon Nov 30 22:23:33 2020 from server01
[root@server03 ~]# ll
total 4
-rw-------. 1 root root 1260 Jun 10 05:26 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Dec  1 00:42 test

Parameter Description:

-v 显示详细连接进度
-P 指定远程主机的 sshd端口号
-r 传送文件夹时 加此参数
-6 使用ipv6协议

Uninterrupted conversation service

scren service

There is a problem when using the terminal to connect to the server for business operations. When the terminal is closed, the tasks running in the terminal will also be interrupted. For example, if the terminal is closed during file transfer, the file transfer will be interrupted, and the connection must be re-established. transmission. Screen is specially designed to solve the above problems. The user can use the screen command to control multiple command line sessions at the same time, with their own switching, and can also perform the following operations:
Session recovery: Even if the network is interrupted, the session can be resumed at any time.
Multi-window: Each session runs independently and has its own window Numbering, input and output, and cache.
Session sharing: Multiple users can use the same session from different terminals, and they can also see the same output.

I will summarize it later, skip it for now

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/110392579