终端 1 2 3 4 - 控制台 /dev/console - 串行终端 /dev/ttyS# - 虚拟终端: tty: /dev/tty# - 伪终端: pty: /dev/pts/#
终端、Shell、tty 和控制台(console)有什么区别? - 知乎 Linux 终端(tty) Linux 伪终端(pty)
控制台 /dev/console 计算机启动的时候,所有的信息都会显示到控制台上,而不会显示到终端上
串行终端 /dev/ttyS# 计算机串行端口连接的终端设备, 这些串行端口所对应的设备名称是/dev/tty0、/dev/tty1 等,分别对应于系统下的 COM1、COM2 等
虚拟终端: tty: /dev/tty# 每打开一个 terminal,系统会启动一个 shell 与这个 terminal 关联起来, shell=命令行解释器, tty 和 terminal 是同义词
tty 是虚拟终端, 与之对应的是 console 是物理终端
伪终端: pty: /dev/pts/# 使用 tty1-6 的情况一般为 Linux 系统直接连了键盘和显示器,或者是使用了 vSphere console 等虚拟化方案,其它情况下使用的都是伪终端, 通过 SSH 等方式建立的连接中使用的都是伪终端
系统信息 1 2 3 4 5 6 7 8 lscpu free lsblk 和 df uname cat /etc/redhat-releasecat /etc/os-releasecat /etc/centos-releaselsb_release
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 [lujinkai@centos8 ~]$ lscpu [lujinkai@centos8 ~]$ cat /proc/cpuinfo [lujinkai@centos8 ~]$ free -h [lujinkai@centos8 ~]$ cat /proc/meminfo [root@centos8 ~] NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 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 2G 0 part [SWAP] sr0 11:0 1 1.6G 0 rom [root@centos8 /] NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 ext4 a489cd2b-daf2-4d94-90ab-696a21284f6a /boot ├─sda2 xfs 30e97194-a6ff-42cb-b6e0-3374d3e614e7 / ├─sda3 xfs 721abfdb-0d93-4559-9ac1-a487ecabf339 /data ├─sda4 └─sda5 swap 1b064969-1539-4d0a-ab1f-6f304f99ac9e [SWAP] sr0 iso9660 CentOS-8-2-2004-x86_64-dvd 2020-06-08-22-08-12-00 [lujinkai@centos8 ~]$ cat /proc/partitions major minor 8 0 209715200 sda 8 1 1048576 sda1 8 2 104857600 sda2 8 3 52428800 sda3 8 4 1 sda4 8 5 2097152 sda5 11 0 1678336 sr0 [lujinkai@centos8 ~]$ uname Linux [lujinkai@centos8 ~]$ uname -r 4.18.0-80.el8.x86_64 [lujinkai@centos8 ~]$ uname -a Linux centos8.mageedu.org 4.18.0-80.el8.x86_64 [lujinkai@centos8 ~]$ cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core) [lujinkai@centos8 ~]$ cat /etc/os-release NAME="CentOS Linux" VERSION="8 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="8" [lujinkai@centos8 ~]$ lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 8.0.1905 (Core) Release: 8.0.1905 Codename: Core
用户登录信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [lujinkai@centos8 ~]$ whoami lujinkai [lujinkai@centos8 ~]$ who am i lujinkai pts/1 2020-07-27 22:12 (10.0.0.1) [lujinkai@centos8 ~]$ who root tty1 2020-07-27 22:21 root pts/0 2020-07-27 22:11 (10.0.0.1) lujinkai pts/1 2020-07-27 22:12 (10.0.0.1) [lujinkai@centos8 ~]$ w 22:21:22 up 10 min, 3 users , load average: 0.05, 0.08, 0.09 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root tty1 - 22:21 18.00s 0.01s 0.01s -bash root pts/0 10.0.0.1 22:11 10:12 0.02s 0.02s -bash lujinkai pts/1 10.0.0.1 22:12 2.00s 0.08s 0.02s w
主机名 1 2 3 hostname NAME hostnamectl set-hostname NAME
主机名不支持使用下划线_
, 可以使用字母、横线或数字组合
主机名不要使用数字结尾,因为有些软件对主机名有要求
命令提示符 1 2 3 4 5 6 7 8 9 10 11 [root@centos8 ~]$echo $PS1 \[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]$ \e \u \h \H \w \W \t \T
命令 命令格式 三种风格:短选项(UNIX)、长选项(GNU)、BSD 风格选项
ctrl + c : 终止命令执行;ctrl + d : 退出终端
内部命令和外部命令 内部命令是 shell 内置的,外部命令是自己安装的
1 2 3 4 5 6 7 8 9 10 type -a COMMAND enable COMMAND enable -n COMMAND enable -n which COMMAND whereis COMMAND
命令执行顺序 1 别名 > 内部命令 > hash > $PATH > command not found
echo 打印变量的时候,变量加引号的规则和 php 一样,单引号当成字符串输出,双引号则解析变量
-e:启用 \ 字符的解释功能 (escapes 转义符)
1 2 3 4 5 6 7 8 9 10 11 12 13 \a \b \c \e \n \r \t \ \0nnn \xHH插入HH(十六进制) $echo -e "\a"
字符集和编码 Unicode 是字符集, Unicode 的编码方案有 UTF-8、UTF-16、UTF-32
bash shell 1 2 3 4 echo $SHELL cat /etc/shells
bash 快捷键 Ctrl + a 光标移到命令行首,相当于 Home Ctrl + e 光标移到命令行尾,相当于 End
hash 缓存表 shell 内部命令本来就在内存中, hash 缓存的是外部命令的路径
1 2 3 4 5 6 hash hash -d COMMANDhash -r
日期和时间 1 2 3 4 5 6 date -d -s clock timedatectl cal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [lujinkai@centos8 ~]$ date +%s 1595857012 [lujinkai@centos8 ~]$ date -d @`date +%s` Mon Jul 27 21:40:31 CST 2020 [lujinkai@centos8 ~]$ date -d @`date +%s` +%F 2020-07-27 [lujinkai@centos8 ~]$ date -d @1595857012 +%F_%T 2020-07-27_21:40:34 [lujinkai@centos8 ~]$date "+%F %T" 2021-03-16 01:12:53 [lujinkai@centos8 ~]$ date -d '-1 day' +%F 2020-07-27 [lujinkai@centos8 ~]$ date -d '1 day' +%F 2020-07-29
1 2 3 4 [root@centos8 ~] [root@centos8 ~]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@centos8 ~] [root@centos8 ~] Asia/Shanghai [root@centos8 ~] [root@centos8 ~] Local time: Mon 2020-07-27 21:56:19 CST Universal time: Mon 2020-07-27 13:56:19 UTC RTC time: Mon 2020-07-27 13:56:19 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive RTC in local TZ: no [root@centos8 ~] lrwxrwxrwx. 1 root root 35 Jul 23 16:33 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
1 2 3 4 5 6 [root@centos8 ~] [root@centos8 ~] [root@centos8 ~]
关机和重启 1 2 3 4 5 6 7 halt poweroff reboot shutdown -h +3 shutdown -h 00:00 "class is over" shutdown -c
但是在 VMware 中, CentOS7、8 和 Ubuntu18.04 关机的时候, 使用halt
会报错, 使用shutdown -h now
就能顺利关机, 不知道为什么?
计算器 bc -q 表示不显示欢迎信息
bc 有四个内置变量,我们在计算时会经常用到,如下表所示:
变量名
作 用
scale
指定精度,也即小数点后的位数;默认为 0,也即不使用小数部分。
ibase
指定输入的数字的进制,默认为十进制。
obase
指定输出的数字的进制,默认为十进制。
last 或者 .
表示最近打印的数字
注意:obase 要尽量放在 ibase 前面,因为 ibase 设置后,后面的数字都是以 ibase 的进制来换算的
命令历史 history 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@4710419222 test ] 7618 2020-07-28 19:19:03 root set -B 7619 2020-07-28 19:19:05 root ls {1,4,6}.log 7620 2020-07-28 19:21:46 root history 7621 2020-07-28 19:22:06 root history 10 7622 2020-07-28 19:22:16 root history 5 [root@4710419222 ~] [root@4710419222 ~] 1 2020-07-28 19:23:15 root history
相关环境变量:
1 2 3 4 5 6 7 8 9 10 HISTSIZE HISTFILE HISTFILESIZE HISTTIMEFORMAT="%F %T \`whoami\` " HISTIGNORE="str1:str2*:…" HISTCONTROL ignoredups ignorespace ignoreboth erasedups
1 2 [root@centos8 profile.d] [root@centos8 profile.d]
获得帮助 (重点) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@4710419222 ~] echo is a shell builtin echo is /usr/bin/ech[root@4710419222 ~] echo (1) - display a line of textecho (3x) - curses input options[root@4710419222 ~] [root@4710419222 ~] hostname is /usr/bin/hostname [root@4710419222 ~]
范例二:
1 2 3 4 5 6 7 8 [root@centos8 ~] history is a shell builtin [root@centos8 ~] history (1) - bash built-in commands, see bash(1)[root@centos8 ~]
man 页面分组 不同类型的帮助称为不同的“章节”,统称为 Linux 手册
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 Executable programs or shell commands 2 System calls (functions provided by the kernel) 3 Library calls (functions within program libraries) 4 Special files (usually found in /dev) 5 File formats and conventions eg /etc/passwd 6 Games 7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7) 8 System administration commands (usually only for root) 9 Kernel routines [Non standard] 1:用户命令 2:系统调用 3:C库调用 4:设备文件及特殊文件 5:配置文件格式 6:游戏 7:杂项 8:管理类的命令 9:Linux 内核API
man 帮助段落说明
NAME 名称及简要说明
SYNOPSIS 用法格式说明
[] 可选内容
<> 必选内容
a|b 二选一
{ } 分组
… 同一内容可出现多次
DESCRIPTION 详细说明
OPTIONS 选项说明
EXAMPLES 示例
FILES 相关文件
AUTHOR 作者
COPYRIGHT 版本信息
REPORTING BUGS bug 信息
从 SEE ALSO 其它帮助参考
man 命令的操作方法
space 下一页
b 上一页
1G 回到首部
G 翻至文件尾部
/keyword 搜索关键字, 使用 n 和 N 上下查找