Linux常用基本命令
查看硬件信息
查看cpu
lscpu命令和cat /proc/cpuinfo都可以查看cpu信息,和lscpu不同的是cat /proc/cpuinfo 按照每核显示cpu信息
Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz 表示CPU型号和主频,x86_64表示架构,支持32位和64位。
[root@centos8 ~]# lscpu
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: 4
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
Stepping: 10
CPU MHz: 2904.004
BogoMIPS: 5808.00
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 xsaves arat md_clear flush_l1d arch_capabilities
查看内存
free和cat /proc/meminfo都可以查看内存信息
free 查看内存
[root@centos8 ~]# free -h
total used free shared buff/cache available
Mem: 7.8Gi 725Mi 6.5Gi 10Mi 537Mi 6.8Gi
Swap: 4.0Gi 0B 4.0Gi
cat /proc/meminfo 查看内存
[root@centos8 ~]# cat /proc/meminfo
MemTotal: 8135208 kB
MemFree: 6841296 kB
MemAvailable: 7116284 kB
Buffers: 2192 kB
Cached: 462160 kB
SwapCached: 0 kB
Active: 543264 kB
Inactive: 332812 kB
Active(anon): 413824 kB
Inactive(anon): 9104 kB
Active(file): 129440 kB
Inactive(file): 323708 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 399592 kB
Mapped: 209148 kB
Shmem: 11208 kB
KReclaimable: 86476 kB
Slab: 265476 kB
SReclaimable: 86476 kB
SUnreclaim: 179000 kB
KernelStack: 9248 kB
PageTables: 23904 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 8261904 kB
Committed_AS: 2450964 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
Percpu: 59904 kB
HardwareCorrupted: 0 kB
AnonHugePages: 174080 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: 282432 kB
DirectMap2M: 4960256 kB
DirectMap1G: 5242880 kB
/proc目录的文件内容都是以内存的方式保存,存储当前的系统状态,不会占据磁盘空间。
使用ls -l或者别名ll 可以查看文件的大小
[root@centos8 ~]# ll /proc/cpuinfo
-r--r--r--. 1 root root 0 Aug 26 05:47 /proc/cpuinfo
[root@centos8 ~]# ls -l /proc/meminfo
-r--r--r--. 1 root root 0 Aug 26 05:47 /proc/meminfo
查看磁盘
lsblk可以查看磁盘大小和分区情况
sda表示第一块硬盘,sda1表示第一个分区,依次类推,SIZE表示磁盘的容量
[root@centos8 ~]# 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 4G 0 part [SWAP]
sr0 11:0 1 7.7G 0 rom
除此以外还可以使用 cat /proc/partitions 来查看磁盘大小和分区情况
[root@centos8 ~]# cat /proc/partitions
major minor #blocks name
8 0 209715200 sda
8 1 2097152 sda1
8 2 104857600 sda2
8 3 52428800 sda3
8 4 1 sda4
8 5 4194304 sda5
11 0 8038400 sr0
查看系统信息
查看系统内核版本
Linux的主流发型版支持uname -r 来查看内核版本信息,其中Ubuntu Server使用的内核版本 比较新,其内核版本是5.4.0-42-generic
CentOS7.8的内核版本
[root@centos7 ~]# uname -r
3.10.0-1127.el7.x86_64
CentOS8.2的内核版本
[root@centos8 ~]# uname -r
4.18.0-193.el8.x86_64
Ubuntu Server20.04的内核版本
guanglei@ubuntu-server:~$ uname -r
5.4.0-42-generic
查看操作系统发型版本
CentOS系列可以通过查看/etc/redhat-release 文件获取操作系统的发型版本
CentOS7查看发型版本
[root@centos7 ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
CentOS8查看发型版本
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
Ubunut Server查看/etc/os-release文件获取操作系统的发型版本
guanglei@ubuntu-server:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 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
日期和时间
时间是非常重要的!!!,应用程序,中间件,数据库都会依赖时间
date命令可以显示当前时间
[root@centos8 ~]# date
Wed Aug 26 14:54:38 CST 2020
如果服务器时间不准,还可以使用date修改时间,修改时间的格式为 mmdd hhmmyyyy.ss
使用date命令将当前时间改为2020年9月25号下午三点三十秒
[root@centos8 ~]# date 092615002020.30
Sat Sep 26 15:00:30 CST 2020
[root@centos8 ~]# date
Sat Sep 26 15:00:31 CST 2020
此时系统的时间就是被修改的错误时间,该时间是存储在内存中,此时可以重启服务器,就会恢复正确的时间。
除了系统时间,每台服务器还有硬件时间,硬件时间是从主板上读取的。如果系统时间错误,除了重启系统,还可以使用hwclock -s 命令以硬件时间为准校准系统时间。
[root@centos8 ~]# date
Wed Aug 26 15:08:09 CST 2020
[root@centos8 ~]# date 092612002020.00
Sat Sep 26 12:00:00 CST 2020
[root@centos8 ~]# date
Sat Sep 26 12:00:01 CST 2020
[root@centos8 ~]# hwclock -s
[root@centos8 ~]# date
Wed Aug 26 15:08:45 CST 2020
hwclock命令用于查看系统的硬件时间
[root@centos8 ~]# hwclock
2020-08-26 15:06:13.071486+08:00
假设硬件时间是错误的,可以使用hwclock -w 以系统时间为准,校准硬件时间
[root@centos8 ~]# hwclock -w
在安装系统时,选择的时区是亚洲上海,时区的配置文件是/etc/localtime,该文件是一个链接文件
[root@centos8 ~]# ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Aug 24 08:07 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
不同的时区在同一时刻的时间是不同,timedatectl list-timezones 命令用于列出所有的时区
timedatectl 只支持CentOS7及其以上的版本
[root@centos8 ~]# timedatectl list-timezones
timedatectl set-timezone命令用于修改时区,时区修改后,如果不是一个时区的,修改后的系统时间和北京时间是不一样的。
修改当前时区是欧洲/巴黎,然后再查看系统时间
[root@centos8 ~]# timedatectl set-timezone Europe/Paris
[root@centos8 ~]# date
Wed Aug 26 09:17:44 CEST 2020
修改当前时区为亚洲/上海,然后再查看系统时间,此时时间被修复回来。
[root@centos8 ~]# timedatectl set-timezone Asia/Shanghai
[root@centos8 ~]# date
Wed Aug 26 15:18:57 CST 2020
在安装系统时如果没有改时区(默认是美国纽约时区),这里就可以使用timedatectl set-timezone来修改。
同步其他服务器的时间(企业中一般有专门的时间服务器),可以使用ntpdate ip命令同步系统时间。
[root@centos7 ~]# ntpdate 120.25.108.11
30 Jul 19:35:42 ntpdate[6675]: adjust time server 120.25.108.11 offset -0.214244 sec
显示完整日期和时间
[root@centos8 ~]# date '+%F %T'
2020-08-26 17:27:42
修改时间为10天后的日期后还原修改的时间
[root@centos8 ~]# date -s "+10day"
Sat Sep 5 17:28:06 CST 2020
[root@centos8 ~]# date -s "-10day"
Wed Aug 26 17:28:24 CST 2020
[root@centos8 ~]# date
Wed Aug 26 17:28:47 CST 2020
显示昨天的日期时间
[root@centos8 ~]# date -d yesterday
Tue Aug 25 17:29:14 CST 2020
显示明天的日期
[root@centos8 ~]# date -d tomorrow
Thu Aug 27 17:29:32 CST 2020
显示10天后的日期
[root@centos8 ~]# date -d '10day'
Sat Sep 5 17:29:51 CST 2020
在Linux系统中使用cal命令查看日历
cal默认显示当年的日历
[root@centos8 ~]# cal
August 2020
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
通过cal -y选项可以查看整年的日历
[root@centos8 ~]# cal -y
2020
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 2 3 4 1 1 2 3 4 5 6 7
5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
26 27 28 29 30 31 23 24 25 26 27 28 29 29 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 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
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 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
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 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31
除此以外还可以使用cal 月份 年份查看指定月份的日历
查看2035年10月份的日历
[root@centos8 ~]# cal 10 2035
October 2035
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 ~]# 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
关机和重启
日常运维时关机和重启谨慎使用
关机可以使用halt或者poweroff命令
重启可以使用reboot,其中 -f选项表示强制关机,不调用shutdown,-p表示切断电源。
而shutdown可以实现关机或者重启,shutdown -r表示重启,即reboot,shutdown -h表示关机,即halt。 -c表示取消关机或重启。
除此以外shutdown 还可以指定时间来关机或重启,默认是1分钟后执行。
shutdown命令的常用案例
三分钟后关机,当前时间是22:37
[root@centos8 ~]# shutdown -h 22:40
Shutdown scheduled for Wed 2020-08-26 22:40:00 CST, use 'shutdown -c' to cancel.
取消关机使用shutdown -c命令
[root@centos8 ~]# shutdown -c
5分钟后重启系统
[root@centos8 ~]# shutdown -r +5
Shutdown scheduled for Wed 2020-08-26 22:43:10 CST, use 'shutdown -c' to cancel.
立刻重启
[root@centos8 ~]# shutdown -r now
如果想要知道当前机器开机时间,可以使用uptime查看
CentOS7开机时间
[root@centos7 ~]# uptime
04:47:12 up 32 min, 1 user, load average: 0.00, 0.01, 0.02
CentOS8开机时间
[root@centos8 ~]# uptime
04:47:07 up 1 min, 1 user, load average: 0.14, 0.07, 0.02
Ubuntu20.04开机时间
guanglei@ubuntu20:~$ uptime
20:48:10 up 33 min, 1 user, load average: 0.02, 0.02, 0.00
用户登录信息查看
显示当前登录有效用户
guanglei@ubuntu20:~$ whoami
guanglei
查看系统当前所有的登录会话及所做的操作
[root@centos8 ~]# w
04:50:07 up 4 min, 2 users, load average: 0.20, 0.08, 0.02
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 04:45 1.00s 0.07s 0.03s w
guanglei tty2 tty2 04:50 4:37 3.85s 0.02s /usr/libexec/gsd-mouse
查看系统当前所有的登录会话
[root@centos8 ~]# who
root pts/0 2020-07-27 04:45 (10.0.0.1)
guanglei tty2 2020-07-27 04:50 (tty2)
文本编辑
在系统运维时会经常到编辑文本文件,而运维初学者不建议直接使用功能复杂的vim来编辑文件,使用nano即可。
nano只能编辑文本文件,无法编辑二进制文件。
[root@centos8 ~]# nano .bashrc
nano编辑文件
在使用nano进行文本编辑时,nano后面跟文件名即可打开文件,然后可以直接修改文件,类似于windows的记事本,修改完成后然后按Ctrl+x退出,接着按y保存修改,回车后退出nano。
会话管理
在有些耗时比较长的运维场景下(例如数据库备份等等)希望命令在终端窗口以外关闭后依然能够正常执行,因为一般的命令在终端窗口关闭以后,命令就会意外中断,每个终端窗口就是一个会话,
关闭了终端窗口就等同于关闭了会话,因此执行中的命令会中断。
这时我们可以使用screen命令实现即使关闭窗口,也能让命令在后台执行。
首次执行screen命令时系统会提示找不到命令
[root@centos7 ~]# screen
bash: screen: command not found...
使用yum install -y screen 安装screen命令
此命令暂时只能在CentOS7.8下使用yum来进行安装
[root@centos7 ~]# yum install -y screen
安装完成以后使用screen 开启一个会话
[root@centos7 ~]# screen
然后使用ping命令来检查centOS8的网络是否通畅
[root@centos7 ~]# ping 10.0.0.100
紧接着关闭centOS7的xshell会话窗口,然后重新开一个会话窗口,使用ps aux|grep ping会发现之前运行的ping进程依然在执行
[root@centos7 ~]# ps aux|grep ping
guanglei 2614 0.0 0.0 463332 3892 ? Sl 06:13 0:01 /usr/libexec/gsd-housekeeping
root 14582 0.0 0.0 128552 1272 pts/1 S+ 22:49 0:00 ping 10.0.100
root 14662 0.0 0.0 112812 968 pts/2 S+ 22:50 0:00 grep --color=auto ping
显示所有已经打开的sceen会话,可以使用screen -ls查看
[root@centos7 ~]# screen -ls
There is a screen on:
14684.pts-2.centos7 (Attached)
1 Socket in /var/run/screen/S-root.
恢复某screen会话,可以使用screen -r命令
[root@centos7 ~]# screen -r
64 bytes from 10.0.0.100: icmp_seq=205 ttl=64 time=0.470 ms
64 bytes from 10.0.0.100: icmp_seq=206 ttl=64 time=0.417 ms
64 bytes from 10.0.0.100: icmp_seq=207 ttl=64 time=2.42 ms
64 bytes from 10.0.0.100: icmp_seq=208 ttl=64 time=0.906 ms
64 bytes from 10.0.0.100: icmp_seq=209 ttl=64 time=1.35 ms
screen除了创建会话,能让命令关闭窗口后继续在后台执行的功能以外,还能够实现远程桌面共享,从而实现远程协助的功能。
实现该功能必须满足两个条件,两个用户必须使用同一个账号登录同一台机器。
假设小红红有个问题,需要小明明远程协助。
首先小红红以root账号登录10.0.0.102的机器,即CentOS7.8的那台机器,然后使用screen -S help开启一个远程会话,会话的名字叫help
[root@centos7 ~]# screen -S help
然后小明明也以root的账号登录10.0.0.102的机器,然后使用screen -x help加入小红红开启的远程会话
screen -x help
共享会话
当小明明解决小红红的问题后,可以使用ctrl a,d退出当前会话。
如果小明明不知道小红红的会话名称,可以使用screen -ls获取会话
[root@centos7 ~]# screen -ls
There are screens on:
14877.help (Attached)
14684.pts-2.centos7 (Attached)
2 Sockets in /var/run/screen/S-root.
echo命令
echo用于输出指定的字符串,主要用于输出信息。echo的含义就是回显,即返回显示。
[root@centos8 ~]# echo linux
linux
echo通常用于显示变量,在显示变量时在变量名前面加$
查看PATH变量
[root@centos8 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
查看SHELL和PS1变量
[root@centos8 ~]# echo $SHELL
/bin/bash
[root@centos8 ~]# echo $PS1
[\u@\h \W]\$
除此以外echo还可以启用-e选项结合转义字符实现特殊用途
使用echo结合转义字符\a实现发声
[root@centos7 ~]# echo -e "\a"
睡眠3秒后发声,sleep 3表示睡眠3秒
一行可以同时执行多个命令,多个命令之间使用;分割
[root@centos7 ~]# sleep 3; echo -e "\a"
输出指定区间的数字和字母
[root@centos7 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos7 ~]# echo {1..10..2}
1 3 5 7 9
[root@centos7 ~]# echo {20..10..2}
20 18 16 14 12 10
[root@centos7 ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
输出笛卡尔乘积
[root@centos7 ~]# echo {1..10}{a..c}
1a 1b 1c 2a 2b 2c 3a 3b 3c 4a 4b 4c 5a 5b 5c 6a 6b 6c 7a 7b 7c 8a 8b 8c 9a 9b 9c 10a 10b 10c
使用反向单引号输出echo $PATH的执行结果
[root@centos7 ~]# echo `echo $PATH`
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
使用反向单引号实现以当前日期在/data目录下创建日志文件
[root@centos7 ~]# touch /data/`date +%F`.log
[root@centos7 ~]# ls /data
2020-08-01.log
字符集和编码
ASCII码:计算机内部,所有信息最终都是一个二进制值。上个世纪60年代,美国制定了一套字符编码,对英语字符与二进制位之间的关系,做了统一规定。
ASCII 码一共规定了128个字符的编码,占用了一个字节的后面7位,最前面的一位统一规定为0
Unicode:用于表示世界上所有语言中的所有字符。 每一个符号都给予一个独一无二的编码数字,Unicode 是一个很大的集合,现在的规模可以容纳100多
万个符号。Unicode 仅仅只是一个字符集,规定了每个字符对应的二进制代码,至于这个二进制代码如何存储则没有规定。
Unicode编码方案:
- UTF-8:变长,1到4个字节
- UTF-16:变长,2或4个字节
- UTF-32:固定长度,4个字节
目前广泛使用的都是UTF-8编码,UTF-8编码中一个中文通常占据3个字节。
[root@centos8 ~]# echo $LANG
en_US.UTF-8
为了理解字符与编码的关系。
首先准备一个测试文件,文件内容包含3行,每行一个字符,可以使用nano编辑,然后使用cat查看,查看时根据UTF-8编码通过数值对应的字符进行转换
[root@centos8 ~]# nano test.txt
[root@centos8 ~]# cat test.txt
a
b
c
然后使用hexdump 查看test.txt的底层十六进制存储
[root@centos8 ~]# hexdump test.txt
0000000 0a61 0a62 0a63
0000006
可以使用echo 输出16进制的61,62,63代表的字符
[root@centos8 ~]# echo -e '\x61'
a
[root@centos8 ~]# echo -e '\x62'
b
[root@centos8 ~]# echo -e '\x63'
c