文章目录
一、系统
1.1 系统启动流程
CentOS6 | CentOS7 |
---|---|
开机自检(BIOS) | - |
MBR引导:读取磁盘存储记录信息,引导系统启动 | - |
GRUB菜单:系统选择菜单 | - |
加载内核(kernel) | - |
运行INIT进程(用于串行启动其它进程):系统第一个进程 | 运行systemd进程(用于并行启动其它进程) |
加载系统运行级别 | /etc/systemd/system/default.target |
执行/etc/rc.d/rc.sysinit脚本:设置主机名、IP | /usr/lib/systemd/system/sysinit.target |
执行/etc/rc.d/rc脚本:服务运行的脚本 | /etc/systemd/system |
运行mingetty进程:显示开机登陆信息 | - |
二、硬件信息
2.1 CPU
- 查看CPU信息
[root@locahost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 142
Model name: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
Stepping: 12
CPU MHz: 2304.002
BogoMIPS: 4608.00
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0,1
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss 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 invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves arat md_clear flush_l1d arch_capabilities
[root@locahost ~]# cat /proc/cpuinfo
[root@locahost ~]#
- 查看CPU负载信息
单核CPU负载最好不要超过0.7,多核CPU不要超过N*0.7。
查看计算机的CPU核心数:grep -c ‘model name’ /proc/cpuinfo
[root@locahost ~]# w
09:55:59 up 46 min, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 13.13.1.1 09:52 7.00s 0.02s 0.00s w
[root@locahost ~]# cat /proc/loadavg
0.00 0.00 0.00 3/219 5443
[root@locahost ~]#
2.2 内存
- 查看使用情况
[root@locahost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 264Mi 1.2Gi 8.0Mi 284Mi 1.3Gi
Swap: 2.0Gi 0B 2.0Gi
[root@locahost ~]# cat /proc/meminfo
[root@locahost ~]#
- 增加swap空间
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 242Mi 274Mi 8.0Mi 1.3Gi 1.4Gi
Swap: 2.0Gi 0B 2.0Gi
[root@localhost ~]# dd if=/dev/zero of=/swap bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 5.84122 s, 184 MB/s
[root@localhost ~]# mkswap /swap
mkswap: /swap: insecure permissions 0644, 0600 suggested.
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=a681b34f-2f5c-4547-a83d-702f3bdcf2d3
[root@localhost ~]# chmod 0600 /swap
[root@localhost ~]# swapon /swap
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 242Mi 274Mi 8.0Mi 1.3Gi 1.4Gi
Swap: 3.0Gi 0B 3.0Gi
[root@localhost ~]# swapoff /swap
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 242Mi 274Mi 8.0Mi 1.3Gi 1.4Gi
Swap: 2.0Gi 0B 2.0Gi
[root@localhost ~]#
- 禁用swap
[root@localhost ~]# # 临时关闭
[root@localhost ~]# swapoff -a
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8Gi 240Mi 1.3Gi 8.0Mi 256Mi 1.4Gi
Swap: 0B 0B 0B
[root@localhost ~]# # 永久关闭
[root@localhost ~]# cp /etc/fstab{,.bak}
[root@localhost ~]# sed 's/\(.*swap.*\)/#\1/' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Sep 26 16:29:04 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=90204c1b-0e93-4936-9b55-ee8f539c6c6a /boot ext4 defaults 1 2
#/dev/mapper/cl-swap swap swap defaults 0 0
[root@localhost ~]# sed -i 's/\(.*swap.*\)/#\1/' /etc/fstab
[root@localhost ~]#
2.3 磁盘
- 查看Block使用情况
Block = 磁盘容量
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 883M 0 883M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.7M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.5G 16G 9% /
/dev/nvme0n1p1 976M 142M 767M 16% /boot
tmpfs 181M 0 181M 0% /run/user/0
[root@localhost ~]#
- 查看Inode使用情况
Inode = 可创建文件数
[root@localhost ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 225975 399 225576 1% /dev
tmpfs 230483 1 230482 1% /dev/shm
tmpfs 230483 595 229888 1% /run
tmpfs 230483 17 230466 1% /sys/fs/cgroup
/dev/mapper/cl-root 8910848 32739 8878109 1% /
/dev/nvme0n1p1 65536 309 65227 1% /boot
tmpfs 230483 5 230478 1% /run/user/0
[root@localhost ~]#
- 打印磁盘列表
[root@ansible ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 7.7G 0 rom
nvme0n1 259:0 0 20G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 19G 0 part
├─cl-root 253:0 0 17G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
[root@ansible ~]#
2.4 分区
- 分区表
限额 | MBR | GPT |
---|---|---|
磁盘大小 | 2TB | 18EB |
分区数量 | 4 | 128+ |
- 查看磁盘分区信息
[root@ansible ~]# parted /dev/nvme0n1 print
Model: NVMe Device (nvme)
Disk /dev/nvme0n1: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1075MB 1074MB primary ext4 boot
2 1075MB 21.5GB 20.4GB primary lvm
[root@ansible ~]#
[root@ansible ~]# fdisk -l /dev/nvme0n1
Disk /dev/nvme0n1: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdefe9a7f
Device Boot Start End Sectors Size Id Type
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 41943039 39843840 19G 8e Linux LVM
[root@ansible ~]#
- MBR
- 查看帮助
[root@ansible ~]# fdisk /dev/nvme0n2
Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x55ec1424.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help):
- 新增分区
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +1G
Created a new partition 1 of type 'Linux' and of size 1 GiB.
Command (m for help): p
Disk /dev/nvme0n2: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x55ec1424
Device Boot Start End Sectors Size Id Type
/dev/nvme0n2p1 2048 2099199 2097152 1G 83 Linux
Command (m for help):
- [修改分区类型]
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ansible ~]#
- 通知操作系统分区变化
partprobe - inform the OS of partition table changes
Alter partition table of disk which is installed OS and which is "not" installed OS
For first one, you need to run "partprobe" command in order to re-read the partition table
you have been change.
For second one, you don't need to run partprobe. Once you written at fdisk utility, you should
able to mount the partition.
[root@ansible ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@ansible ~]#
- 初始化文件系统
[root@ansible ~]# mkfs.xfs /dev/nvme0n2p1
meta-data=/dev/nvme0n2p1 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@ansible ~]#
- 挂载分区目录
[root@ansible ~]# mkdir /mnt/MBR
[root@ansible ~]# mount /dev/nvme0n2p1 /mnt/MBR/
[root@ansible ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 884M 0 884M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.8M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.9G 16G 11% /
/dev/nvme0n1p1 976M 199M 711M 22% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/nvme0n2p1 1014M 40M 975M 4% /mnt/MBR
[root@ansible ~]#
- 查看设备的UUID
[root@ansible ~]# blkid | grep /dev/nvme0n2p1
/dev/nvme0n2p1: UUID="2f6fd688-3363-4e9e-ab3b-252a2a31e406" TYPE="xfs" PARTUUID="55ec1424-01"
[root@ansible ~]#
- 设置开机自动挂载
[root@ansible ~]# tail -1 /etc/fstab
UUID=2f6fd688-3363-4e9e-ab3b-252a2a31e406 /mnt/MBR xfs defaults 0 0
[root@ansible ~]# umount /mnt/MBR
[root@ansible ~]# mount -a
[root@ansible ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 884M 0 884M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.8M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.9G 16G 11% /
/dev/nvme0n1p1 976M 199M 711M 22% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/nvme0n2p1 1014M 40M 975M 4% /mnt/MBR
[root@ansible ~]#
- GPT
DISK LABELS
GPT (GUID Partition Table)
GPT is modern standard for the layout of the partition table. GPT uses 64-bit logical block
addresses, checksums, UUIDs and names for partitions and an unlimited number of partitions
(although the number of partitions is usually restricted to 128 in many partitioning tools).
Note that the first sector is still reserved for a protective MBR in the GPT specification.
It prevents MBR-only partitioning tools from mis-recognizing and overwriting GPT disks.
GPT is always a better choice than MBR, especially on modern hardware with a UEFI boot loader.
- 新增一块大于2T的磁盘
[root@ansible ~]# parted /dev/nvme0n3 print
Error: /dev/nvme0n3: unrecognised disk label
Model: NVMe Device (nvme)
Disk /dev/nvme0n3: 4398GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
[root@ansible ~]# fdisk /dev/nvme0n3
....
Device does not contain a recognized partition table.
The size of this disk is 4 TiB (4398046511104 bytes). DOS partition table format cannot
be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use
GUID partition table format (GPT).
....
- 创建分区
Command (m for help): g
Created a new GPT disklabel (GUID: D88FD3C6-2F6F-EA4B-B12C-CF50FA46F1B7).
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-8589934558, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-8589934558, default 8589934558): +3T
Created a new partition 1 of type 'Linux filesystem' and of size 3 TiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ansible ~]#
- 查看
[root@ansible ~]# fdisk -l /dev/nvme0n3
Disk /dev/nvme0n3: 4 TiB, 4398046511104 bytes, 8589934592 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: D88FD3C6-2F6F-EA4B-B12C-CF50FA46F1B7
Device Start End Sectors Size Type
/dev/nvme0n3p1 2048 6442452991 6442450944 3T Linux filesystem
[root@ansible ~]#
- 挂载
[root@ansible ~]# partprobe
[root@ansible ~]# mkfs.xfs /dev/nvme0n3p1
meta-data=/dev/nvme0n3p1 isize=512 agcount=4, agsize=201326592 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=805306368, imaxpct=5
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=393216, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@ansible ~]# mkdir /mnt/GPT
[root@ansible ~]# blkid | grep nvme0n3p1
/dev/nvme0n3p1: UUID="433e6bb8-facc-41ed-980f-17ef311f49f1" TYPE="xfs" PARTUUID="210ad650-1453-874b-a758-a74d3dcd9276"
[root@ansible ~]# echo "UUID=433e6bb8-facc-41ed-980f-17ef311f49f1 /mnt/GPT xfs defaults 0 0" >> /etc/fstab
[root@ansible ~]# mount -a
[root@ansible ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 884M 0 884M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 8.8M 892M 1% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/cl-root 17G 1.9G 16G 11% /
/dev/nvme0n1p1 976M 199M 711M 22% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/nvme0n3p1 3.0T 22G 3.0T 1% /mnt/GPT
[root@ansible ~]#
三、环境信息
3.1 登陆提示
- 登陆前提示信息
[root@localhost ~]# cat /etc/issue
\S
Kernel \r on an \m
[root@localhost ~]# cat /etc/issue.net
\S
Kernel \r on an \m
[root@localhost ~]#
- 登陆后提示信息
[root@localhost ~]# cat /etc/motd
[root@localhost ~]#
- 查看缩写具体含义
[root@localhost ~]# man agetty | sed -n '/^ISSUE FILES/,/^FILES/p'
ISSUE FILES
The default issue file is /etc/issue. If the file exists then agetty also checks for /etc/issue.d directory. The directory is optional extension to
the default issue file and content of the directory is printed after /etc/issue content. If the /etc/issue does not exist than the directory is
ignored. All files with .issue extension from the directory are printed in version-sort order. The directory allow to maintain 3rd-party messages
independently on the primary system /etc/issue file.
The default path maybe overridden by --issue-file option. In this case specified path has to be file or directory and the default /etc/issue as well
as /etc/issue.d are ignored.
The issue files may contain certain escape codes to display the system name, date, time etcetera. All escape codes consist of a backslash (\) imme‐
diately followed by one of the characters listed below.
4 or 4{interface}
Insert the IPv4 address of the specified network interface (for example: \4{eth0}). If the interface argument is not specified, then select
the first fully configured (UP, non-LOCALBACK, RUNNING) interface. If not any configured interface is found, fall back to the IP address of
the machine's hostname.
6 or 6{interface}
The same as \4 but for IPv6.
b Insert the baudrate of the current line.
d Insert the current date.
e or e{name}
Translate the human-readable name to an escape sequence and insert it (for example: \e{red}Alert text.\e{reset}). If the name argument is
not specified, then insert \033. The currently supported names are: black, blink, blue, bold, brown, cyan, darkgray, gray, green, half‐
bright, lightblue, lightcyan, lightgray, lightgreen, lightmagenta, lightred, magenta, red, reset, reverse, and yellow. All unknown names are
silently ignored.
s Insert the system name (the name of the operating system). Same as 'uname -s'. See also the \S escape code.
S or S{VARIABLE}
Insert the VARIABLE data from /etc/os-release. If this file does not exist then fall back to /usr/lib/os-release. If the VARIABLE argument
is not specified, then use PRETTY_NAME from the file or the system name (see \s). This escape code allows to keep /etc/issue distribution
and release independent. Note that \S{ANSI_COLOR} is converted to the real terminal escape sequence.
l Insert the name of the current tty line.
m Insert the architecture identifier of the machine. Same as 'uname -m'.
n Insert the nodename of the machine, also known as the hostname. Same as 'uname -n'.
o Insert the NIS domainname of the machine. Same as 'hostname -d'.
O Insert the DNS domainname of the machine.
r Insert the release number of the OS. Same as 'uname -r'.
t Insert the current time.
u Insert the number of current users logged in.
U Insert the string "1 user" or "<n> users" where <n> is the number of current users logged in.
v Insert the version of the OS, that is, the build-date and such.
An example. On my system, the following /etc/issue file:
This is \n.\o (\s \m \r) \t
displays as:
This is thingol.orcan.dk (Linux i386 1.1.9) 18:29:30
FILES
[root@localhost ~]#
3.2 命令行提示
- 查看
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
[root@localhost ~]#
- 查看缩写含义
[root@localhost ~]# man bash | grep -A 32 ^PROMPTING
PROMPTING
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs
more input to complete a command. Bash displays PS0 after it reads a command but before executing it. Bash allows these prompt strings to be cus‐
tomized by inserting a number of backslash-escaped special characters that are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific
time representation. The braces are required
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
\W the basename of the current working directory, with $HOME abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] end a sequence of non-printing characters
[root@localhost ~]#
3.3 字符集
- 查看当前字符集
[root@locahost ~]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
[root@locahost ~]#
- 查看可用字符集
[root@locahost ~]# locale -a
C
C.utf8
en_AG
en_AU
en_AU.utf8
en_BW
en_BW.utf8
en_CA
en_CA.utf8
en_DK
en_DK.utf8
en_GB
en_GB.iso885915
en_GB.utf8
en_HK
en_HK.utf8
en_IE
en_IE@euro
en_IE.utf8
en_IL
en_IN
en_NG
en_NZ
en_NZ.utf8
en_PH
en_PH.utf8
en_SC.utf8
en_SG
en_SG.utf8
en_US
en_US.iso885915
en_US.utf8
en_ZA
en_ZA.utf8
en_ZM
en_ZW
en_ZW.utf8
POSIX
[root@locahost ~]#
- 设置中文字符集
[root@locahost ~]# dnf install glibc-langpack-zh -y
[root@locahost ~]# locale -a | grep zh_CN
zh_CN
zh_CN.gb18030
zh_CN.gbk
zh_CN.utf8
[root@locahost ~]# localectl set-locale LANG=zh_CN.UTF-8
[root@locahost ~]#
3.4 时区
- 查看
[root@localhost ~]# timedatectl
Local time: Thu 2020-10-15 22:27:24 EDT
Universal time: Fri 2020-10-16 02:27:24 UTC
RTC time: Fri 2020-10-16 02:27:24
Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@localhost ~]#
- 查看可用
[root@centos8 ~]# timedatectl list-timezones | grep Asia/Shanghai
Asia/Shanghai
[root@centos8 ~]#
- 修改
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai
[root@localhost ~]# timedatectl
Local time: Fri 2020-10-16 10:29:00 CST
Universal time: Fri 2020-10-16 02:29:00 UTC
RTC time: Fri 2020-10-16 02:29:00
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@localhost ~]#
3.5 主机名
localhost.localdomain:主机名.域名,重设后登出登入刷新。
[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostnamectl set-hostname centos8
[root@localhost ~]#
3.6 系统运行级别
- 查看当前
[root@centos8 ~]# systemctl get-default
multi-user.target
[root@centos8 ~]#
- 查看可用
[root@centos8 ~]# ll /lib/systemd/system/runlevel*target
lrwxrwxrwx. 1 root root 15 Apr 24 11:53 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Apr 24 11:53 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Apr 24 11:53 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Apr 24 11:53 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Apr 24 11:53 /lib/systemd/system/runlevel6.target -> reboot.target
[root@centos8 ~]#
- 修改
[root@centos8 ~]# systemctl set-default rescue.target
Removed /etc/systemd/system/default.target.
Created symlink /etc/systemd/system/default.target → /usr/lib/systemd/system/rescue.target.
[root@centos8 ~]# systemctl reboot
四、进程信息
4.1 管理进程
- 后台进程
命令 | 说明 |
---|---|
command & | 将进程置于后台运行 |
Ctrl + Z | 将进程暂停并置于后台 |
jobs | 查看后台进程 |
fg %[job-id] | 取后台进程到前台运行 |
bg %[job-id] | 后台运行暂停状态的后台进程 |
- 查看后台进程
参数 | 说明 |
---|---|
jobs -l | 同时列出进程ID |
jobs + | 最后一个放入后台的进程 |
jobs - | 倒数第二个放入后台的进程 |
- 查看进程文件占用
命令 | 说明 |
---|---|
lsof [file] | 查看文件被哪个进程占用 |
kill -HUP `lsof -t [file]` | 告知进程释放该文件占用 |
lsof -p [pid] | 查看进程占用的文件 |
lsof -u [uname | uid ] | 查看用户占用的文件 |
4.2 {R,E,S}UID
ref:https://en.wikipedia.org/wiki/User_identifier
RUID:谁启动了这个进程
EUID:进程是以谁的身份运行的
SUID:进程运行时若发生身份切换,EUID暂存的位置
(swap: suid=euid;euid=ruid => swap-back: euid=suid)
4.3 打印进程
- 全进程全输出
[root@ansible ~]# ps -ef | head -4
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 18:24 ? 00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 17
root 2 0 0 18:24 ? 00:00:00 [kthreadd]
root 3 2 0 18:24 ? 00:00:00 [rcu_gp]
[root@ansible ~]#
- 定制输出列
[root@ansible ~]# ps -e -o pid,user,cmd | head -4
PID USER CMD
1 root /usr/lib/systemd/systemd --switched-root --system --deserialize 17
2 root [kthreadd]
3 root [rcu_gp]
[root@ansible ~]#
- 打印属于当前用户的进程
[rayslee@ansible ~]$ ps -x
PID TTY STAT TIME COMMAND
4842 pts/7 S 0:00 -bash
4867 pts/7 R+ 0:00 ps -x
[rayslee@ansible ~]$
- 打印属于特定用户的进程
[root@ansible ~]# ps -u apache
PID TTY TIME CMD
4539 ? 00:00:00 httpd
4540 ? 00:00:00 httpd
4541 ? 00:00:00 httpd
4542 ? 00:00:00 httpd
[root@ansible ~]#
- 打印属于特定命令的进程
[root@ansible ~]# ps -C httpd
PID TTY TIME CMD
4538 ? 00:00:00 httpd
4539 ? 00:00:00 httpd
4540 ? 00:00:00 httpd
4541 ? 00:00:00 httpd
4542 ? 00:00:00 httpd
[root@ansible ~]#
- 打印进程树
[root@ansible ~]# ps -C httpd --forest
PID TTY TIME CMD
4538 ? 00:00:00 httpd
4539 ? 00:00:00 \_ httpd
4540 ? 00:00:00 \_ httpd
4541 ? 00:00:00 \_ httpd
4542 ? 00:00:00 \_ httpd
[root@ansible ~]#
- 打印进程的SELinux上下文
[root@ansible ~]# ps -e --context | grep httpd
4538 system_u:system_r:httpd_t:s0 /usr/sbin/httpd -DFOREGROUND
4539 system_u:system_r:httpd_t:s0 /usr/sbin/httpd -DFOREGROUND
4540 system_u:system_r:httpd_t:s0 /usr/sbin/httpd -DFOREGROUND
4541 system_u:system_r:httpd_t:s0 /usr/sbin/httpd -DFOREGROUND
4542 system_u:system_r:httpd_t:s0 /usr/sbin/httpd -DFOREGROUND
4908 unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 grep --color=auto httpd
[root@ansible ~]#
五、资源信息
5.1 CPU 资源
top
键 | 说明 |
---|---|
c | 显示出完整的进程信息 |
C | 按使用率排序 |
sar
查看系统 CPU 的负载状况,每 1 秒统计一次,统计 3 次。
[root@localhost ~]# sar -u 1 3
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
07:38:02 PM CPU %user %nice %system %iowait %steal %idle
07:38:03 PM all 0.00 0.00 0.25 0.00 0.00 99.75
07:38:04 PM all 0.00 0.00 0.00 0.00 0.00 100.00
07:38:05 PM all 0.00 0.00 0.25 0.00 0.00 99.75
Average: all 0.00 0.00 0.17 0.00 0.00 99.83
[root@localhost ~]#
5.2 内存资源
top
键 | 说明 |
---|---|
M | 按使用率排序 |
sar
查看系统内存使用状况,每 1 秒统计一次,统计 3 次。
[root@localhost ~]# sar -rh 1 3
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
07:41:39 PM kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
07:41:40 PM 230.3M 477.0M 577.8M 71.5% 3.1M 334.4M 321.1M 11.2% 298.7M 122.6M 0.0k
07:41:41 PM 230.1M 476.9M 577.9M 71.5% 3.1M 334.4M 321.1M 11.2% 298.9M 122.6M 0.0k
07:41:42 PM 230.1M 476.9M 577.9M 71.5% 3.1M 334.4M 321.1M 11.2% 298.9M 122.6M 0.0k
Average: 230.2M 476.9M 577.9M 71.5% 3.1M 334.4M 321.1M 11.2% 298.8M 122.6M 0.0k
[root@localhost ~]#
5.3 磁盘 IO
iostat
[root@localhost ~]# iostat -h
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.1% 0.1% 0.9% 0.2% 0.0% 96.8%
tps kB_read/s kB_wrtn/s kB_read kB_wrtn Device
37.94 766.2k 214.4k 321.9M 90.1M sda
34.12 668.5k 217.2k 280.8M 91.2M dm-0
0.23 5.2k 0.0k 2.2M 0.0k dm-1
[root@localhost ~]#
pidstat
[root@localhost ~]# pidstat -d --human
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
07:34:20 PM UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
07:34:20 PM 0 1 219.6k 11.8k 96.4B 22 systemd
07:34:20 PM 0 593 51.4B 0.0B 0.0B 5 xfsaild/dm-0
07:34:20 PM 0 691 282.8B 0.0B 0.0B 0 systemd-journal
07:34:20 PM 0 722 23.2k 0.0B 0.0B 2 systemd-udevd
07:34:20 PM 0 847 6.8k 186.4B 0.0B 29 sssd
07:34:20 PM 997 850 2.2k 0.0B 0.0B 0 lsmd
07:34:20 PM 0 851 3.3k 0.0B 0.0B 3 smartd
07:34:20 PM 81 853 2.1k 0.0B 0.0B 7 dbus-daemon
07:34:20 PM 0 855 803.4B 0.0B 0.0B 1 irqbalance
07:34:20 PM 0 1121 694.1B 0.0B 0.0B 0 agetty
07:34:20 PM 0 1165 141.4B 0.0B 0.0B 0 systemd
07:34:20 PM 0 1223 201.5k 54.7k 4.0k 1 bash
[root@localhost ~]#
sar
查看系统磁盘读写情况,每 1 秒统计一次,统计 3 次。
[root@localhost ~]# sar -d 1 3
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
07:37:45 PM DEV tps rkB/s wkB/s areq-sz aqu-sz await svctm %util
07:37:46 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:46 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:46 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:46 PM DEV tps rkB/s wkB/s areq-sz aqu-sz await svctm %util
07:37:47 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:47 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:47 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:47 PM DEV tps rkB/s wkB/s areq-sz aqu-sz await svctm %util
07:37:48 PM dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:48 PM dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:37:48 PM dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: DEV tps rkB/s wkB/s areq-sz aqu-sz await svctm %util
Average: dev8-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev253-0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Average: dev253-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
[root@localhost ~]#
5.4 网络 IO
sar
查看系统网络发送接收情况,每 1 秒统计一次,统计 3 次。
[root@localhost ~]# sar -n DEV 1 3 -h
Linux 4.18.0-240.el8.x86_64 (localhost.localdomain) 01/12/2021 _x86_64_ (4 CPU)
07:45:59 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
07:46:00 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s10
07:46:00 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s9
07:46:00 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s3
07:46:00 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% lo
07:46:00 PM 0.99 0.99 59.4B 176.2B 0.00 0.00 0.00 0.0% enp0s8
07:46:00 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
07:46:01 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s10
07:46:01 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s9
07:46:01 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s3
07:46:01 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% lo
07:46:01 PM 1.00 1.00 60.0B 690.0B 0.00 0.00 0.00 0.0% enp0s8
07:46:01 PM rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
07:46:02 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s10
07:46:02 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s9
07:46:02 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s3
07:46:02 PM 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% lo
07:46:02 PM 2.00 4.00 120.0B 968.0B 0.00 0.00 0.00 0.0% enp0s8
Average: rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s %ifutil IFACE
Average: 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s10
Average: 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s9
Average: 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% enp0s3
Average: 0.00 0.00 0.0B 0.0B 0.00 0.00 0.00 0.0% lo
Average: 1.33 1.99 79.7B 610.0B 0.00 0.00 0.00 0.0% enp0s8
[root@localhost ~]#
ifconfig
errors
表示收包错误的总数量;dropped
表示数据包已经进入了 Ring Buffer,但是由于内存不够等系统原因,导致在拷贝到内存的过程中被丢弃的总数量;overruns
表示 Ring Buffer 队列中被丢弃的报文数目,由于 Ring Buffer(aka Driver Queue)传输的 IO 大于 kernel 能够处理的 IO 导致;
六、日志信息
6.1 日志轮替
- 配置文件
[root@master01 ~]# grep -Ev '^$|^#' /etc/logrotate.conf
weekly
rotate 4
create
dateext
include /etc/logrotate.d
[root@master01 ~]#
配置项 | 说明 |
---|---|
weekly | 每周进行一次日志轮替 |
rotate 4 | 保留四周(次)的日志 |
create | 轮替后创建新的日志文件 |
dateext | 轮替的日志文件后缀名采用日期格式 |
include /etc/logrotate.d | 该目录下的配置文件同样生效 |
- 再举一个例子
[root@ansible ~]# cat /etc/logrotate.d/sssd
/var/log/sssd/*.log {
weekly
missingok
notifempty
sharedscripts
rotate 2
compress
delaycompress
postrotate
/bin/kill -HUP `cat /var/run/sssd.pid 2>/dev/null` 2> /dev/null || true
endscript
}
[root@ansible ~]#
配置项 | 说明 |
---|---|
missingok | 即便找不到日志文件(/var/log/sssd/*.log )也不要报错 |
notifempty | 日志文件为空的话就不要进行轮替了 |
sharedscripts | 脚本只执行一次,不管有几个日志文件 |
compress | 使用gzip对轮替的日志文件进行压缩 |
delaycompress | 推迟到下次日志轮替时再压缩这次轮替的日志文件 |
postrotate/endscript | 日志轮替后要进行的动作 |
新的日志文件可能并会被应用程序立即识别,使用
kill -HUP
给它发送个信号吧!
[root@ansible ~]# man sssd | grep 'SIGHUP' -A 2
SIGHUP
Tells the SSSD to stop writing to its current debug file descriptors and to close and reopen them. This is meant to facilitate log rolling with
programs like logrotate.
[root@ansible ~]#
七、定时任务
- 同步/异步定时任务
对比项 | Crond | Anaron |
---|---|---|
功用 | 在设定的时刻,执行某项任务,可以精确到分钟。 | 周期性的执行某项任务(小时、天、周、月) |
区别 | 严格恪守设定时间,运行时机错过即错过 | 不设想系统一直运行,定时检测周期任务执行状况,择时查漏补缺。 |
配置文件 | /etc/crontab 、/etc/cron.d | /etc/anacrontab |
交互 | 每小时执行一次anacron 命令 | 将/etc/cron.{daily, weekly, monthly} 当作周期任务 |
- Crond保障Anacron每小时运行一次
[root@ansible ~]# tail -1 /etc/cron.d/0hourly
01 * * * * root run-parts /etc/cron.hourly
[root@ansible ~]# tail -1 /etc/cron.hourly/0anacron
/usr/sbin/anacron -s
[root@ansible ~]#
- Anacron保障Crond的周期性任务准确执行
[root@ansible ~]# tail -4 /etc/anacrontab
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
[root@ansible ~]#
7.1 同步定时任务
- 格式说明
[root@master01 ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
[root@master01 ~]#
- 做一个小例子
[root@master01 ~]# echo "echo 'Hello World' >> /tmp/test-cron-job.out" > test-cron-job.sh
[root@master01 ~]# chmod +x test-cron-job.sh
[root@master01 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@master01 ~]# crontab -l
* * * * * /root/test-cron-job.sh
[root@master01 ~]# tail -f /tmp/test-cron-job.out
Hello World
Hello World
- 符号说明
符号 | 说明 |
---|---|
* | 任何 |
, | 多个(0 8,12,16 * * * = 每天8点,12点和16点执行) |
- | 区段(0 5 * * 1-6 = 周一到周六每早五点整执行) |
/ | 每(*/10 * * * * = 每隔10分钟执行一次) |
@hourly | 每小时 |
@daily | 每天 |
@weekly | 每周 |
@monthly | 每月 |
@yearly | 每年 |
7.2 异步定时任务
[root@master01 ~]# ll -d /etc/cron*ly
drwxr-xr-x. 2 root root 23 Sep 27 04:29 /etc/cron.daily
drwxr-xr-x. 2 root root 22 Sep 27 04:29 /etc/cron.hourly
drwxr-xr-x. 2 root root 6 May 11 2019 /etc/cron.monthly
drwxr-xr-x. 2 root root 6 May 11 2019 /etc/cron.weekly
[root@master01 ~]#
放置于
/etc/cron.{hourly, daily, weekly, monthly}
中的脚本需可执行且无后缀名。
[root@master01 ~]# ll /etc/cron.hourly/0anacron
-rwxr-xr-x. 1 root root 575 Nov 9 2019 /etc/cron.hourly/0anacron
[root@master01 ~]#