重要的配置文件路径

1)查看linux的内核参数

[root@k8s-test-abc-node-wqvmy ~]# cat /etc/sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
kernel.sched_wakeup_granularity_ns=15000000
kernel.sched_min_granularity_ns=10000000
kernel.sysrq=0
net.ipv4.ip_forward=1
net.ipv4.conf.all.send_redirects=0
...

2)

降权使用某个用户执行命令

举例:
1、使用registry用户,执行命令:sh /opt/galax/registry/script/log_backup.sh

su registry -s /bin/bash -c "sh /opt/galax/registry/script/log_backup.sh"

【zgrep】在某个目录下面批量的grep匹配字符串

1、zgrep:压缩文件中寻找匹配的正则表达式。用法和grep命令相同,只不过操作的对象是压缩文件。

zgrep 关键字 压缩文件路径
举例:
zgrep a xxx.zip                 // 从某个压缩文件中匹配某个关键字
zgrep 'aa bb cc' xxx.zip        // 从某个压缩文件中匹配某个关键词(带空格)
zgrep a ./*.zip                 // 从某个目录下面正则匹配多个压缩文件,再匹配关键字
举例:从压缩的日志文件里面搜索关键字
[gandalf@VRM01 ~]$ cat test.log 
1
2
3
4
5
5
[gandalf@VRM01 ~]$ zip test.zip ./test.log
  adding: test.log (stored 0%)
[gandalf@VRM01 ~]$ zgrep 5 ./test.zip
5
5
[gandalf@VRM01 ~]$ zgrep 4 ./*.zip
./test2.zip:4
./test.zip:4

【ps】 查看进程的详细信息

ps -eo pid,lstart,etime,cmd | grep registry

举例:
VRM01:/home/GalaX8800 # ps -eo pid,lstart,etime,cmd | grep registry
 289208 Thu Jan 12 11:35:57 2023    02:46:27 /opt/galax/registry/bin/registry serve /opt/galax/registry/conf/config.yml
2571143 Thu Jan 12 14:22:24 2023       00:00 grep --color=auto registry

lstart:启动时间。
etime:显示了自从该进程启动以来,经历过的时间,格式为 [[DD-]hh:]mm:ss。

【<<EOF】

说明:End Of File的缩写,表示自定义终止符;不固定,可以随意设置别名。

用法:注意需要成对出现

功能:使用<<EOF,告诉主shell,后续的输入,是其他命令或者子shell的输入,直到遇到EOF为止,再回到主shell。

<<EOF    // 开始
......
......
EOF      // 结束

也可以:

<<AAAA    // 开始
......
......
AAAA      // 结束

【lsof】查看指定端口对应的进程信息

lsof -i -P -n | grep 7443
ps -aux | grep 62015

举例:
VRM01:/home/GalaX8800 # lsof -i -P -n | grep 7443
java       37385      fcs   59u  IPv6 83542094      0t0  TCP 51.32.17.131:52540->51.32.17.130:7443 (CLOSE_WAIT)
java       62015      fcs   65u  IPv4   268229      0t0  TCP 51.32.17.130:7443 (LISTEN)
VRM01:/home/GalaX8800 # ps -aux | grep 62015
fcs        62015  0.4  4.8 4578004 370888 ?      Sl   Jan09   9:43 /usr/java/jre1.8.0_352/bin/java -Djava.util.logging.config.file=/opt/galax/vrmportal/tomcat/conf/logging.properties -server -Xms1024m -Xmx1024m -Xmn128m -Xss256k -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -XX:SurvivorRatio=1 -XX:LargePageSizeInBytes=128m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=75 -XX:SoftRefLRUPolicyMSPerMB=0 -Djava.security.egd=file:/dev/./urandom -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dorg.apache.catalina.connector.RECYCLE_FACADES=true -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true -Dorg.apache.tomcat.util.http.ServerCookie.FWD_SLASH_IS_SEPARATOR=false -Djava.endorsed.dirs= -classpath /opt/galax/vrmportal/tomcat/bin/bootstrap.jar:/opt/galax/vrmportal/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/opt/galax/vrmportal/tomcat -Dcatalina.home=/opt/galax/vrmportal/tomcat -Djava.io.tmpdir=/opt/galax/vrmportal/tomcat/temp -Djava.awt.headless=true org.apache.catalina.startup.Bootstrap start
root     2796270  0.0  0.0  22240  2356 pts/0    S+   15:22   0:00 grep --color=auto 62015
VRM01:/home/GalaX8800 # 

输出随机密码

[root@k8s-test]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c 16
3XR4KuUcRAQTXWLV
[root@k8s-test]# 

1、linux环境中通过/dev/urandom设备产生随机密码。
2、tr -dc '[:alnum:]'  # 所有的字母和数字
3、head -c 16          # head作用取多少字符,-c指取多少字节,用来定义密码的长度

开启 SFTP 上传

1、编辑 sshd 的配置文件,路径:/etc/ssh/sshd_config

2、找到 Subsystem;若没有该行,则直接添加,如果有,则取消前面的注释。

3、重启 sshd 服务:service sshd restart

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server -l INFO -f AUTH

开启历史命令

HISTSIZE=2000 // 定义 history 命令输出的记录数,也即是开启历史命令,为 0 表示关闭
history       // 显示终端执行过的命令
history 10    // 显示最近 10 条终端执行过的命令

查看某个目录所在的磁盘剩余大小

VRM01:/home/GalaX8800 # df -BG "/var/lib" | tail -1 | awk '{print $4}' | grep -oP '\d+'
6
VRM01:/home/GalaX8800 # df -BG "/var/lib"
Filesystem         1G-blocks Used Available Use% Mounted on
/dev/mapper/vg_vrm-lv_root    10G  4G    6G 40% /

追加或者替换文本到指定的文件

追加:echo "this is a text" >> /a/b.txt 
替换:echo "this is a text" > /a/b.txt

写入多行文本到文件并指定结束符

cat > text.conf <<EOF
XXXXXX
YYYYYYY
ZZZZZZ
EOF

说明:此时当前文件夹下面有一个text.conf文件,里面的内容就是上面指定的三行。

修改服务器主机名称

hostnamectl set-hostname k8s-node01

【scp】跨服务器文件拷贝

scp /etc/hosts root@51.32.17.130:/etc/hosts

再输入密码即可。

查询服务器的规格

1、一次性查看所有信息:lscpu

Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   42 bits physical, 48 bits virtual
CPU(s):                          4  // 总的逻辑数
On-line CPU(s) list:             0-3
Thread(s) per core:              1   // 单线程
Core(s) per socket:              4   // 单颗CPU核心数量,4核
Socket(s):                       1     // 物理CPU数,1个
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Xeon(R) Gold 5120 CPU @ 2.20GHz
Stepping:                        4
CPU MHz:                         2199.998
BogoMIPS:                        4399.99
Hypervisor vendor:               KVM
Virtualization type:             full
L1d cache:                       128 KiB
L1i cache:                       128 KiB
L2 cache:                        16 MiB
L3 cache:                        16 MiB
NUMA node0 CPU(s):               0-3
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Mitigation; PTE Inversion
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT Host state unknown
Vulnerability Meltdown:          Vulnerable
Vulnerability Mmio stale data:   Vulnerable: Clear CPU buffers attempted, no microcode; SMT Host state unknown
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP disabled, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall
                                  nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pd
                                 cm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3
                                 dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpc
                                 id mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsav

                                 es arat umip md_clear arch_capabilities

2、查看CPU个数:

第一种:top 之后,按 1

VRM01:/home/GalaX8800 # top
top - 11:26:37 up 13 days, 30 min,  2 users,  load average: 0.43, 0.46, 0.52
Tasks: 285 total,   2 running, 281 sleeping,   0 stopped,   2 zombie
%Cpu0  : 13.4 us, 20.1 sy,  0.0 ni, 65.2 id,  0.0 wa,  0.7 hi,  0.7 si,  0.0 st
%Cpu1  : 14.7 us, 24.0 sy,  0.0 ni, 60.0 id,  0.0 wa,  0.7 hi,  0.7 si,  0.0 st
%Cpu2  : 16.9 us, 26.2 sy,  0.0 ni, 55.3 id,  0.3 wa,  0.7 hi,  0.7 si,  0.0 st
%Cpu3  : 15.5 us, 20.5 sy,  0.0 ni, 62.0 id,  0.7 wa,  0.7 hi,  0.7 si,  0.0 st
MiB Mem :   7396.7 total,    249.5 free,   5463.8 used,   1683.4 buff/cache
MiB Swap:   5120.0 total,   3990.2 free,   1129.8 used.   1442.0 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                  
 349506 root      20   0       0      0      0 Z   3.0   0.0   0:00.09 qemu_hotreplace                                                          
    892 root      20   0   50660  15592  14248 S   1.7   0.2  85:36.43 systemd-journal                                                          
   2222 root      20   0  297756  12896   9536 S   1.7   0.2  80:24.48 rsyslogd                                                                 
 352810 root      20   0   16308  12296   4440 R   1.7   0.2   0:00.05 perl          

说明:看 %Cpu*,可以知道总的CPU核心数是4个。

第二种:

cat /proc/cpuinfo| grep "physical id" | sort| uniq| wc -l
VRM01:/home/GalaX8800 # cat /proc/cpuinfo| grep "physical id" | sort| uniq| wc -l
1

说明:总的CPU数量是1个。

3、查看单个CPU的核心数:

cat /proc/cpuinfo| grep "cpu cores"| uniq
VRM01:/home/GalaX8800 # cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores    : 4

说明:单颗 CPU 的核心数是 4 核。

4、查看总的内存:

free -h

VRM01:/home/GalaX8800 # free -h
       total    used    free   shared buff/cache  available
Mem:     7.2Gi    5.3Gi    272Mi    202Mi    1.6Gi    1.4Gi
Swap:     5.0Gi    1.1Gi    3.9Gi

说明:表示总的内存是 8G。free 是一个快速查看内容使用情况的办法,是对 /proc/meminfo 收集到的信息的一个概述。-h 是指以 human 能看懂的方式进行展示。

第二种:

cat /proc/meminfo
VRM01:/home/GalaX8800 # cat /proc/meminfo
MemTotal:    7574172 kB
MemFree:     299284 kB
。。。

5、查看磁盘信息:

1)查看磁盘挂载情况:lsblk // 只列出硬盘,不列出分区信息

VRM01:/home/GalaX8800 # lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                        11:0    1   438K  0 rom  
vda                       252:0    0   120G  0 disk 
├─vda1                    252:1    0   500M  0 part /boot
└─vda2                    252:2    0 119.5G  0 part 
  ├─vg_vrm-lv_root        253:0    0    10G  0 lvm  /
  ├─vg_vrm-lv_root_backup 253:1    0    10G  0 lvm  
  ├─vg_vrm-lv_home        253:2    0     5G  0 lvm  /home
  ├─vg_vrm-lv_tmp         253:3    0     1G  0 lvm  /tmp
  ├─vg_vrm-lv_vartmp      253:4    0     1G  0 lvm  /var/tmp
  ├─vg_vrm-lv_varlog      253:5    0    20G  0 lvm  /var/log
  ├─vg_vrm-lv_backuplog   253:6    0    20G  0 lvm  /var/backuplog
  ├─vg_vrm-lv_galax       253:7    0     2G  0 lvm  /etc/galax
  ├─vg_vrm-lv_gaussdb     253:8    0    25G  0 lvm  /opt/gaussdb
  ├─vg_vrm-lv_patch       253:9    0    20G  0 lvm  /opt/galax/patch
  ├─vg_vrm-lv_backup      253:10   0    40G  0 lvm  /var/backup
  └─vg_vrm-lv_swap        253:11   0     5G  0 lvm  [SWAP]
vdb                       252:16   0   171G  0 disk 
├─vg_krm-lv_temp          253:12   0    50G  0 lvm  /opt/krm/temp
├─vg_krm-lv_content       253:13   0   100G  0 lvm  /opt/krm/content
├─vg_krm-lv_registry      253:14   0    10G  0 lvm  /opt/krm/registry
└─vg_krm-lv_app           253:15   0    10G  0 lvm  /opt/krm/app
vdc                       252:32   0    41G  0 disk 
├─vg_vrm-lv_varlog        253:5    0    20G  0 lvm  /var/log
├─vg_vrm-lv_backuplog     253:6    0    20G  0 lvm  /var/backuplog
├─vg_vrm-lv_gaussdb       253:8    0    25G  0 lvm  /opt/gaussdb
├─vg_vrm-lv_patch         253:9    0    20G  0 lvm  /opt/galax/patch
└─vg_vrm-lv_backup        253:10   0    40G  0 lvm  /var/backup

2)查看硬盘及分区信息:fdisk -l

VRM01:/home/GalaX8800 # fdisk -l
Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xbc65c4a9

Device     Boot   Start       End   Sectors   Size Id Type
/dev/vda1  *       2048   1026047   1024000   500M 83 Linux
/dev/vda2       1028096 251656191 250628096 119.5G 8e Linux LVM

Disk /dev/vdb: 171 GiB, 183609851904 bytes, 358612992 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
...

3)以可读的格式输出硬盘使用情况:df -h

VRM01:/home/GalaX8800 # df -h
Filesystem                       Size  Used Avail Use% Mounted on
devtmpfs                         3.6G     0  3.6G   0% /dev
tmpfs                            3.7G     0  3.7G   0% /dev/shm
tmpfs                            3.7G   79M  3.6G   3% /run
tmpfs                            3.7G     0  3.7G   0% /sys/fs/cgroup
/dev/mapper/vg_vrm-lv_root       9.8G  3.7G  5.7G  39% /
/dev/mapper/vg_vrm-lv_patch       20G  243M   19G   2% /opt/galax/patch
...

【ps】查看进程启动的详细时间等信息

ps -eo pid,lstart,etime,cmd | grep registry 

举例:
VRM01:/home/GalaX8800 # ps -eo pid,lstart,etime,cmd | grep registry 
 460904 Sat May  6 14:13:49 2023       00:00 grep --color=auto registry
2607886 Sat May  6 01:25:06 2023    12:48:43 /opt/galax/registry/bin/registry serve /opt/galax/registry/conf/config.yml

添加sudo提权操作(ToDo)

方式:vi /etc/sudoers,细节百度

iptables 限制某个端口只能指定的客户端ip可以访问,其余的请求丢弃或者拒绝

待补充

【uname】显示操作系统信息

语法:

    -a 或--all  显示全部的信息,包括内核名称、主机名、操作系统版本、处理器类型和硬件架构等。。
    -m 或--machine  显示处理器类型。
    -n 或--nodename  显示主机名。
    -r 或--release  显示内核版本号。
    -s 或--sysname  显示操作系统名称。
    -v  显示操作系统的版本。
    --help  显示帮助。
    --version  显示版本信息。
    -p 显示处理器类型(与 -m 选项相同)。

1、查询当前服务器下载软件时需要匹配的操作系统和架构等信息:

[root@HN01 bin]# echo docker-compose-$(uname -s)-$(uname -m)
docker-compose-Linux-x86_64

2、查询全量的信息:

[root@HN01 harbor]# uname -a
Linux HN01 4.18.0-147.5.1.6.h451.eulerosv2r9.x86_64 #1 SMP Thu Apr 22 04:55:25 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Linux的压缩与解压缩

Q:常见的解压缩命令

1、解压缩 *.tgz 或者 *.tar.gz 类型:

tar -zxvf file.tgz // 解压到当前目录
tar -zxvf file.tgz -C directory // 解压到指定目录

2、解压缩 *.zip 类型:

 unzip file.zip // 解压到当前目录
 unzip file.zip -d directory // 解压到指定目录

3、解压缩 *.tar 类型:

tar -xf file.tar

4、解压缩 *.gz 类型:

gunzip file.gz

5、解压缩*.rar类型:(需要安装软件,https://www.rarlab.com/download.htm,如:rarlinux-x64-5.6.b5.tar.gz)

unrar file.rar

Q:常见的压缩命令

1、压缩成 *.tgz 类型:

2、压缩成 *.zip类型:

3、压缩成 *.tar类型:

【sed】SED命令使用

1、在isula的配置文件中,registry-mirrors 数组字段里面添加多个元素:

51.32.1.1:6000 http

sed -i '/insecure-registries/d' ./daemon.json && sed -i '$i,"insecure-registries":["51.32.1.1:7000"]' ./daemon.json
sed -i '/registry-mirrors/{n;s/.*/\t"51.32.18.113:5000", "51.32.18.112:5001"/;}' /etc/isulad/daemon.json


有:
sed -i '/insecure-registries/{n;s/.*/\t"51.32.18.113:5000", "51.32.18.112:5001"/;}' /etc/isulad/daemon.json
没有:
sed -i '/pidfile/a    "insecure-registries": [ "51.32.1.1:6000" ],' /etc/isulad/daemon.json



sed -i '/insecure-registries/!b add; /insecure-registries/c ,"insecure-registries":["51.32.1.1:9000"]; :add; $i,"insecure-registries":["51.32.1.1:9000"]' ./daemon.json

sed -n '/twle/!b Print; s/^/- /; :Print;p' test
sed -n '/insecure-registries/!b Print; /insecure-registries/c ,"insecure-registries":["51.32.1.1:9000"]; :Print;p' ./daemon.json

sed -i '/insecure-registries/!b add; :add; $i,"insecure-registries":["51.32.1.1:9000"]' ./daemon.json

sed -n '/insecure-registries/c ,"insecure-registries":["51.32.1.1:9999"]' ./daemon.json


sed -n '/twle/!b Print; s/^/- /; :Print;p' test
{
    "registry-mirrors": [
        "51.32.18.113:5000", "51.32.18.112:5001"
    ],
    "insecure-skip-verify-enforce": false,
    "insecure-registries": []
}
{
    "group": "isula",
    "default-runtime": "lcr",
    "graph": "/opt/k8s/cri/isulad",
    "state": "/var/run/isulad",
    "engine": "lcr",
    "log-level": "ERROR",
    "pidfile": "/var/run/isulad.pid",
    "log-opts": {
        "log-file-mode": "0600",
        "log-path": "/var/lib/isulad",
        "max-file": "1",
        "max-size": "30KB"
    },
    "log-driver": "stdout",
    "container-log": {
        "driver": "json-file"
    },
    "hook-spec": "/etc/default/isulad/hooks/default.json",
    "start-timeout": "2m",
    "storage-driver": "overlay2",
    "storage-opts": [
        "overlay2.override_kernel_check=true"
    ],
    "registry-mirrors": [
        "51.32.18.113:5000", "51.32.18.112:5001"
    ],
    "pod-sandbox-image": "registry.simbaos.com/pause:3.5",
    "native.umask": "secure",
    "network-plugin": "cni",
    "cni-bin-dir": "/opt/cni/bin",
    "cni-conf-dir": "/etc/cni/net.d",
    "image-layer-check": false,
    "use-decrypted-key": true,
    "insecure-skip-verify-enforce": false
}

Service 服务

说明:xxx.service 文件配置的服务常用 systemd 管理,有系统和用户的分别。

systemd 是 Linux 系统最新的初始化系统,对应的进程管理命令是 systemctl。systemctl 命令兼容了service,即 systemctl 也会去 /etc/init.d 目录下,查看,执行相关程序。

Systemd 默认从目录 /etc/systemd/system/ 读取配置文件。但是,里面存放的大部分文件都是符号链接,指向目录 /usr/lib/systemd/system/,真正的配置文件存放在 /usr/lib/systemd/system/ 目录。

举例:

启动 http 服务:systemctl start httpd
设置开机自启:systemctl enable httpd

1、存放路径:

系统:/user/lib/systemd/system/xxx.service

用户:/etc/lib/systemd/user/xxx.service

2、文件内容:

xxx.service 文件通常包含3个模块,即:

[Unit]:表示启动顺序和依赖关系;

[Service]:服务的启动行为以及如何启动、重启、停止等信息;

[Install]:表示如何安装配置文件。

3、内容详解:

[Unit]
    Description给出当前服务的简单描述。
    Documentation给出文档位置。
    After表示在什么服务之后启动。
    Before表示在什么服务之前启动。
    Wants表示该服务和某服务存在某种弱依赖关系,即某服务停止运行或退出不影响该服务继续运行。
    Requires则表示”强依赖”关系,即某服务停止运行或退出,改服务也必须停止运行。
    Wants与Requires只涉及依赖关系,与启动顺序无关,默认情况下是同时启动的。
    Conflicts:代表冲突的服务,即这个项目后面接的服务如果有启动,那么我们这个 unit 本身就不能启动,我们 unit 有启动,则此项目后的服务就不能启动,就是冲突性的检查。
注意:After和Before只涉及启动顺序,不涉及依赖关系。

[Service]
    Type :定义启动类型
        simple(默认值):ExecStart启动的进程为主进程
        forking:ExecStart将以fork()方式启动,此时父进程将会退出,子进程将成为主进程
        oneshot:类似于simple,但只执行一次,Systemd 会等它执行完,才启动其他服务
        dbus:类似于simple,但会等待 D-Bus 信号后启动
        notify:类似于simple,启动结束后会发出通知信号,然后 Systemd 再启动其他服务
        idle:类似于simple,但是要等到其他任务都执行完,才会启动该服务。一种使用场合是为让该服务的输出,不与其他服务的输出相混
    ExecStart:定义启动进程时执行的命令,就是手动启动时执行的命令。
    ExecReload:重启服务时执行的命令。
    ExecStop:停止服务时执行的命令。
    ExecStartPre:启动服务之前执行的命令。
    ExecStartPost:启动服务之后执行的命令。
    ExecStopPost:停止服务之后执行的命令。
    killmod:定义 Systemd 如何停止 sshd 服务。
        control-group(默认值):当前控制组里面的所有子进程,都会被杀掉
        process:只杀主进程
        mixed:主进程将收到 SIGTERM 信号,子进程收到 SIGKILL 信号
        none:没有进程会被杀掉,只是执行服务的 stop 命令
    Restart:定义了 sshd 退出后,Systemd 的重启方式。
        no(默认值):退出后不会重启
        on-success:只有正常退出时(退出状态码为0),才会重启
        on-failure:非正常退出时(退出状态码非0),包括被信号终止和超时,才会重启
        on-abnormal:只有被信号终止和超时,才会重启
        on-abort:只有在收到没有捕捉到的信号终止时,才会重启
        on-watchdog:超时退出,才会重启
        always:不管是什么退出原因,总是重启
对于守护进程,推荐设为on-failure。对于那些允许发生错误退出的服务,可以设为on-abnormal。
    RestartSec:表示 Systemd 重启服务之前,需要等待的秒数。
    user可以设置服务的用户名
    WorkingDirectory指定服务的安装目录
    RemainAfterExit:当设置为 RemainAfterExit=1 时,则当这个 daemon 所属的所有程序都终止之后,此服务会再尝试启动。这对于Type=oneshot 的服务很有帮助
    TimeoutSec: 若这个服务在启动或者是关闭时,因为某些缘故导致无法顺利 “ 正常启动或正常结束” 的情况下,则我们要等多久才进入 “ 强制结束 ” 的状态!
    EnvironmentFile:可以指定启动脚本的环境配置文件!例如 sshd.service 的配置文件写入到/etc/sysconfig/sshd 当中!你也可以使用 Environment= 后面接多个不同的Shell 变量来给予设置
    
[Install]
    WantedBy:表示该服务所在的 Target。 Target的含义是服务组,表示一组服务。WantedBy=multi-user.target指的是服务所在的Target是multi-user.target。
    Systemd 默认的启动 Target就是multi-user.target,在这个组里的所有服务,都将开机启动。
    查看 multi-user.target 包含的所有服务:systemctl list-dependencies multi-user.target
    Also:当目前这个 unit 本身被 enable 时, Also 后面接的 unit 也请 enable。 也就是具有相依性的服务可以写在这里。
    Alias:进行一个链接的别名,当 systemctl enable 相关的服务时则此服务会进行链接文件的创建,以multi-user.target 为例,这个组是用来作为默认操作环境default.target 的规划,因此当你设置成 default.target时,这个/etc/systemd/system/default.target 就会链接到/usr/lib/systemd/system/multi-user.target。

4、regsitry 服务示例:

[Unit]
Description=registry Service
After=network.service

[Service]
Type=forking
PIDFile=/var/run/registry.pid
ExecStart=/usr/bin/sh /opt/galax/root/registry/registryd.sh start
ExecStop=/usr/bin/sh /opt/galax/root/registry/registryd.sh stop
ExecReload=/usr/bin/sh /opt/galax/root/registry/registryd.sh restart
KillMode=process

[Install]
WantedBy=multi-user.target

5、id serviceName,查看服务的uid,gid等等信息

VRM01:/home/GalaX8800 # id registry
uid=1003(registry) gid=1001(fusioncompute) groups=1001(fusioncompute),10(wheel)

多命令执行

在Linux中执行命令时,一条条写一条条执行经常会感觉麻烦,短短几条命令也不值得去写一个shell脚本去执行,这时就可以使用linux中的命令连接符号进行执行多条命令

这些连接符号有:; && || ()

1、;

说明:分号(;) 可以进行多条命令的无关联执行,每一条执行结果不会影响其他命令的执行

cmd1 ; cmd2 ; cmd3 [;...]

2、&&

说明: &&左边的command1执行成功(返回0表示成功)后,&&右边的command2才能被执行。

举例:当把文件sql.txt复制一份为sql.bak.txt成功,然后显示副本sql.bak.txt
cp sql.txt sql.bak.txt && cat sql.bak.txt 

3、||

说明:如果 || 左边的command1执行失败(返回1表示失败),才执行||右边的command2,否则不执行command2,具有短路功能。

举例:
1、打印1111.txt的第一列内容,若执行不成功则执行显示facebook.txt的内容
awk '{print $1}' 1111.txt || cat facebook.txt   
awk: cmd. line:1: fatal: cannot open file `1111.txt' for reading (No such file or directory)
google 110 5000
baidu 100 5000
guge 50 3000
sohu 100 4500

2、当打印1111.txt的第一列内容得命令被成功执行,则不执行打印facebook.txt的命令
awk '{print $1}' facebook.txt || cat facebook.txt
google
baidu
guge
sohu

4、()

说明:如果想执行几个命令,则需要用命令分隔符分号隔开每个命令,并使用圆括号()把所有命令组合起来。结合||和&&可以实现复杂的功能。

格式:
(command1;command2;command3;...)

举例:
1、使用多个命令,如果sort命令执行成功,先将排序后的文件备份到/root/backup/目录下,然后再打印
sort facebook.txt > facebook.txt.sorted && (cp facebook.txt.sorted /root/backup/facebook.txt.sorted;lp facebook.txt.sorted)

【grep】文本搜索

1、说明:Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

2、常用的场景:

# 忽略大小写:
cat xxx.txt | grep -i abc

【date】日期管理

说明:显示或者设置系统的日期和时间。

常用操作:

# 查看系统时间和日期
[gandalf@VRM02 ~]$ date
Sat May  6 03:12:11 PM CST 2023

# 获取系统时区
[gandalf@VRM02 ~]$ date +%z
+0800

# 查询时间戳(从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数)
[gandalf@VRM02 ~]$ date +%s
1683357234

# 把时间戳转换成日期
[gandalf@VRM02 ~]$ date -d @1683357234
Sat May  6 03:13:54 PM CST 2023

# 设置时间
VRM02:/home/GalaX8800 # date -s 03:12:55
Sat May  6 03:12:55 AM CST 2023

# 设置日期
VRM02:/home/GalaX8800 # date -s "03:12:55 20230506"
Sat May  6 03:12:55 AM CST 2023

【arping】获取ip对应的mac地址

说明:主要用来获取ip对应的mac地址,更新本地arp缓存表。平时主要用来探测ip地址是否冲突即同一个网络里,同一个ip不同mac地址的情况。ip地址冲突将导致网络故障。

1)获取某个ip的mac地址:

VRM01:/home/GalaX8800 # arping 51.32.18.120
ARPING 51.32.18.120 from 51.32.17.131 eth0
Unicast reply from 51.32.18.120 [28:6E:D4:88:D8:B7]  1.138ms
Unicast reply from 51.32.18.120 [28:6E:D4:88:D8:B7]  1.045ms
Unicast reply from 51.32.18.120 [28:6E:D4:88:D8:B7]  0.769ms
Unicast reply from 51.32.18.120 [28:6E:D4:88:D8:B7]  0.726ms
^CSent 4 probes (1 broadcast(s))
Received 4 response(s)

2)arping冲突地址检测:

# 当同一个ip返回是同一个mac地址时,没有冲突;如果同时返回多个mac地址时,表示地址冲突。

3)arp -a:查看本地mac缓存表:

VRM01:/home/GalaX8800 # arp -a
? (51.32.29.228) at 28:6e:d4:88:e8:d2 [ether] on eth0
? (51.32.29.215) at 28:6e:d4:88:ca:4f [ether] on eth0
? (51.32.29.218) at 28:6e:d4:88:da:6d [ether] on eth0
? (51.32.29.205) at 28:6e:d4:88:c8:33 [ether] on eth0
? (51.32.18.130) at 28:6e:d4:88:dc:0e [ether] on eth0
? (51.32.29.190) at 28:6e:d4:88:cb:9a [ether] on eth0
? (51.32.29.224) at 28:6e:d4:88:d7:9f [ether] on eth0
? (51.32.29.211) at 28:6e:d4:88:c7:c7 [ether] on eth0
? (51.32.29.198) at 28:6e:d4:88:da:6d [ether] on eth0
? (51.32.8.23) at ac:75:1d:ac:74:8f [ether] on eth0
posted on 2023-07-13 14:52  彦承  阅读(54)  评论(0编辑  收藏  举报