07-系统重要命令
less
less
-N
/内容
q
G
g
100g
200G
空格/f
b
more
more
q
wc
wc
wc 文件
wc -l
wc -L
命令|wc -l
语法结构:
wc -l 文件名称
cat file |wc -l
ip add |wc -l
wc -m 文件名
[root@oldboyedu ~]
20
[root@oldboyedu ~]
12
案例1.统计文件总行数
[root@oldboyedu ~]
15341
案例2.统计文中字符串最长的行中的字符数
[root@oldboyedu ~]
7
案例3.统计文中有多少个字
[root@oldboyedu ~]
670281 /etc/services
1字节(Byte) = 8位(bit)
1KB( KB,千字节) = 1024B
1MB( MB,兆字节) = 1024K
1GB( GB,吉字节,千兆) = 1024MB
1TB( TB,万亿字节,太字节) = 1024GB
1PB( PB,千万亿字节,拍字节) = 1024TB
1EB( EB,百亿亿字节,艾字节) = 1024PB
1ZB(ZB,十万亿亿字节,泽字节) = 1024EB
1YB( YB,一亿亿亿字节,尧字节) = 1024ZB
1BB( BB,千亿亿亿字节) = 1024YB
uniq
uniq 去重--把相邻挨着的相同内容进行去重
-c
案例1.uniq去重
[root@oldboyedu ~]
dage
hello
lidao
lizhen
oldboy
test
案例2.uniq去重统计
[root@oldboyedu ~]
1 dage
1 lizhen
2 hello
2 oldboy
2 test
3 lidao
案例3.uniq去重统计并且按照数字进行排序
[root@oldboyedu ~]
3 lidao
2 test
2 oldboy
2 hello
1 lizhen
1 dage
案例4.uniq去重统计并且按照数字进行排序取前三
[root@oldboyedu ~]
3 lidao
2 test
2 oldboy
案例5.统计passwd文件中单词出现最多的前3
[root@oldboyedu ~]
22 sbin
16 nologin
6 var
sort
sort :排序
参数:
sort 文件名
sort -r 文件名
sort -n 文件名
sort -rn 文件名
sort -k 文件名
sort -k2 -n 文件名
[root@oldboyedu ~]
1
10
2
3
4
5
6
7
8
9
案例2.字符串也是按照第一列正序排序
[root@oldboyedu ~]
a
abc
b
bfg
c
ccs
ddd
etx
f
案例3.按照数字正序排序 sort -n
[root@oldboyedu ~]
1
2
3
4
6
7
9
10
34
44
55
80
案例4.按照数字逆序排序 sort -nr
80
55
44
34
10
9
7
6
4
3
2
1
案例5.按照单词正序排序
[root@oldboyedu ~]
hello
lidao
test
lizhenya
zhengtao
zhangtao
oldboy
[root@oldboyedu ~]
[root@oldboyedu ~]
hello
lidao
lizhenya
oldboy
test
zhangtao
zhengtao
案例6.按照单词逆序排序---没意义
[root@oldboyedu ~]
test
oldboy
lizhenya
lidao
hello
dage
案例7.按照第二列的数字正序排列
[root@oldboyedu ~]
test 1
dage 5
hello 11
oldboy 20
lidao 40
lizhen 100
[root@oldboyedu ~]
test 1
dage 5
hello 11
oldboy 20
lidao 40
lizhen 100
案例8.按照第二列的数字倒序排列
[root@oldboyedu ~]
lizhen 100
lidao 40
oldboy 20
hello 11
dage 5
test 1
使用xargs -n 按照n列输出内容
使用xargs按照n列输出内容,不会改变源文件
[root@oldboyedu ~]
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu ~]
1
2
3
4
5
6
7
8
9
10
[root@oldboyedu ~]
1 2
3 4
5 6
7 8
9 10
系统优化相关
查看系统版本
方法1.
cat /etc/redhat-release
[root@oldboyedu ~]
CentOS Linux release 7.9.2009 (Core)
方法2.
hostnamectl
[root@oldboyedu ~]
Static hostname: oldboyedu
Icon name: computer-vm
Chassis: vm
Machine ID: 006800701d2a492db75fee7b3ef45076
Boot ID: af5206b0dcaf488ea68f96b5ab87c212
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.el7.x86_64
Architecture: x86-64
查看内核版本
centos5.x操作系统
centos6.x操作系统 2.6.32
centos7.x操作系统 3.10.0
uname -a
[root@oldboyedu ~]
Linux oldboyedu 3.10.0-1160.el7.x86_64
uname -r
[root@oldboyedu ~]
3.10.0-1160.el7.x86_64
系统时间同步
系统时间:操作系统时间,可以在操作系统上看到的时间
硬件时间:主板时间,BIOS时间
1.查看系统时间:
date
date +%F-%H-%M-%S
[root@oldboyedu ~]
2024-03-08-15-48-59
2.查看硬件时间
clock
[root@oldboyedu ~]
Fri 08 Mar 2024 03:47:21 PM CST -0.461240 seconds
3.同步系统时间
ntpdate
安装ntpdate
yum install ntpdate -y
同步阿里云时间
ntpdate ntp1.aliyun.com
[root@oldboyedu ~]
8 Mar 15:50:18 ntpdate[4873]: step time server 120.25.115.20 offset -1.186939 sec
保存到硬件时间
hwclock -w
练习时间同步:
[root@oldboyedu ~]
Tue Feb 18 10:23:53 CST 2020
[root@oldboyedu ~]
[root@oldboyedu ~]
[root@oldboyedu ~]
[root@oldboyedu ~]
8 Mar 15:57:21 ntpdate[4881]: step time server 120.25.115.20 offset 127891980.802183 sec
[root@oldboyedu ~]
Fri Mar 8 15:57:23 CST 2024
[root@oldboyedu ~]
关闭selinux
作用:限制root管理员 工作中禁止
selinux 默认自动开启,开机自动开启
查看selinux
getenforce
[root@oldboyedu ~]
Enforcing
2.临时设置selinux关闭
setenforce
[root@oldboyedu ~]
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@oldboyedu ~]
查看临时关闭是否成功
[root@oldboyedu ~]
Permissive
3.永久关闭selinux,设置开机禁止启动
vim /etc/selinux/config
设置SELINUX=disabled
保存退出。重启系统
方法二:
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
关闭防火墙
作用:流量限制,流量分析。类似与地铁的安检
1.查看防火墙状态
systemctl status firewalld
[root@oldboyedu ~]
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled(
Active: active (running)
Docs: man:firewalld(1)
Main PID: 872 (firewalld)
CGroup: /system.slice/firewalld.service
└─872 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
2.临时关闭防火墙
systemctl stop firewalld
[root@oldboyedu ~]
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
Process: 872 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 872 (code=exited, status=0/SUCCESS)
3.永久关闭防火墙-禁止开机启动
systemctl disable firewalld
[root@oldboyedu ~]
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@oldboyedu ~]
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service;
Active: inactive (dead)
Docs: man:firewalld(1)
systemctl 用法:
systemctl 动作 服务名称
服务名称:NetworkManager
network
firewalld
动作:
systemctl stop 服务
systemctl status 服务
systemctl start 服务
systemctl restart 服务
systemctl enable 服务
systemctl disable 服务
什么情况下开启防火墙?
1.在公网需要开启防火墙
2.有用户可以直接访问的服务器 NAT转换
3.云服务器
什么情况下关闭防火墙?
1.在局域网中不需要开启防火墙
2.没有公网IP地址
3.内部测试服务器
4.高并发服务器需要关闭firewalld。安装部署硬件防火墙
YUM仓库
作用:安装软件连接的地址
yum仓库类似应用商店
Linux操作系统默认给我们的随机的yum仓库地址
1.查看系统默认随机的yum源
[root@oldboyedu ~]
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10,072
extras/7/x86_64 CentOS-7 - Extras 519
updates/7/x86_64 CentOS-7 - Updates 5,766
repolist: 16,357
2.修改默认的仓库为阿里云
opsx.alibaba.com
配置页面地址:https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b11Hr6Sde
修改流程:
第一步:备份默认的仓库
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@oldboyedu ~]
[root@oldboyedu ~]
total 40
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1309 Oct 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Oct 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Oct 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo
第二步:
下载阿里云仓库到本地
[root@oldboyedu ~]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2523 100 2523 0 0 9135 0 --:--:-- --:--:-- --:--:-- 9141
第三步:
检查是否下载到本地;
[root@oldboyedu ~]
total 44
-rw-r--r--. 1 root root 2523 Mar 8 16:36 CentOS-Base.repo
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1309 Oct 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root 649 Oct 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 Oct 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Oct 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Oct 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 Oct 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root 616 Oct 23 2020 CentOS-x86_64-kernel.repo
[root@oldboyedu ~]
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 519
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 5,766
repolist: 16,357
安装wget:
[root@oldboyedu ~]
配置epel源 扩展仓库
测试安装:sl cowsay
配置epel扩展仓库:
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
检查是否安装成功:
[root@oldboyedu ~]
Loaded plugins: fastestmirror
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,786
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 519
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 5,766
repolist: 30,143
再次安装sl cowsay
[root@oldboyedu ~]