SRE运维工程师笔记-Linux基础入门 - Go语言中文社区

SRE运维工程师笔记-Linux基础入门


SRE运维工程师笔记-Linux基础入门

1. Linux基础

1.1 用户类型

  • root用户
    一个特殊的管理账户
    也被称为超级用户
    root已接近完整的系统控制
    对系统损害几乎有无限的能力
    除非必要,不要登录为root
  • 普通(非特权)用户
    权限有限
    造成损害的能力比较有限

1.2 终端terminal

在这里插入图片描述
设备终端:键盘、鼠标、显示器

1.2.1 终端类型

  • 控制台终端: /dev/console
  • 串行终端:/dev/ttyS#
  • 虚拟终端:tty:teletypewriters, /dev/tty#,tty 可有n个,Ctrl+Alt+F#
  • 图形终端:startx, xwindows
    CentOS 6: Ctrl + Alt + F7
    CentOS 7: 在哪个终端启动,即位于哪个虚拟终端
  • 伪终端:pty:pseudo-tty , /dev/pts/# 如:SSH远程连接

1.2.2 查看当前的终端设备

tty命令可以查看当前所在终端
范例:

[root@Centos-8 ~]# tty
/dev/pts/1

dws@ubuntu1804:~$ tty
/dev/pts/0

1.3 交互式接口

交互式接口:启动终端后,在终端设备附加一个交互式应用程序

1.3.1 交互式接口类型

  • GUI:Graphic User Interface
    X protocol, window manager, desktop
    Desktop:
    GNOME (C, 图形库gtk),
    KDE (C++,图形库qt)
    XFCE (轻量级桌面)
  • CLI:Command Line Interface
    shell程序

1.3.2 什么是shell

在这里插入图片描述
Shell 是Linux系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并
把它送入内核去执行
shell也被称为LINUX的命令解释器(command interpreter),Shell 本身是一个程序。将用户输入的
命令行拆解为”命令名“与”参数“。接着,根据命令名找到对应要执行的程序,对被执行的程序进行初始
化,然后将刚才解析出来的参数传给该程序并执行
shell是一种高级程序设计语言,提供了变量,函数,条件判断,循环等开发语言的功能
由于Shell本身是个程序,所以它可以被任何用户自己开发的各种Shell所代替

1.3.3 各种shell

在这里插入图片描述

  • sh:Steve Bourne
  • bash:Bourne-Again Shell,GPL,CentOS 和 Ubuntu 默认使用
  • csh:c shell , C 语言风格
  • tcsh
  • ksh :Korn Shell, AIX 默认 shell
  • zsh: MacOS默认shell

1.3.4 bash shell

GNU Bourne-Again Shell(bash)是GNU计划中重要的工具软件之一,目前也是 Linux标准的shell,与sh兼容
显示当前使用的shell

[root@Centos-8 ~]# echo ${SHELL}
/bin/bash

dws@ubuntu1804:~$ echo ${SHELL}
/bin/bash

1.4 设置主机名

# 临时生效
hostname  NAME

#持久生效,支持Centos7和Ubuntu18.04以上版本
hostnamectl set-hostname NAME

范例:

[root@Centos-8 ~]# hostname bj-yz-k8s-node1-100-10.magedu.local

注意:

  • 主机名不支持使用下划线,但支持横线,可使用字母,横线或数字组合
  • 有些软件对主机名有特殊要求

范例:

[root@Centos-8 ~]# hostnamectl set-hostname centos8.1
[root@Centos-8 ~]# systemctl restart postfix
Job for postfix.service failed because the control process exited with error code.
See "systemctl status postfix.service" and "journalctl -xe" for details.

在这里插入图片描述

1.5 命令提示符 prompt

登录Linux后,默认的系统命令提示符毫无没有个性,无法明显辨别生产和测试环境,而导致误操作。可以通过修改PS1变量实现个性的提示符格式,避免这种低级错误
范例:#管理员 $普通用户

#Centos默认提示符
[root@Centos-8 ~]# 
[user@centos8 ~]$ 

#Ubuntu默认提示符
root@ubuntu1804:~#
dws@ubuntu1804:~$

显示提示符格式

[root@Centos-8 ~]# echo $PS1
[\u@\h \W]\$

root@ubuntu1804:~# echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

修改提示符格式范例

[root@Centos-8 ~]#PS1="\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]"
[root@Centos-8 ~]#PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"

在这里插入图片描述
在这里插入图片描述
提示符格式说明:

  • \e 控制符\033
  • \u 当前用户
  • \h 主机名简称
  • \H 主机名
  • \w 当前工作目录
  • \W 当前工作目录基名
  • \t 24小时时间格式
  • \T 12小时时间格式
  • ! 命令历史数
  • #开机后命令历史数

范例:在CentOS系统实现持久保存提示符格式

[root@centos8 ~]# echo 'PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\]\W\[\e[1;32m\]]\[\e[0m\]\\$"' > /etc/profile.d/env.sh
[root@centos8 ~]# cat /etc/profile.d/env.sh 
PS1="\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"

在这里插入图片描述范例:实现Ubuntu系统持久保存提示符格式

root@ubuntu1804:~# echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" >> .bashrc
root@ubuntu1804:~# tail -1 .bashrc 
PS1='\[\e[1;35m\][\u@\h \W]\$\[\e[0m\]'

在这里插入图片描述

1.6 执行命令

1.6.1 执行命令过程

输入命令后回车,提请shell程序找到键入命令所对应的可执行程序或代码,并由其分析后提交给内核分配资源将其运行起来

1.6.2 shell中可执行的两类命令

  • 内部命令:由shell自带的,而且通过某命令形式提供, ,用户登录后自动加载并常驻内存中
  • 外部命令:在文件系统路径下有对应的可执行程序文件,当执行命令时才从磁盘加载至内存中,执行完毕后从内存中删除

区别指定的命令是内部或外部命令

type COMMAND

范例: 查看是否存在对应内部和外部命令

[root@centos8 ~]#type -a echo
echo is a shell builtin
echo is /usr/bin/echo

在这里插入图片描述

1.6.2.1 内部命令相关

help 内部命令列表
enable 管理内部命令

  • enable cmd 启用内部命令
  • enable –n cmd 禁用内部命令
  • enable –n 查看所有禁用的内部命令

1.6.2.2 执行外部命令

查看外部命令路径:

which -a
which --skip-alias
whereis

Hash缓存表

系统初始hash表为空,当外部命令执行时,默认会从PATH路径下寻找该命令,找到后会将这条命令的路径记录到hash表中,当再次使用该命令时,shell解释器首先会查看hash表,存在将执行之,如果不存在,将会去PATH路径下寻找,利用hash缓存表可大大提高命令的调用速率

hash 命令常见用法

  • hash 显示hash缓存
  • hash -l 显示hash缓存,可作为输入使用
  • hash -p path name 将命令全路径path起别名为name
  • hash -t name 打印缓存中name的路径
  • hash -d name 清除name缓存
  • hash -r 清除缓存

1.6.3 命令别名

对于经常执行的较长的命令,可以将其定义成较短的别名,以方便执行显示当前shell进程所有可用的命令别名

alias

定义别名NAME,其相当于执行命令VALUE

alias NAME=‘VALUE’

范例:扫描新加的磁盘

[root@centos8 ~]#alias scandisk='echo - - - >/sys/class/scsi_host/host0/scan;echo - - - >/sys/class/scsi_host/host1/scan;echo - - - > /sys/class/scsi_host/host2/scan'

在这里插入图片描述范例: 持久保存别名

[root@centos8 ~]#echo "alias free='free -h'" >> .bashrc
[root@centos8 ~]#cat .bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
alias free='free -h'

在这里插入图片描述
设置完以后要登出再登陆才能生效
在这里插入图片描述撤消别名:unalias

unalias [-a] name [name …]
unalias -a #取消所有别名

注意:在命令行中定义的别名,仅对当前shell进程有效;如果想永久有效,要定义在配置文件中

  • 仅对当前用户:~/.bashrc
  • 对所有用户有效:/etc/bashrc

编辑配置给出的新配置不会立即生效,bash进程重新读取配置文件

source /path/to/config_file
. /path/to/config_file

如果别名同原命令同名,如果要执行原命令,可使用

\ALIASNAME
“ALIASNAME”
‘ALIASNAME’
command ALIASNAME
/path/commmand #只适用于外部命令

1.6.4 命令格式

COMMAND [OPTIONS…] [ARGUMENTS…]
COMMAND [COMMAND] [COMMAND] …

选项:用于启用或关闭命令的某个或某些功能

  • 短选项:UNIX 风格选项,-c 例如:-l, -h
  • 长选项:GNU风格选项,–word 例如:–all, --human
  • BSD风格选项: 一个字母,例如:a,使用相对较少
    参数:命令的作用对象,比如:文件名,用户名等
    范例:
[root@centos8 ~]#id -u user
1000

[root@centos8 ~]#ls -a
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .dbus    Documents  .esd_auth      initial-setup-ks.cfg  Music     .pki    .tcshrc    Videos
..  .bash_history    .bash_profile  .cache   .cshrc   Desktop  Downloads  .ICEauthority  .local                Pictures  Public  Templates  .Xauthority
[root@centos8 ~]#ls -all
total 48
dr-xr-x---. 15 root root 4096 Oct 31 21:25 .
dr-xr-xr-x. 18 root root  236 Oct 25 22:45 ..
-rw-------.  1 root root 1442 Oct 25 22:55 anaconda-ks.cfg
-rw-------.  1 root root 1398 Nov  1 23:05 .bash_history
-rw-r--r--.  1 root root   18 May 11  2019 .bash_logout
-rw-r--r--.  1 root root  176 May 11  2019 .bash_profile
-rw-r--r--.  1 root root  197 Nov  1 23:03 .bashrc
drwx------. 10 root root  236 Oct 31 21:19 .cache
drwx------. 11 root root  215 Oct 31 21:19 .config
-rw-r--r--.  1 root root  100 May 11  2019 .cshrc
drwx------.  3 root root   25 Oct 25 23:02 .dbus
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Desktop
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Documents
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Downloads
-rw-------.  1 root root   16 Oct 31 21:18 .esd_auth
-rw-------.  1 root root  310 Oct 31 21:18 .ICEauthority
-rw-r--r--.  1 root root 1806 Oct 25 23:33 initial-setup-ks.cfg
drwx------.  3 root root   19 Oct 31 21:18 .local
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Music
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Pictures
drwxr-----.  3 root root   19 Oct 31 21:18 .pki
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Public
-rw-r--r--.  1 root root  129 May 11  2019 .tcshrc
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Templates
drwxr-xr-x.  2 root root    6 Oct 31 21:18 Videos
-rw-------.  1 root root   56 Oct 31 21:25 .Xauthority

[root@centos8 ~]#free -h
              total        used        free      shared  buff/cache   available
Mem:          1.7Gi       434Mi       430Mi       9.0Mi       925Mi       1.2Gi
Swap:         2.0Gi       1.0Mi       2.0Gi
[root@centos8 ~]#free -human
free: invalid option -- 'u'

Usage:
 free [options]

Options:
 -b, --bytes         show output in bytes
     --kilo          show output in kilobytes
     --mega          show output in megabytes
     --giga          show output in gigabytes
     --tera          show output in terabytes
     --peta          show output in petabytes
 -k, --kibi          show output in kibibytes
 -m, --mebi          show output in mebibytes
 -g, --gibi          show output in gibibytes
     --tebi          show output in tebibytes
     --pebi          show output in pebibytes
 -h, --human         show human-readable output
     --si            use powers of 1000 not 1024
 -l, --lohi          show detailed low and high memory statistics
 -t, --total         show total for RAM + swap
 -s N, --seconds N   repeat printing every N seconds
 -c N, --count N     repeat printing N times, then exit
 -w, --wide          wide output

     --help     display this help and exit
 -V, --version  output version information and exit

For more details see free(1).


[root@centos8 ~]#ps a
    PID TTY      STAT   TIME COMMAND
   3352 tty1     Ss+    0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
  47468 pts/0    Ss     0:00 -bash
  47604 pts/0    R+     0:00 ps a

注意:

  • 多个选项以及多参数和命令之间使用空白字符分隔
  • 取消和结束命令执行:Ctrl+c,Ctrl+d
  • 多个命令可以用 “;” 符号分开
  • 一个命令可以用\分成多行

1.7 常见命令

1.7.1 查看硬件信息

1.7.1.1 查看CPU

lscpu命令可以查看cpu信息
cat /proc/cpuinfo也可看查看到
范例 :

[root@CentOS-8 ~]#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
CPU family:          6
Model:               142
Model name:          Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Stepping:            10
CPU MHz:             1800.001
BogoMIPS:            3600.00
Hypervisor vendor:   VMware
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            256K
L3 cache:            6144K
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 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 mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves arat md_clear flush_l1d arch_capabilities

[root@CentOS-8 ~]#cat /proc/cpuinfo

1.7.1.2 查看内存大小

[root@CentOS-8 ~]#free
              total        used        free      shared  buff/cache   available
Mem:        1996236      781604      720956       10824      493676     1038368
Swap:       2097148           0     2097148
[root@CentOS-8 ~]#cat /proc/meminfo
MemTotal:        1996236 kB
MemFree:          721196 kB
MemAvailable:    1038608 kB
Buffers:            3276 kB
Cached:           422136 kB
SwapCached:            0 kB
Active:           424028 kB
Inactive:         348408 kB
Active(anon):     349048 kB
Inactive(anon):     8800 kB
Active(file):      74980 kB
Inactive(file):   339608 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Dirty:                 4 kB
Writeback:             0 kB
AnonPages:        328796 kB
Mapped:           191304 kB
Shmem:             10824 kB
KReclaimable:      68264 kB
Slab:             213252 kB
SReclaimable:      68264 kB
SUnreclaim:       144988 kB
KernelStack:        8848 kB
PageTables:        23732 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     3095264 kB
Committed_AS:    1748092 kB
VmallocTotal:   34359738367 kB
VmallocUsed:           0 kB
VmallocChunk:          0 kB
Percpu:            96256 kB
HardwareCorrupted:     0 kB
AnonHugePages:    129024 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      212864 kB
DirectMap2M:     1884160 kB
DirectMap1G:           0 kB

1.7.1.3 查看硬盘和分区情况

[root@CentOS-8 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0   10G  0 part /
├─sda3   8:3    0    2G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part
└─sda5   8:5    0    7G  0 part /home
sr0     11:0    1  8.6G  0 rom
[root@CentOS-8 ~]#cat /proc/partitions
major minor  #blocks  name

   8        0   20971520 sda
   8        1    1048576 sda1
   8        2   10485760 sda2
   8        3    2097152 sda3
   8        4          1 sda4
   8        5    7337984 sda5
  11        0    9047040 sr0

1.7.2 查看系统版本信息

1.7.2.1 查看系统架构

root@ubuntu2004:~# arch
x86_64

[root@centos8 ~]# arch
x86_64

[root@rhel5 ~]# arch
i686

1.7.2.2 查看内核版本

[root@centos8 ~]#uname -r
4.18.0-147.el8.x86_64

[root@centos7 ~]#uname -r
3.10.0-1062.el7.x86_64

[root@centos6 ~]# uname -r
2.6.32-754.el6.x86_64

[root@ubuntu1804 ~]#uname -r
4.15.0-29-generic

1.7.2.3 查看操作系统发行版本

#CentOS8 查看发行版本
[root@CentOS-8 ~]#cat /etc/redhat-release
CentOS Linux release 8.3.2011

[15:29:52 root@CentOS-8 ~]#cat /etc/os-release
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"

[15:32:30 root@CentOS-8 ~]#lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 8.3.2011
Release:        8.3.2011
Codename:       n/a

#Ubuntu查看发行版本
du@ubuntu1804:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.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=bionic
UBUNTU_CODENAME=bionic

du@ubuntu1804:~$ cat /etc/issue
Ubuntu 18.04.5 LTS \n \l

du@ubuntu1804:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:        18.04
Codename:       bionic

范例:查看OS版本

[root@centos8 ~]#lsb_release -is
CentOS
[root@centos8 ~]#lsb_release -cs
Core
[root@centos8 ~]#lsb_release -rs
8.2.2004

[root@centos7 ~]#lsb_release -is
CentOS
[root@centos7 ~]#lsb_release -cs
Core
[root@centos7 ~]#lsb_release -rs
7.9.2009

[root@centos6 ~]#lsb_release -is
CentOS
[root@centos6 ~]#lsb_release -cs
Final
[root@centos6 ~]#lsb_release -rs
6.10

root@ubuntu2004:~# lsb_release -is
Ubuntu
root@ubuntu2004:~# lsb_release -cs
focal
root@ubuntu2004:~# lsb_release -rs
20.04

[root@ubuntu1804 ~]#lsb_release -is
Ubuntu
[root@ubuntu1804 ~]#lsb_release -cs
bionic
[root@ubuntu1804 ~]#lsb_release -rs
18.04

1.7.3 时间和日期

Linux的两种时钟

  • 系统时钟:由Linux内核通过CPU的工作频率进行的
  • 硬件时钟:主板

相关命令

  • date 显示和设置系统时间

范例:

[root@CentOS-8 ~]#date +%s
1619336385
[root@CentOS-8 ~]#date -d @`date +%s`
Sun Apr 25 15:40:34 CST 2021

[root@CentOS-8 ~]#date -d @1619336385
Sun Apr 25 15:39:45 CST 2021

[root@CentOS-8 ~]#date -d @1619336385 +%F_%T
2021-04-25_15:39:45
  • clock,hwclock: 显示硬件时钟

-s, --hctosys 以硬件时钟为准,校正系统时钟
​ -w, --systohc 以系统时钟为准,校正硬件时钟

范例:

[root@centos8 ~]#ll /usr/sbin/clock
lrwxrwxrwx. 1 root root 7 Apr 25  2021 /usr/sbin/clock -> hwclock

时区:

/etc/localtime

范例:

[root@centos8 ~]#timedatectl set-timezone Asia/Shanghai

[root@centos8 ~]#timedatectl status
               Local time: Mon 2020-07-27 17:20:56 CST
           Universal time: Mon 2020-07-27 09:20:56 UTC
                 RTC time: Mon 2020-07-27 09:20:56
               Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
             NTP service: active
         RTC in local TZ: no
         
[root@centos8 ~]#ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Dec 11 11:19 /etc/localtime ->
../usr/share/zoneinfo/Asia/Shanghai

root@ubuntu1804:~# cat /etc/timezone
Asia/Shanghai

显示日历:

cal –y

范例:

[15:41:25 root@CentOS-8 ~]#cal 9 1752
   September 1752
Su Mo Tu We Th Fr Sa
       1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

1.7.4 关机和重启

关机:

  • halt
  • poweroff

重启
reboot
-f:强制,不调用shutdown
-p:切断电源

关机或重启:shutdown

shutdown [OPTION]... [TIME] [MESSAGE]

-r: reboot
-h: halt
-c:cancel
TIME:无指定,默认相当于+1(CentOS7)

  • now: 立刻,相当于+0
  • +#: 相对时间表示法,几分钟之后;例如 +3
  • hh:mm: 绝对时间表示,指明具体时间

1.7.5 用户登录信息查看命令

  • whoami: 显示当前登录有效用户
  • who: 系统当前所有的登录会话
  • w: 系统当前所有的登录会话及所做的操作

1.7.6 文本编辑

  • nano 工具可以实现文本的编辑,上手容易,适合初学者
  • gedit 工具是图形工具

范例: 创建登录提示文件 /etc/motd
参考网站链接: link.

[root@CentOS-8 ~]#cat /etc/motd

在这里插入图片描述

1.7.7 会话管理

命令行的典型使用方式是,打开一个终端窗口(terminal window,以下简称"窗口"),在里面输入命令。用户与计算机的这种临时的交互,称为一次"会话"(session)

会话的一个重要特点是,窗口与其中启动的进程是连在一起的。打开窗口,会话开始;关闭窗口,会话结束,会话内部的进程也会随之终止,不管有没有运行完

一个典型的例子就是,SSH 登录远程计算机,打开一个远程窗口执行命令。这时,网络突然断线,再次登录的时候,是找不回上一次执行的命令的。因为上一次 SSH 会话已经终止了,里面的进程也随之消失了。为了解决这个问题,会话与窗口可以"解绑":窗口关闭时,会话并不终止,而是继续运行,等到以后需要的时候,再让会话"绑定"其他窗口

终端复用器软件就是会话与窗口的"解绑"工具,将它们彻底分离。
(1)它允许在单个窗口中,同时访问多个会话。这对于同时运行多个命令行程序很有用。
(2)它可以让新窗口"接入"已经存在的会话。
(3)它允许每个会话有多个连接窗口,因此可以多人实时共享会话。
(4)它还支持窗口任意的垂直和水平拆分。

类似的终端复用器还有Screen,Tmux

1.7.7.1 screen

利用screen 可以实现会话管理,如:新建会话,共享会话等
注意:CentOS7 来自于base源,CentOS8 来自于epel源
范例:安装 screen

#CentOS7 安装screen
[root@centos7 ~]#yum -y install screen

#CentOS8 安装screen
[root@centos8 ~]#dnf -y install epel-release
[root@centos8 ~]#dnf -y install screen

screen命令常见用法:

  • 创建新screen会话
    screen –S [SESSION]
  • 加入screen会话
    screen –x [SESSION]
  • 退出并关闭screen会话
    exit
  • 剥离当前screen会话
    Ctrl+a,d
  • 显示所有已经打开的screen会话
    screen -ls
  • 恢复某screen会话
    screen -r [SESSION]

1.7.7.2 tmux

在这里插入图片描述
Tmux 是一个终端复用器(terminal multiplexer),类似 screen,但是更易用,也更强大
Tmux 就是会话与窗口的"解绑"工具,将它们彻底分离,功能如下

  • 它允许在单个窗口中,同时访问多个会话。这对于同时运行多个命令行程序很有用。
  • 它可以让新窗口"接入"已经存在的会话。
  • 它允许每个会话有多个连接窗口,因此可以多人实时共享会话。
  • 它还支持窗口任意的垂直和水平拆分

安装

yum install tmux

启动与退出

[root@centos8 ~]#tmux
[root@centos8 ~]#exit
logout

mux 窗口有大量的快捷键。所有快捷键都要通过前缀键唤起。默认的前缀键是 Ctrl+b,即先按下Ctrl+b,快捷键才会生效。帮助命令的快捷键是 Ctrl+b ? 然后,按下 q 键,就可以退出帮助

新建会话
第一个启动的 Tmux 窗口,编号是0,第二个窗口的编号是1,以此类推。这些窗口对应的会话,就是 0号会话、1 号会话。使用编号区分会话,不太直观,更好的方法是为会话起名。下面命令新建一个指定名称的会话。

tmux new -s <session-name>

tmux ls或Ctrl+b,s 可以查看当前所有的 Tmux 会话

tmux ls
tmux list-session

分离会话
在 Tmux 窗口中,按下Ctrl+b d或者输入tmux detach命令,就会将当前会话与窗口分离。

tmux detach

接入会话
tmux attach 命令用于重新接入某个已存在的会话。

tmux attach -t <session-name>

范例:

tmux attach -t 0

杀死会话
tmux kill-session命令用于杀死某个会话。

tmux kill-session -t <session-name>

切换会话
tmux switch命令用于切换会话

tmux switch -t <session-name>

可以将窗口分成多个窗格(pane),每个窗格运行不同的命令
上下分窗格

tmux split-window
ctrl+b,"

左右分窗格

tmux split-window -h
ctrl+b,%

窗格快捷键

Ctrl+b %:划分左右两个窗格
Ctrl+b ":划分上下两个窗格
Ctrl+b <arrow key>:光标切换到其他窗格。<arrow key>是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓
Ctrl+b ;:光标切换到上一个窗格
Ctrl+b o:光标切换到下一个窗格。
Ctrl+b {:当前窗格左移
Ctrl+b }:当前窗格右移
Ctrl+b Ctrl+o:当前窗格上移
Ctrl+b Alt+o:当前窗格下移
Ctrl+b x:关闭当前窗格
Ctrl+b !:将当前窗格拆分为一个独立窗口
Ctrl+b z:当前窗格全屏显示,再使用一次会变回原来大小
Ctrl+b Ctrl+<arrow key>:按箭头方向调整窗格大小
Ctrl+b q:显示窗格编号

窗口管理
除了将一个窗口划分成多个窗格,Tmux 也允许新建多个窗口
新建窗口
tmux new-window命令用来创建新窗口

tmux new-window

新建一个指定名称的窗口

tmux new-window -n <window-name>

切换窗口
tmux select-window命令用来切换窗口
切换到指定编号的窗口

tmux select-window -t <window-number>

切换到指定名

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/dws123654/article/details/121071218
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-02 13:33:57
  • 阅读 ( 231 )
  • 分类:运维

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢