基础命令操作
系统命令
head
1 2 3 4
| head test.txt
head -n 20 test.txt head -20 test.txt
|
tail
1 2 3 4 5 6 7 8 9 10
| tail test.txt
tail -n 20 test.txt tail -20 test.txt
tail -f test.txt
tailf test.txt
tail -F 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
wc -L test.txt
wc -c test.txt
wc -w test.txt
cat file | wc -l
|
- -l:line行数
- -c:char字符数
- -w:word单词数
sort
按字符串排序(排序从左到右依次按字符大小比较排序)
按数字大小排序
逆序排序
指定列排序
uniq
uniq去重(uniq只能去重相邻相同的字符,也就是需要sort才能生效)
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
| xargs -n1 test.txt
|
行转换成n列
系统基本配置
查看系统版本号
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小时,违和感不大
查看系统时间
查看硬件时间
同步系统时间
将系统时间同步到硬件时间
selinux配置
selinux是国外的,但凡涉及安全之类的,美国国家安全局还是有插手的,有没有开后门什么的就不知道了,一般企业中不开启selinux,如果开启的话需要对标签、规则需要很熟悉,还是比较麻烦的,管理selinux上下文需要要安装policycoreutils和policycoreutils-python-utils。
1 2 3 4 5 6 7 8 9
| getenforce 开启,阻止 setenforce 1 setenforce 0
vim /etc/selinux/config
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
firewall-cmd --permanent --add-port=21/tcp firewall-cmd --reload
firewall-cmd --permanent --remove-port=21/tcp firewall-cmd --reload
firewall-cmd --zone=block --add-source=192.168.12.11/32 --permanent
firewall-cmd --permanent --zone=public --add-rich-rule=“rule family=‘ipv4’ source address=’192.168.12.11‘ drop“
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 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"
animalsay 'hello'
echo test |figlet _ _ | |_ ___ ___| |_ | __/ _ \/ __| __| | || __/\__ \ |_ \__\___||___/\__|
|