07-系统重要命令

07-系统重要命令
less
less		#一页一页的查看文件
	-N 	#显示行号
	/内容 #搜索内容 n往下查找N往上查找
	q	#退出less命令
	G	#快速到文件尾
	g	#快速到问头
	100g	#快速到100行
	200G	#快速到200行

空格/f #往下翻一页 *重点
b	#往上翻一页	*重点

more
more 	#一页一页看文件,到文尾自动退出。
	q	#退出命令
wc
wc 	#做统计 word count
	wc 文件	#统计文件中的行数 字数 字节数---不常用
	wc -l 	#统计文件的行数
	wc -L 	#统计最长行的字符串长度
    命令|wc -l #接收其他命令的结果
语法结构:
	wc -l 文件名称 				 #统计文件行数
	cat file |wc -l 			#统计文件行数
	ip add |wc -l				#统计文件行数
	wc -m 文件名				#统计文中有多少个字--基本不用

#cat file |wc -l
[root@oldboyedu ~]# cat passwd |wc -l
20
#ip add |wc -l
[root@oldboyedu ~]# ip add |wc -l
12

案例1.统计文件总行数
[root@oldboyedu ~]# cat /var/log/messages |wc -l
15341

案例2.统计文中字符串最长的行中的字符数
[root@oldboyedu ~]# cat 2.txt |wc -L
7
案例3.统计文中有多少个字
[root@oldboyedu ~]# wc -m /etc/services 
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 ~]# cat test1.txt |sort |uniq
dage
hello
lidao
lizhen
oldboy
test

案例2.uniq去重统计
[root@oldboyedu ~]# cat test1.txt |uniq -c |sort
      1 dage
      1 lizhen
      2 hello
      2 oldboy
      2 test
      3 lidao

案例3.uniq去重统计并且按照数字进行排序
[root@oldboyedu ~]# cat test1.txt |uniq -c |sort -nr
      3 lidao
      2 test
      2 oldboy
      2 hello
      1 lizhen
      1 dage

案例4.uniq去重统计并且按照数字进行排序取前三
[root@oldboyedu ~]# cat test1.txt |uniq -c |sort -rn|head -3
      3 lidao
      2 test
      2 oldboy

案例5.统计passwd文件中单词出现最多的前3
[root@oldboyedu ~]# cat passwd |sed 's#[:/0-9x]# #g'|xargs -n1 |sort |uniq -c |sort -nr |head -3
     22 sbin
     16 nologin
      6 var
      
sort
sort :排序
#默认按照第一列正序排序
参数:
	sort 文件名		#默认按照第一列的第一个字母正序排序。文字和数据都是
	sort -r 文件名		#逆序排序
	sort -n 文件名		#针对数字 按照数字正序排序
	sort -rn 文件名	#按照数字逆序排序
	sort -k 文件名		#指定列进行排序
    sort -k2 -n 文件名	#按照第二列的数字正序排列
    
	
[root@oldboyedu ~]# sort 1.txt 
1
10
2
3
4
5
6
7
8
9

案例2.字符串也是按照第一列正序排序
[root@oldboyedu ~]# sort 3.txt 
a
abc
b
bfg
c
ccs
ddd
etx
f

案例3.按照数字正序排序 sort -n 
[root@oldboyedu ~]# sort -n 1.txt 
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 ~]# cat test1.txt 
hello
lidao
test
lizhenya
zhengtao
zhangtao
oldboy

[root@oldboyedu ~]# 
[root@oldboyedu ~]# sort test1.txt 

hello
lidao
lizhenya
oldboy
test
zhangtao
zhengtao

案例6.按照单词逆序排序---没意义
[root@oldboyedu ~]# sort -r test1.txt 
test
oldboy
lizhenya
lidao
hello
dage

案例7.按照第二列的数字正序排列
[root@oldboyedu ~]# sort  -k2 -n test1.txt 
test	1
dage	5
hello	11
oldboy	20
lidao	40
lizhen	100
[root@oldboyedu ~]# sort -k2n test1.txt 
test	1
dage	5
hello	11
oldboy	20
lidao	40
lizhen	100
案例8.按照第二列的数字倒序排列
[root@oldboyedu ~]# sort -nrk2 test1.txt 
lizhen	100
lidao	40
oldboy	20
hello	11
dage	5
test	1

使用xargs -n 按照n列输出内容
使用xargs按照n列输出内容,不会改变源文件
[root@oldboyedu ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu ~]# echo {1..10} |xargs -n1
1
2
3
4
5
6
7
8
9
10
[root@oldboyedu ~]# echo {1..10} |xargs -n2
1 2
3 4
5 6
7 8
9 10
系统优化相关
查看系统版本
方法1.
cat /etc/redhat-release
[root@oldboyedu ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
方法2.
hostnamectl
[root@oldboyedu ~]# hostnamectl
   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 ~]# uname -a
Linux oldboyedu 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

uname -r	#显示内核版本
[root@oldboyedu ~]# uname -r
3.10.0-1160.el7.x86_64
系统时间同步
系统时间:操作系统时间,可以在操作系统上看到的时间
硬件时间:主板时间,BIOS时间
#两种时间必须保持一致,否则会出现问题
1.查看系统时间:
date
	date +%F-%H-%M-%S
[root@oldboyedu ~]# date +%F-%H-%M-%S
2024-03-08-15-48-59

2.查看硬件时间
clock
[root@oldboyedu ~]# clock
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 ~]# ntpdate ntp1.aliyun.com
 8 Mar 15:50:18 ntpdate[4873]: step time server 120.25.115.20 offset -1.186939 sec

保存到硬件时间
hwclock -w

练习时间同步:
[root@oldboyedu ~]# date
Tue Feb 18 10:23:53 CST 2020
[root@oldboyedu ~]# 
[root@oldboyedu ~]# 
[root@oldboyedu ~]# 
[root@oldboyedu ~]# ntpdate ntp1.aliyun.com
 8 Mar 15:57:21 ntpdate[4881]: step time server 120.25.115.20 offset 127891980.802183 sec
[root@oldboyedu ~]# date
Fri Mar  8 15:57:23 CST 2024
[root@oldboyedu ~]# hwclock -w

关闭selinux
作用:限制root管理员 工作中禁止
selinux 默认自动开启,开机自动开启
查看selinux
getenforce
[root@oldboyedu ~]# getenforce
Enforcing
2.临时设置selinux关闭
setenforce
[root@oldboyedu ~]# setenforce 
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@oldboyedu ~]# setenforce 0
查看临时关闭是否成功
[root@oldboyedu ~]# getenforce 
Permissive
3.永久关闭selinux,设置开机禁止启动
vim /etc/selinux/config
设置SELINUX=disabled
保存退出。重启系统

方法二:
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

1

关闭防火墙
作用:流量限制,流量分析。类似与地铁的安检
1.查看防火墙状态
systemctl status firewalld

[root@oldboyedu ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled(#开机自动运行); vendor preset: enabled)
   Active: active (running) #运行中# since Thu 2024-03-07 21:12:26 CST; 14h ago
     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 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead)#停止 since Fri 2024-03-08 11:38:51 CST; 12s ago
     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 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@oldboyedu ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; #disabled; vendor preset: enabled)
   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 ~]# yum repolist
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 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@oldboyedu ~]# ll /etc/yum.repos.d/
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 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
  % 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 ~]# ll /etc/yum.repos.d/
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 ~]# yum repolist
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 ~]# yum install wget -y

2

配置epel源 扩展仓库
测试安装:sl cowsay
配置epel扩展仓库:
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
检查是否安装成功:
[root@oldboyedu ~]# yum repolist
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 ~]# yum install sl cowsay -y

3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

atomLg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值