01.Linux系统核心技术-Linux运维工程师必须掌握的核心命令

2021/10/7 7:12:47

本文主要是介绍01.Linux系统核心技术-Linux运维工程师必须掌握的核心命令,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

01.Linux系统核心技术-Linux运维工程师必须掌握的核心命令

Linux系统核心技术

查看计算机硬件系统信息

服务器核心硬件就是CPU、内存、磁盘和网卡,它们配置的好坏会影响程序的运行效率,我们可以使用命令的方式查看服务器硬件的配置

查看CPU信息

查看CPU信息可以使用lscpu命令

从命令的输出结果可以看得出来CPU的型号是 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz,CPU(s)是4表示同时可以执行四个任务。

[root@centos8-1 ~]# lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              4
On-line CPU(s) list: 0-3
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           2
NUMA node(s):        1
Vendor ID:           GenuineIntel
BIOS Vendor ID:      GenuineIntel
CPU family:          6
Model:               158
Model name:          Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BIOS Model name:     Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Stepping:            10
CPU MHz:             2904.005
BogoMIPS:            5808.01
Hypervisor vendor:   VMware
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            12288K
NUMA node0 CPU(s):   0-3
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 arch_perfmon nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves arat md_clear flush_l1d arch_capabilities

如果想要同时执行更多的任务,可以在VMware WorkStation中修改,修改完成以后会立即生效。
修改CPU数量

[root@centos8-1 ~]# lscpu # 再次使用lscpu查看cpu信息发现CPU(s)已经变成了8
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              8
On-line CPU(s) list: 0-7
Thread(s) per core:  1
Core(s) per socket:  2
Socket(s):           4
NUMA node(s):        1
Vendor ID:           GenuineIntel
BIOS Vendor ID:      GenuineIntel
CPU family:          6
Model:               158
Model name:          Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BIOS Model name:     Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Stepping:            10
CPU MHz:             2904.005
BogoMIPS:            5808.01
Hypervisor vendor:   VMware
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            12288K
NUMA node0 CPU(s):   0-7
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 arch_perfmon nopl xtopology tsc_reliable nonstop_tsc cpuid pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 xsaves arat md_clear flush_l1d arch_capabilities

除了lscpu以外,还可以使用cat /proc/cpuinfo命令查看CPU信息,不过它是以每核分开显示的。

[root@centos8-1 ~]# cat /proc/cpuinfo

内存信息查看

使用free或者是cat /proc/meminfo可以查看当前系统的内存信息,包括总内存、已经使用内存以及可用内存信息

[root@centos8-1 ~]# free # 查看内存信息 默认的单位是K
              total        used        free      shared  buff/cache   available
Mem:        3996164      748088     2645976       11108      602100     3006428
Swap:       8388604           0     8388604
[root@centos8-1 ~]# free -h # 以人类可读的方式 查看内存信息 
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       730Mi       2.5Gi        10Mi       588Mi       2.9Gi
Swap:         8.0Gi          0B       8.0Gi

[root@centos8-1 ~]# cat /proc/meminfo
MemTotal:        3996164 kB
MemFree:         2646116 kB
MemAvailable:    3006592 kB
Buffers:            3420 kB
Cached:           547356 kB
SwapCached:            0 kB
Active:            71968 kB
Inactive:         929456 kB
Active(anon):       2544 kB
Inactive(anon):   459208 kB
Active(file):      69424 kB
Inactive(file):   470248 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       8388604 kB
SwapFree:        8388604 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:        446576 kB
Mapped:           206272 kB
Shmem:             11108 kB
KReclaimable:      51348 kB
Slab:             123884 kB
SReclaimable:      51348 kB
SUnreclaim:        72536 kB
KernelStack:        8880 kB
PageTables:        23916 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    10386684 kB
Committed_AS:    2156796 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Percpu:            95232 kB
HardwareCorrupted:     0 kB
AnonHugePages:    239616 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      210752 kB
DirectMap2M:     2934784 kB
DirectMap1G:     3145728 kB

Linux和Windows不同的是Linux的图形界面只是它的一个程序,而不是必备组件。我们之前安装CentOS8.4选择的Server With GUI包含GNOME桌面的图形环境,我们可以使用init 3命令关闭图形,CentOS8会出现一个字符登录界面。
init 3关闭图形
然后再使用free -h观察已经使用的内存只有334M,相比图形界面的730M 减少了接近400M内存,为了节省内存,生产环境一般不会安装和使用图形界面。

[root@centos8-1 ~]# init 3
[root@centos8-1 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       334Mi       2.9Gi       9.0Mi       586Mi       3.3Gi
Swap:         8.0Gi          0B       8.0Gi

想要再切回图形界面使用init 5命令即可。

[root@centos8-1 ~]# init 5

还可以使用runlevel命令查看当前系统运行的模式,其中输出结果的3表示之前是字符界面,5表示当前系统运行的是图形界面
图形界面

[root@centos8-1 ~]# runlevel
3 5

查看硬盘和分区情况

使用lsblk命令可以查看磁盘信息,包括有几块磁盘以及每块磁盘的分区、容量信息

通过命令的执行结果可以得知目前CentOS8系统上有三块磁盘,分别是sda,sdb,sdc,其中sdb和sdc没有分区,
而sda磁盘是在安装系统时创建了四个分区,具体信息如下表格所示

分区名 挂载点 容量 文件系统
根分区 / 100G xfs
boot分区 /boot 2G ext4
swap分区 swap 8G swap
data分区 /data 50G xfs
[root@centos8-1 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    2G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    8G  0 part [SWAP]
sdb      8:16   0   50G  0 disk 
sdc      8:32   0   50G  0 disk 
sr0     11:0    1  9.3G  0 rom  

除此以外还可以使用 cat /proc/partitions命令查看磁盘信息,从命令的执行结果可以看出容量还是以KB为单位。

[root@centos8-1 ~]# cat /proc/partitions 
major minor  #blocks  name

   8       32   52428800 sdc
   8       16   52428800 sdb
   8        0  209715200 sda
   8        1    2097152 sda1
   8        2  104857600 sda2
   8        3   52428800 sda3
   8        4          1 sda4
   8        5    8388608 sda5
  11        0    9696256 sr0

查看系统信息

查看系统架构

通过arch命令可以查看当前系统架构为x86_64,这也是目前主流的系统架构。

[root@centos8-1 ~]# arch
x86_64

查看Linux内核版本

不同的Linux发行版使用的Linux内核版本不同,通过uname -r可以查看Linux系统的内核版本

[root@centos8-1 ~]# uname -r #查看CentOS8.4的内核版本
4.18.0-305.3.1.el8.x86_64
guanglei@ubuntu1804-1:~$ uname -r #查看ubuntu1804的内核版本
4.15.0-156-generic
guanglei@ubuntu-server-2004-1:~$ uname -r #查看ubuntu server 20.04的内核版本
5.4.0-88-generic

查看操作系统的版本

通过 cat /etc/os-release 命令或者是lsb_release -a命令可以查看操作系统的版本,后期在开发程序时不同的操作系统版本需要做不同的处理。

查看Ubuntu Server的版本

guanglei@ubuntu-server-2004-1:~$ cat /etc/os-release  #查看Ubuntu Server的版本
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
guanglei@ubuntu1804-1:~$ lsb_release -a # 查看Ubuntu Server版本
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:        18.04
Codename:       bionic

查看CentOS8的版本

[root@centos8-1 ~]# cat /etc/os-release #查看CentOS8的版本
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"

CentOS还可以使用 cat /etc/redhat-release版本

[root@centos8-1 ~]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105

日期和时间

系统时间和硬件时间

Linux中的时间有系统时间和硬件时间两种

  • 系统时间是由Linux内核通过CPU的工作频率计算出来的,可以使用date命令查看
  • 硬件时间是主板内置时钟的时间,和操作系统无关,可以使用clock命令看
[root@centos8-1 ~]# date #查看系统时间
Tue Oct  5 13:10:36 CST 2021
[root@centos8-1 ~]# clock #查看硬件时间
2021-10-05 13:11:00.787437+08:00

date和clock可以使用分号隔开后同时执行看系统时间和硬件时间

[root@centos8-1 ~]# date;clock #同时查看系统时间和硬件时间
Tue Oct  5 13:11:37 CST 2021
2021-10-05 13:11:55.490189+08:00

目前我的系统时间和硬件时间相差了18秒

如果硬件时间是准确的,那么就通过命令clock -s以硬件时间为准,同步校正系统时间。

[root@centos8-1 ~]# clock -s # 以硬件时钟为准,校准系统时间
[root@centos8-1 ~]# date;clock
Tue Oct  5 13:14:54 CST 2021
2021-10-05 13:14:54.197324+08:00

如果系统时间是准确的,那么通过clock -w命令以系统时间为准,同步校正硬件时间

[root@centos8-1 ~]# clock -w #以系统为准校准硬件时间
[root@centos8-1 ~]# date;clock
Tue Oct  5 13:17:28 CST 2021
2021-10-05 13:17:28.478297+08:00

时区

不同的国家在同一时刻所处的时间是不同的,因为时区不同。
time

例如中国就是东八区,东八区和格林威治的时区相比相差8个小时(也就是比北京时间慢了8个小时),之前在安装操作系统的时候设置的时区就是Asia/Shanghai,,通过timedatectl status查看当前时间状态,其中 Universal time就是格林威治时间,而Local Time就是系统设置时区(Asia/Shanghai)的时间,它们两者刚好相差了8个小时。 Time zone: Asia/Shanghai (CST, +0800) 表示当前的时区是亚洲\上海。

[root@centos8-1 ~]# timedatectl status #查看当前时区状态
               Local time: Tue 2021-10-05 13:20:53 CST
           Universal time: Tue 2021-10-05 05:20:53 UTC
                 RTC time: Tue 2021-10-05 05:20:53
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

如果现在你去了法国巴黎,可以通过timectl set-timezone 设置时区为欧洲\巴黎,当修改时区后使用date命令查看系统时间显示的就是当前法国的巴黎时间,相比北京时间慢了6个小时。

[root@centos8-1 ~]# timedatectl set-timezone Europe/Paris #设置时区为欧洲/巴黎
[root@centos8-1 ~]# date
Tue Oct  5 07:24:38 CEST 2021

修改时区最终影响的是 /etc/localtime文件

[root@centos8-1 ~]# ll /etc/localtime 
lrwxrwxrwx. 1 root root 34 Oct  5 07:24 /etc/localtime -> ../usr/share/zoneinfo/Europe/Paris

不过目前我在中国,因此还是改回亚洲上海

[root@centos8-1 ~]# timedatectl set-timezone Asia/Shanghai #将时区设置为亚洲上海
[root@centos8-1 ~]# date
Tue Oct  5 13:35:15 CST 2021

timedatectl list-timezones可以查看所有时区

[root@centos8-1 ~]# timedatectl list-timezones

日历

cal命令可以查看当前月份的日历,例如今天是2021年10月5号

[root@centos8-1 ~]# cal
    October 2021    
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 

查看指定月份的日历

[root@centos8-1 ~]# cal 12 2021 #查看2021年12月份的日历
    December 2021   
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 

还可以查看指定年份的日历

[root@centos8-1 ~]# cal 2022  # 查看2022年的日历
                               2022                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31      
30 31                                                             
        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30      
                                                                  
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30   
31                                                                
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

关机和重启

生产环境不能随便关机和重启!!! 因为服务器基本都是7*24对外提供服务
测试环境或者是本地虚拟机安装的Linux服务器可以按需求关机或者重启
关机可以使用halt或者poweroff命令

而shutdown可以实现关机或者重启,shutdown -r表示重启,即reboot,shutdown -h表示关机,即halt。 -c表示取消关机或重启。
除此以外shutdown 还可以指定时间来关机或重启,默认是1分钟后执行。

关机和重启命令的常见用法

[root@centos8-1 ~]# poweroff #关机
[root@centos8-1 ~]# reboot #重启
[root@centos8-1 ~]# shutdown -r  now #重启
[root@centos8-1 ~]# shutdown -h 17:00 #指定的时间关机
[root@centos8-1 ~]# shutdown -c #取消关机
[root@centos8-1 ~]# shutdown -r +5 #5分钟后重启系统

文本编辑器

Linux下有nano和vim等文本编辑器,目前我们先使用nano来编辑文本。
如果系统上没有安装nano文本编辑器,那么在使用之前首先需要使用yum install -y nano命令来安装nano文本编辑器

[root@centos8-1 ~]# yum install -y nano

在使用nano进行文本编辑时,nano后面跟文件路径即可打开文件,然后可以直接修改文件,类似于windows的记事本,修改完成后然后按Ctrl+x退出,接着按y保存修改,回车后退出nano。

然后使用nano /etc/motd来修改该文件,motd文件是用户登录之后提示的内容

[root@centos8-1 ~]# nano /etc/motd

然后输入Welcome to my class
使用nano编辑器修改内容
之后按Ctrl+x退出nano编辑器,按y保存到文件汇中,然后按回车键确认修改。

再重新登录CentOS8.4

  1. 输入exit命令退出登录
    exit退出登录
  2. 选择CentOS8.4的会话重新连接
    选择CentOS8.4的会话重新连接

登录成功之后就会看到/etc/motd文件内容显示在终端上
motd

我这里准备了一个motd文件,首先使用MobaXterm终端上传到/etc目录下覆盖现有的motd文件,在使用MobaXterm终端登录CentOS8.4-1后,首先将SSH Broswer的路径切换到/etc目录下
切换目录
然后点击上传文件的按钮
上传文件

选择提前准备好的motd文件上传到/etc目录下
motd

然后再次重新登录系统就会看到一个大佛
自定义motd

大家可以自行访问https://www.bootschool.net/ascii-art 在线生成ASCII(图)艺术字,然后将内容复制到motd文件即可。

ASCII(图)艺术字
ASCII(图)艺术字

会话管理

会话与进程

当我们登录Linux打开一个终端窗口,然后输入命令,用户与计算机的这种临时交互被称为一次会话(Session)、会话的一个重要的特点就是窗口与其中启动的进程是连接在一起的,打开窗口则会话开始,关闭窗口则会话结束,而且会话内部的进程也会随之而终止,不管有没有运行完成。

这里举一个例子,我们首先使用XShell远程登录CentOS8.4-1,然后使用ping 10.0.0.103命令检测是否能够连接10.0.0.103,也就是Ubuntu Server 20.04.3-1那台机器
ping
命令执行的结果显示能够连接,而且只要你不关闭窗口会一直显示收到数据以及耗时。
接收数据

然后我们复制一个会话
复制会话

然后使用ps aux|grep ping 查看ping的进程信息,因为没有关闭会话窗口所以进程还在
查看ping进程

当我们关闭之前开启的会话窗口之后
关闭会话窗口
再次使用ps aux|grep ping查看进程会发现ping进程不存在了
ping进程不存在

为了解决上述的问题,我们就可以将会话与窗口解绑,也就是窗口关闭时会话并不会终止,而是继续运行,等到以后需要的时候,再让会话绑定其他窗口。

而终端复用器程序(例如screen,tmux)就是将会话与窗口的解绑工具,将它们彻底分离。

终端复用器-screen

screen的安装

首次运行screen时会发现Shell提示command not found

[root@centos8-1 ~]# screen
bash: screen: command not found...

我们需要使用yum install -y screen 来安装screen,但是执行之后发现提示错误:: Unable to find a match: screen

[root@centos8-1 ~]# yum install -y screen
Last metadata expiration check: 0:54:04 ago on Wed 06 Oct 2021 09:47:41 AM CST.
No match for argument: screen
Error: Unable to find a match: screen

Centos8安装screen需要使用epel来安装,安装epel后,再安装screen,解决centos8无法安装screen的问题

[root@centos8-1 ~]# yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

然后再执行yum install -y screen 便可安装成功

[root@centos8-1 ~]# yum install -y screen

Screen安装成功
Screen安装成功

screen创建会话

在安装完screen后就可以使用了,首先在终端输入screen,然后你就会看到屏幕好像是刷新了一下,本质就是一个会话
screen

[root@centos8-1 ~]# screen

然后再次运行ping 10.0.0.103
ping

[root@centos8-1 ~]# ping 10.0.0.103

由于开启了screen,即使关闭了所有的会话窗口,你会发现使用ps aux |grep ping 命令查看ping命令的进程也还在。

[root@centos8-1 ~]# ps aux|grep ping
gdm         1719  0.0  0.1 305932  7752 tty1     Sl+  09:32   0:00 /usr/libexec/gsd-housekeeping
root       57816  0.0  0.0  32448  2456 pts/3    S+   10:55   0:00 ping 10.0.0.103
root       57995  0.0  0.0  12136  1120 pts/1    S+   10:56   0:00 grep --color=auto ping

如果后期在做运维工作,有些命令很耗时又怕误操作关闭会话导致命令的进程关闭就可以使用screen。

screen实现桌面共享

screen除了创建会话,能让命令关闭窗口后继续在后台执行的功能以外,还能够实现远程桌面共享,从而实现远程协助的功能。

实现该功能必须满足两个条件,两个用户必须使用同一个账号登录同一台机器。

例如这里小红和小明同时使用root账号登录了CentOS8.4,其中左边的窗口是小红,右边的窗口是小明
小红和小明

然后小红使用screen -S help开启一个远程会话,会话的名称叫help

[root@centos8-1 ~]# screen -S help # 基于screen开启名为help的会话

然后小明使用screen -x help加入小红开启的会话
开启和加入会话

[root@centos8-1 ~]# screen -x help # 加入小红开启的help会话

此时小明可以看到小红窗口的内容
小明看到小红窗口的内容

当小明解决小红的问题后,可以使用ctrl a,d退出当前会话
退出会话

如果小明不知道小红的会话名称,可以使用screen -ls获取当前会话

[root@centos8-1 ~]# screen -ls #查看当前会话
There is a screen on:
        5344.help       (Attached)
1 Socket in /run/screen/S-root.

echo命令的使用

echo就是用于输出信息

echo输出固定字符串

[root@centos8-1 ~]# echo hello #输出hello
hello

[root@centos8-1 ~]# echo "hello world" #输出hello world
hello world

echo输出变量值

[root@centos8-1 ~]# echo $PATH #查看PATH环境变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@centos8-1 ~]# echo $PS1 # 查看PS1变量
[\u@\h \W]\$
[root@centos8-1 ~]# echo $SHELL # 查看SHELL变量 
/bin/bash

echo输出转义字符

  • -e 表示启用转义字符的解释功能,\开头的就是转义字符
[root@centos8-1 ~]# echo  -e '\a' # 发声音 作为通知

echo 默认可以实现换行

  • -n取消换行
[root@centos8-1 ~]# echo #换行

[root@centos8-1 ~]# echo -n #取消换行
[root@centos8-1 ~]# 

echo输出信息还可以带颜色

  • 31m-37m表示字体颜色
  • 41-47表示背景颜色
  • 1表示高亮
    echo输出带颜色的信息
[root@centos8-1 ~]# echo -e '\E[1;31mWelcome to my class\E[0m' #输出代言色的Welcome to my class
Welcome to my class

echo还可以清除屏幕,等价于clear命令或者是快捷键Ctrl+l

[root@centos8-1 ~]# echo -e '\033[2J' # 清除屏幕


这篇关于01.Linux系统核心技术-Linux运维工程师必须掌握的核心命令的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程