Linux运维
文章目录
前言
这次简单介绍下Linux命令(补充)以及Linux系统的优化
一、系统命令
只要屏幕上可以显示的内容,都可以使用> >> 存放到文件中
1、less
1.less #一页一页的查看文件内容,查看大文件比较好
-N #显示行号
空格/f # 往下翻一页
b # 往上翻一页
/ # 搜索内容
n # 往下查找
N # 往上查找
q # 退出less命令
G # 快速到文件的尾部
200G # 快速到200行
g # 快速到文件的首行
100g # 快速到100行
2.命令参数:
-b <缓冲区大小> 设置缓冲区的大小
-e 当文件显示结束后,自动离开
-f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件
-g 只标志最后搜索的关键词
-i 忽略搜索时的大小写
-m 显示类似more命令的百分比
<strong>-N 显示每行的行号</strong>
-o <文件名> 将less 输出的内容在指定文件中保存起来
-Q 不使用警告音
-s 显示连续空行为一行
-S 行过长时间将超出部分舍弃
-x <数字> 将“tab”键显示为规定的数字空格
<strong>/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能</strong>
n:重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
<strong>b 向后翻一页
d 向后翻半页</strong>
h 显示帮助界面
Q 退出less 命令
u 向前滚动半页
y 向前滚动一行
<strong>空格键 滚动一页
回车键 滚动一行</strong>
2、more
more 命令类似 cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示,而且还有搜寻字串的功能(与 vi 相似),使用中的说明文件,请按 h 。
1、常用参数列表
-num 一次显示的行数
-d 在每屏的底部显示友好的提示信息
-l 忽略 Ctrl+l (换页符)。如果没有给出这个选项,则more命令在显示了一个包含有 Ctrl+l 字符的行后将暂停显示,并等待接收命令。
-f 计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)
-p 显示下一屏之前先清屏。
-c 从顶部清屏然后显示。
-s 文件中连续的空白行压缩成一个空白行显示。
-u 不显示下划线
+/ 先搜索字符串,然后从字符串之后显示
+num 从第num行开始显示
2、常用操作命令
Enter 向下n行,需要定义。默认为1行
Ctrl+F 向下滚动一屏
空格键 向下滚动一屏
Ctrl+B 返回上一屏
= 输出当前行的行号
:f 输出文件名和当前行的行号
v 调用vi编辑器
!命令 调用Shell,并执行命令
q 退出more
3、wc
利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。
wc [-clw][--help][--version][文件...]
1.参数:
-c或--bytes或--chars 只显示Bytes数。
-l或--lines 显示行数。
-w或--words 只显示字数。
--help 在线帮助。
--version 显示版本信息。
-L #统计文件中最长行的字母的个数 统计长度
4、sort
sort #排序
-r #逆序排序
-n #按数字进行正序排序
-k #指定列进行排序
案例1.按数字排序 默认按照第一列进行正序排序
[root@study01 ~]# sort 2.txt
1
10
2
3
4
5
6
7
8
9
[root@study01 ~]# sort 3.txt
a
abc
b
bad
c
cde
d
f
g
案例3.按照数字进行正序排序
[root@study01 ~]# sort -n 2.txt
1
2
3
4
5
6
7
8
9
10
案例4.按照数字进行倒序排序
[root@study01 ~]# sort -nr 2.txt
10
9
8
7
6
5
4
3
2
1
5、uniq
1.参数:
-c或--count 在每列旁边显示该行重复出现的次数。
-d或--repeated 仅显示重复出现的行列。
-f<栏位>或--skip-fields=<栏位> 忽略比较指定的栏位。
-s<字符位置>或--skip-chars=<字符位置> 忽略比较指定的字符。
-u或--unique 仅显示出一次的行列。
-w<字符位置>或--check-chars=<字符位置> 指定要比较的字符。
--help 显示帮助。
--version 显示版本信息。
[输入文件] 指定已排序好的文本文件。如果不指定此项,则从标准读取数据;
[输出文件] 指定输出的文件。如果不指定此选项,则将内容显示到标准输出设备(显示终端)。
去重(把挨着的相同的内容进行去重)
testfile中的原有内容为:
[root@study01 ~]# cat testfile #原有内容
test 30
test 30
test 30
Hello 95
Hello 95
Hello 95
Hello 95
Linux 85
Linux 85
使用uniq 命令删除重复的行后,有如下输出结果:
[root@study01 ~]# uniq testfile #删除重复行后的内容
test 30
Hello 95
Linux 85
检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数。使用如下命令:
uniq -c testfile
结果输出如下:
[root@study01 ~]# uniq -c testfile #删除重复行后的内容
3 test 30 #前面的数字的意义为该行共出现了3次
4 Hello 95 #前面的数字的意义为该行共出现了4次
2 Linux 85 #前面的数字的意义为该行共出现了2次 当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用:
[root@study01 ~]# cat testfile1 # 原有内容
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
这时我们就可以使用 sort:
[root@study01 ~]# sort testfile1 | uniq
Hello 95
Linux 85
test 30
统计各行在文件中出现的次数:
[root@study01 ~]# sort testfile1 | uniq -c
3 Hello 95
3 Linux 85
3 test 30
在文件中找出重复的行:
[root@study01 ~]# sort testfile1 | uniq -d
Hello 95
Linux 85
test 30
二、系统优化
- 1.查看系统的版本
- 2.查看系统的内核版本
- 3.系统时间同步
- 4.关闭Selinux
- 5.关闭firewalld、NetworkManager
- 6.修改默认的YUM仓库
- 7.字符集优化
- 8.Shell远程连接优化
- 9.安装常用的软件命令 自动补全命令
1、查看系统的版本号
方法一:
[root@study01 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
方法二:
[root@study01 ~]# hostnamectl
Static hostname: study01
Icon name: computer-vm
Chassis: vm
Machine ID: 9c67d90f3cb841199ccd4a896ae4ebf1
Boot ID: 6bb984d074874068ad39a4823105af41
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
2、查看系统内核版本
centos5.x操作系统
centos6.x操作系统 2.6.32
centos7.6 7.9操作系统 3.10.0
方法1: uname -a 显示所有内核信息
[root@study01 ~]# uname -a
Linux study01 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
方法2: uname -r 显示内核版本号
[root@study01 ~]# uname -r
3.10.0-1160.el7.x86_64
3、系统时间同步
系统时间: 两种时间必须一致
第一种: 硬件时间 bios时间
第二种: 系统时间 操作系统可以看到的时间
1.查看系统时间
[root@study01 ~]# date
Sat Mar 9 09:12:41 CST 2024
[root@study01 ~]# date +%F-%H
2024-03-09-09
[root@study01 ~]# date +%F-%H-%M
2024-03-09-09-13
[root@study01 ~]# date +%F-%H-%M-%S
2024-03-09-09-13-49
[root@study01 ~]# date -s 20080808
Fri Aug 8 00:00:00 CST 2008
[root@study01 ~]# date
Fri Aug 8 00:00:04 CST 2008
2.查看硬件时间
[root@study01 ~]# clock
Sat 09 Mar 2024 09:15:12 AM CST -0.318672 seconds
3.同步系统时间 ntpdate
[root@study01 ~]# ntpdate ntp1.aliyun.com
9 Mar 09:31:36 ntpdate[3722]: step time server 120.25.115.20 offset 491822065.265684 sec
第三步: 检查系统时间
[root@study01 ~]# date
Sat Mar 9 09:31:42 CST 2024
同步硬件时间: 将系统时间同步给硬件时间
4、关闭Selinux
selinux配置文件位置: /etc/selinux/config
作用: 限制root管理员 工作中禁止掉
默认开启的,开机自动运行
01.查看Selinux
[root@study01 ~]# getenforce
Enforcing
02.临时在操作系统退出关闭selinux 重启后selinux自动运行
# 关闭selinux
[root@study01 ~]# setenforce 0
# 查看是否成功
[root@study01 ~]# getenforce
Permissive
永久关闭selinux,开机禁止启动
[root@study01 ~]# vim /etc/selinux/config
[root@study01 ~]# cat -n /etc/selinux/config
1
2 # This file controls the state of SELinux on the system.
3 # SELINUX= can take one of these three values:
4 # enforcing - SELinux security policy is enforced.
5 # permissive - SELinux prints warnings instead of enforcing.
6 # disabled - No SELinux policy is loaded.
7 #SELINUX=enforcing
8 SELINUX=disabled # 修改为disable
9 # SELINUXTYPE= can take one of three values:
10 # targeted - Targeted processes are protected,
11 # minimum - Modification of targeted policy. Only selected processes are protected.
12 # mls - Multi Level Security protection.
13 SELINUXTYPE=targeted
5、关闭防火墙和NetworkManager
作用: 流量限制,流量分析 攻击行为(类似地铁安检人员)
1.查看防火墙状态
[root@study01 ~]# 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 Fri 2024-03-08 08:57:14 CST; 2h 39min ago
Docs: man:firewalld(1)
Main PID: 1046 (firewalld)
CGroup: /system.slice/firewalld.service
└─1046 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
2.临时关闭防火墙
[root@study01 ~]# systemctl stop firewalld
3.禁止开机自动运行
[root@study01 ~]# systemctl disable firewalld
systemctl用法:
systemctl 动作 服务名称
服务: NetworkManager
network
firewalld
动作:
systemctl stop 服务 # 停止服务
systemctl start 服务 # 启动服务
systemctl restart 服务 # 重启服务
systemctl enable 服务 # 开机自动启动此服务
systemctl disable 服务 # 开机禁止启动此服务
systemctl status 服务 # 查看服务的状态
systemctl reload 服务 # 重新加载服务
什么情况下需要开启防火墙?
1.在公网需要开启
2.用户可以直接访问的服务的服务器 NAT转换 DMZ映射
3.有公网的云服务器
什么情况下需要关闭防火墙?
1.在局域网中不需要开启 教室内 公司内
2.没有公网IP地址
3.内部测试服务器
4.高并发场景需要关闭firewalld 导致服务速度慢 解决方法 部署硬件防火墙
高并发:
很多人同一时间访问我们的服务器
双11 上亿人访问某东 某宝
6、YUM仓库优化
作用: 安装软件的链接的地址,安装软件需要去YUM仓库下载
yum仓库类似 华为应用商店
Linux操作系统默认给我们随机的YUM仓库地址
YUM仓库|YUN源
官方仓库(美国)
阿里云
腾讯云
清华
北大
北邮
华为
亚马逊
...
[root@study01 ~]# ll /etc/yum.repos.d/ # 此目录下存放了yum仓库的地址 不能删除
total 40
-rw-r--r--. 1 root root 1664 Oct 23 2020 CentOS-Base.repo
-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
1.查看系统随机默认的YUM仓库
[root@study01 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn # 默认的仓库地址
* extras: mirrors.bupt.edu.cn
* updates: mirrors.bupt.edu.cn
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.3e221b11FrNVxf
修改流程:
第一步: 备份默认的仓库地址
[root@study01 ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
第二步: 下载阿里云仓库到本地
[root@study01 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
第三步: 检查配置是否正确
[root@study01 ~]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com # 修改为aliyun.com成功
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
注意: 显示以下内容说明你的仓库没有了
[root@study01 ~]# yum -y install lrzsz
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
There are no enabled repos.
Run "yum repolist all" to see the repos you have.
To enable Red Hat Subscription Management repositories:
subscription-manager repos --enable <repo>
To enable custom repositories:
yum-config-manager --enable <repo>
`配置扩展的仓库`
```sh
第一步: 安装wget命令
[root@study01~]# yum -y install wget # wget默认在base源中
在测试安装两个命令: sl cowsay
第二步: 配置epel扩展仓库:
[root@study01~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
第三步: 检查是否配置成功:
[root@study01 ~]# 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
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 # 是否增加至3万+
第四步: 在尝试安装sl cowsay命令
[root@study01 ~]# yum -y install sl cowsay
[root@study01 ~]# sl # 直接回车
[root@study01 ~]# animalsay "呵呵哒"
_____
< 呵呵哒 >
-----
\ /\ ___ /\
\ // \/ \/ \\
(( O O ))
\\ / \ //
\/ | | \/
| | | |
| | | |
| o |
| | | |
|m| |m|
7、字符集优化
作用: 可以让中文在Linux系统中正常显示
常用的字符集两种:
第一种: UTF-8 默认的字符集
第二种: GBK 国标
系统默认的字符集必须和远程连接工具xshell(CRT)一致
查看系统的字符集:
[root@study01 ~]# echo $LANG
en_US.UTF-8
语言.编码
修改语言为中文: 临时修改
[root@study01 ~]# LANG='zh_CN.UTF-8'
永久修改: 需要修改配置
[root@study01 ~]# cat /etc/locale.conf
LANG="en_US.UTF-8"
8、Shell远程连接优化
SSH服务: 能够让我们使用远程连接工具远程连接服务器,默认开启
优化远程连接的速度
/etc/ssh/sshd_config # 配置文件的位置
第一步: 修改配置文件
[root@study01 ~]# grep UseDNS /etc/ssh/sshd_config
UseDNS no # 在115行 将默认yes 修改为no
第二步: 重启SSH服务
[root@study01 ~]# systemctl restart sshd
正向解析: 将域名解析成IP地址 www.baidu.com-->ip地址 常用
正常来讲我们连接服务器 知道IP地址,直接连接即可
www.linuxnc.com 直接可以使用域名远程连接
反向解析: 将IP地址解析成域名 10.0.0.200-->对应的域名? 不用
[root@study01 ~]# curl cip.cc # 可以查看公网IP地址的信息
IP : 221.218.209.249
地址 : 中国 北京
运营商 : 联通
数据二 : 北京市海淀区 | 联通
数据三 : 中国北京北京市 | 联通
URL : http://www.cip.cc/221.218.209.249
9、安装常用的软件命令 自动补全命令
- lrzsz Linux/Unix同Windows进行ZModem文件传输的命令行工具。
- wget 下载文件的工具
- tree 用于以树状图列出目录的内容
- net-tools 网络工具包
- ntpdate 时间同步
自动补全命令
- bash-completion.noarch
- bash-completion-extras.noarch