基础命令操作

系统命令

1
2
3
4
head test.txt # 默认显示文件头10行

head -n 20 test.txt # 显示test.txt头20行
head -20 test.txt # 同

tail

1
2
3
4
5
6
7
8
9
10
tail test.txt # 默认显示test.txt文件末尾10行

tail -n 20 test.txt # 显示test.txt文件末尾20行
tail -20 test.txt # 同

tail -f test.txt # 不退出文件,实时显示文件test.txt末尾的变化

tailf test.txt # tailf不是别名而独立的命令,同tail -f效果一样

tail -F test2.txt # 也是实时显示末尾变化,但是文件不存在也行,一存在test2.txt文件就显示末尾变化

less

1
2
3
less /var/log/messages

cat file | less # 支持管道

进入less之后可选操作

  • -N:设置行号
  • 空格/f:往下翻一页
  • b:往上翻一页
  • /:搜索,n匹配成功的下一个,N匹配成功的上一个
  • G:到文件尾部
  • g:到文件头部
  • ng:跳转到第n行

wc

1
2
3
4
5
6
7
8
9
10
11
12
13
wc test.txt # 显示详细信息
3 6 28 test.txt
行数 单词数 字符数 文件名

wc -l test.txt # 统计test.txt行数,这类主要是shell编程

wc -L test.txt # 统计test.txt字符串行最长行的字符数

wc -c test.txt # 统计test.txt的字符数

wc -w test.txt # 统计test.txt的单词数

cat file | wc -l # 支持管道
  • -l:line行数
  • -c:char字符数
  • -w:word单词数

sort

按字符串排序(排序从左到右依次按字符大小比较排序)

1
sort test.txt # 排序test.txt输出显示到屏幕

按数字大小排序

1
sort -n test.txt # 按照数字大小排序

逆序排序

1
sort -r test.txt # 逆序字符串排序

指定列排序

1
sort -k 2 test.txt # 按第2列进行字符串排序

uniq

uniq去重(uniq只能去重相邻相同的字符,也就是需要sort才能生效)

1
sort test.txt |uniq # 字符串排序去重相同字符串输出

uniq去重并统计出现次数

1
cat test.txt |sort|uniq -c

取出出现次数最多的前3个单词

1
cat test|sort|uniq -c|sort -rn|head -n 3

xargs

行转换为列

1
2
3
4
5
6
7
8
# test.txt:1 2 3 4 
xargs -n1 test.txt

# 转换成一列输出
# 1
# 2
# 3
# 4

行转换成n列

1
echo {1..10}|xargs -n 3 # 转换成3列

系统基本配置

查看系统版本号

1
2
3
cat redhat-release

hostnamectl

查看系统内核版本

1
2
3
4
uname --help # 查看命令帮助,不用死记

uname -a # 显示所有内核信息
uname -r # 查看内核版本号

查看时间

时间:

  • 硬件时间 — BIOS时间
  • 系统时间 —- 操作系统看到的时间

时间标准:

  • CST:China Standard Time中国标准时间,等于UTC+8
  • UTC:University Time Coordinated全球通用的时间标准
  • ETD:Eastern Daylight Time东部夏令时间,北美东部地区,等于UTC-4;eg:ETD=UTC-4,CST=UTC+8 –> ETD+12=CST,所以一般设置语言为英文,timezone为America/New_York的刚好和国内的差12小时,违和感不大

查看系统时间

1
date +%F-%H-%M-%S # 输出YYYY-MM-DD - HH - MM - SS格式的系统时间,例如2025-09-24-08-47-07

查看硬件时间

1
clock

同步系统时间

1
ntpdate ntp1.aliyun.com # 用阿里云的时间同步服务器

将系统时间同步到硬件时间

1
1.hwclock -w # --systohc,sys to hc

selinux配置

selinux是国外的,但凡涉及安全之类的,美国国家安全局还是有插手的,有没有开后门什么的就不知道了,一般企业中不开启selinux,如果开启的话需要对标签、规则需要很熟悉,还是比较麻烦的,管理selinux上下文需要要安装policycoreutils和policycoreutils-python-utils。

1
2
3
4
5
6
7
8
9
getenforce # 查看当前selinux模式
开启,阻止
setenforce 1 # 设置临时selinux为enforcing,开启,阻止
setenforce 0 # 设置临时selinux为permissive,不阻止,留日志

# 一般永久关闭要修改/etc/selinux/config配置文件
vim /etc/selinux/config
# 修改SELINUX
SELINUX=disabled # 永久不开启

防火墙配置

1
2
3
4
5
6
7
systemctl stop firewalld # 停止防火墙服务

systemctl start firewalld # 启动防火墙服务

systemctl enable --now firewalld # 立即启用防火墙服务并开机自启

systemctl disable --now firewalld # 立即停止防火墙服务并开机禁止自启

什么情况需要开启防火墙:

  • 在公网
  • 用户可以直接访问的服务器,NAT转换,DMZ映射
  • 有公网的服务器

什么情况需要关闭防火墙:

  • 局域网,没必要
  • 没公网IP,没必要
  • 内部测试服务器
  • 高并发需要关闭firewalld,高并发情况开启firewalld会影响服务器速度。解决:部署硬件防火墙

基本防火墙管理

扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
firewall-cmd --list-all # 列出当前激活的zone信息

firewall-cmd --permanent --add-port=21/tcp # 开发21端口tcp协议,没有--permanent就是临时开放
firewall-cmd --reload

firewall-cmd --permanent --remove-port=21/tcp # 移除
firewall-cmd --reload

# 阻止192.168.12.11全部请求,zone为block的方案
firewall-cmd --zone=block --add-source=192.168.12.11/32 --permanent

# 阻止192.168.12.11全部请求,zone为Public自定义方案
firewall-cmd --permanent --zone=public --add-rich-rule=“rule family=‘ipv4’ source address=’192.168.12.11‘ drop“

# 允许IP为192.168.1.50的以tcp协议访问22端口
firewall-cmd --permanent --zone=public --add-rich-rule=“rule family=’ipv4‘ source address=‘192.168.1.50’ port protocol=‘tcp’ port=‘22’ accept“

yum仓库配置

配置阿里源

1
2
3
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # 备份仓库
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo # 拉取阿里源的配置文件
yum makecache # 生成缓存

扩展

1
2
3
4
5
6
[test] # 配置仓库名
name=mytest # 配置仓库描述
baseurl=https://mysource... # 配置仓库网址
enabled=1 # 1启用该仓库,0不启用
gpgcheck=0 # 是否开启密钥检查软件包
gpgkey=... # 检查密钥的文件

配置epel扩展仓库

1
2
3
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup # 备份
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup # 备份
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo # 安装

安装sl cowsay figlet 小玩具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yum install -y sl cowsay

sl # 火车

cowsay "hello" # 奶牛说hello

animalsay 'hello' # 随机动物说hello

echo test |figlet # 字体
_ _
| |_ ___ ___| |_
| __/ _ \/ __| __|
| || __/\__ \ |_
\__\___||___/\__|