Linux常用基础操作命令
1.通过包管理器查询命令来源
在 CentOS/RHEL 中,yum 或 dnf 的 provides 功能可直接追溯命令/文件所属的包:
# 查询未安装的命令来源(需启用仓库缓存)
yum provides */lsb_release
# 或
dnf provides */lsb_release
# 输出示例:
redhat-lsb-core-4.1-47.el7.x86_64 : LSB core modules support
Repo : base
Matched from:
Filename : /usr/bin/lsb_release
不同 Linux 发行版查询命令归属的差异:
| 发行版 | 命令未安装时的查询方法 | 已安装时的查询方法 |
|---|---|---|
| CentOS/RHEL | yum provides */<命令名> | rpm -qf $(which <命令名>) |
| Debian/Ubuntu | apt-file search <命令名> | dpkg -S $(which <命令名>) |
| Fedora | dnf provides */<命令名> | rpm -qf $(which <命令名>) |
2.查看 cpu
lscpu命令可以查看cpu信息
cat /proc/cpuinfo也可看查看到
[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 有几个线程
Core(s) per socket: 4 #每个槽位有4个core
Socket(s): 2 #服务器面板上有2个cpu 槽位
NUMA node(s): 2 #nodes的数量
Vendor ID: GenuineIntel
CPU family: 6
Model: 44
Stepping: 2
CPU MHz: 2128.025
BogoMIPS: 4256.03
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0,2,4,6 #对应的core
NUMA node1 CPU(s): 1,3,5,7
[root@centos8 ~]# cat /proc/cpuinfo
查看内存大小
[root@centos8 ~]#free
total used free shared buff/cache available
Mem: 816188 156888 412660 5792 246640 527024
Swap: 2097148 0 2097148
[root@centos8 ~]#cat /proc/meminfo
MemTotal: 816188 kB
MemFree: 412780 kB
MemAvailable: 527144 kB
Buffers: 5304 kB
Cached: 208252 kB
SwapCached: 0 kB
Active: 138976 kB
Inactive: 127508 kB
Active(anon): 53348 kB
Inactive(anon): 5368 kB
Active(file): 85628 kB
Inactive(file): 122140 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 52964 kB
Mapped: 78456 kB
Shmem: 5792 kB
KReclaimable: 33084 kB
Slab: 69144 kB
SReclaimable: 33084 kB
SUnreclaim: 36060 kB
KernelStack: 4428 kB
PageTables: 5888 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 2505240 kB
Committed_AS: 250040 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
HardwareCorrupted: 0 kB
AnonHugePages: 6144 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: 108416 kB
DirectMap2M: 940032 kB
DirectMap1G: 0 kB
[root@centos8 ~]#
查看硬盘和分区情况
[root@centos8 ~]#lsblk
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 7G 0 rom
[root@centos8 ~]#cat /proc/partitions
major minor #blocks name
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 7377920 sr0
查看系统架构
root@ubuntu2004:~# arch
x86_64
[root@centos8 ~]# arch
x86_64
[root@rhel5 ~]# arch
i686
查看内核版本
[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
查看操作系统发行版本
查看代码
#CentOS8 查看发行版本
[root@centos8 ~]#cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@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"
[root@centos8 ~]#lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 8.1.1911 (Core)
Release: 8.1.1911
Codename: Core
[root@centos8 ~]#
#ubuntu查看发行版本
[root@ubuntu1804 ~]#cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.1 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.1 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/privacypolicy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
[root@ubuntu1804 ~]#cat /etc/issue
Ubuntu 18.04.1 LTS \n \l
[root@ubuntu1804 ~]#lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic
[root@ubuntu1804 ~]#
详细内容截取命令
查看代码
[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
日期和时间
Linux的两种时钟
系统时钟:由Linux内核通过CPU的工作频率进行的
硬件时钟:主板
相关命令
date 显示和设置系统时间
[root@centos8 ~]#date +%s
1584689051
[root@centos8 ~]#date -d @`date +%s`
Fri Mar 20 15:24:18 CST 2020
[root@centos8 ~]#date -d @1584689051
Fri Mar 20 15:24:11 CST 2020
[root@centos8 ~]#date -d @1584689051 +%F_%T
2020-03-20_15:24:11
[root@ubuntu ~]#date -d "2020-02-02" +%s
1580572800
clock,hwclock: 显示硬件时钟
-s, --hctosys #以硬件时钟为准,校正系统时钟
-w, --systohc #以系统时钟为准,校正硬件时钟
范例:
[root@centos8 ~]#ll /usr/sbin/clock
lrwxrwxrwx. 1 root root 7 Apr 24 2020 /usr/sbin/clock -> hwclock
创建登录提示文件 /etc/motd
cat /etc/motd
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
佛祖保佑 永不宕机
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
会话管理
命令行的典型使用方式是,打开一个终端窗口(terminal window,以下简称"窗口"),在里面输入命
令。用户与计算机的这种临时的交互,称为一次"会话"(session)
会话的一个重要特点是,窗口与其中启动的进程是连在一起的。打开窗口,会话开始;关闭窗口,会话
结束,会话内部的进程也会随之终止,不管有没有运行完
一个典型的例子就是,SSH 登录远程计算机,打开一个远程窗口执行命令。这时,网络突然断线,再次
登录的时候,是找不回上一次执行的命令的。因为上一次 SSH 会话已经终止了,里面的进程也随之消
失了。为了解决这个问题,会话与窗口可以"解绑":窗口关闭时,会话并不终止,而是继续运行,等到
以后需要的时候,再让会话"绑定"其他窗口
终端复用器软件就是会话与窗口的"解绑"工具,将它们彻底分离。
(1)它允许在单个窗口中,同时访问多个会话。这对于同时运行多个命令行程序很有用。
(2) 它可以让新窗口"接入"已经存在的会话。
(3)它允许每个会话有多个连接窗口,因此可以多人实时共享会话。
(4)它还支持窗口任意的垂直和水平拆分。
类似的终端复用器还有Screen,Tmux
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]
tmux

浙公网安备 33010602011771号