首页
关于
推荐
CSDN
Search
1
文件上传下载-io-流的理解-笔记
158 阅读
2
vue循环指令el-table-column展示图片src路径拼接
150 阅读
3
正则表达式,将字符串分割两部分
143 阅读
4
MySQL数据库练习【一】
135 阅读
5
MySQL数据库练习【三】
123 阅读
默认分类
Mysql
Java基础
一天一练
Mongodb
Nginx
Docker
FastDFS
面试题
云计算基础
linux基础
shell脚本
实验
工具
基础命令
redis
zookeeper
部署
案例
登录
Search
标签搜索
vue
Mysql
IO
面试题
良辰美景好时光
累计撰写
72
篇文章
累计收到
0
条评论
首页
栏目
默认分类
Mysql
Java基础
一天一练
Mongodb
Nginx
Docker
FastDFS
面试题
云计算基础
linux基础
shell脚本
实验
工具
基础命令
redis
zookeeper
部署
案例
页面
关于
推荐
CSDN
搜索到
72
篇与
的结果
2024-07-21
find 按文件修改时间查找文件及find空文件夹
查找两天前修改过的文件:find . -type f -mtime -2查找3天内修改过的文件:find -ctime -3find命令使用超过6天,空文件独立查询命令:find /data/backup -ctime +6 -exec rm -f {} \; 删除/data/backup目录下修改时间超过6天的文件。 find /data/backup -type d -empty -exec rmdir {} \; >/dev/null 2>&1 删除/data/backup目录下空的文件夹,同时输出正确和错误信息到空。查找超过6天且是空文件的find命令:find ./ -type d -empty -ctime +6按修改时间来查找文件,要用到选项-mtime:find /home/admin -mtime -1 #查找/home/admin目录下修改时间在1天之内的文件find /home/admin -name *.txt -mtime -1 #查找/home/admin目录下修改时间在1天之内的文件名为.txt结尾的文件find 按文件修改时间查找文件---(+n)----------|----------(n)----------|----------(-n)--- (n+1)*24H前| (n+1)*24H~n*24H间 |n*24H内 -ctime -n 查找距现在 n*24H 内修改过的文件-ctime n 查找距现在 n24H 前, (n+1)24H 内修改过的文件-ctime +n 查找距现在 (n+1)*24H 前修改过的文件[a|c|m]min [最后访问|最后状态修改|最后内容修改]min[a|c|m]time [最后访问|最后状态修改|最后内容修改]timelinux 文件的三种时间(以 find 为例):atime 最后一次访问时间, 如 ls, more 等, 但 chmod, chown, ls, stat 等不会修改些时间, 使用 ls -utl 可以按此时间顺序查看;ctime 最后一次状态修改时间, 如 chmod, chown 等状态时间改变但修改时间不会改变, 使用 stat file 可以查看;mtime 最后一次内容修改时间, 如 vi 保存后等, 修改时间发生改变的话, atime 和 ctime 也相应跟着发生改变.注意:linux 里是不会记录文件的创建时间的,除非这个文件自创建以来没有发生改变,那么它的创建时间就是它的最后一次修改时间。ls -lt /home/admin # 按修改时间顺序查看ls -lut /home/admin # 按访问时间顺序查看(如果想反序查看的话需要加一个选项 -r)知识点:atime ,ctime ,mtime比如,编辑文件内容并保存,文件内容改变会更新 mtime,同时文件系统因写入操作更新 inode 相关信息,也会导致 ctime 更新由于 ctime 记录的是 inode 节点信息的更改时间,而文件内容修改会导致 inode 信息更新,所以 mtime 的更新通常会伴随 ctime 的更新。但 ctime 更新不一定是因为 mtime 变化,修改文件权限等元数据也会更新 ctime,此时 mtime 不变。
2024年07月21日
15 阅读
0 评论
0 点赞
2024-07-18
Linux三剑客【grep、sed、awk】-02
Linux三剑客【grep、sed、awk】-021. 💓grep文本搜索工具语法结构:参数选项参数选项注释说明-w精确匹配整词-v取反-E扩展正则-i忽略大小写-r递归过滤文件内容-l只显示符合匹配条件的文件名-n显示匹配内容的行号-c只显示匹配的行数-o匹配过程 -A过滤内容,往下显示n行-B过滤内容,往上显示n行-C过滤内容,上下各显示n行2. 💓sed:批量编辑文本文件2.1. sed的作用sed是取行、修改、过滤和替换文本内容的强大工具。常用功能有对文件实现快速的增删改查。2.2. sed命令的语法及参数语法格式:参数选项:参数选项注释说明参数选项注释说明-n取消默认sed输出,常与p连用-r支持扩展正则-i直接修改文件内容-e支持多次修改动作参数:动作参数注释说明动作参数注释说明a增加pprint,打印匹配行内容d删除s替换i插入g全局2.2.1. sed增删改查——查找行案例实践:环境准备: [root@linux:~]# cat -n test.txt 1 I am lizhenya teacher! 2 I teach linux. 3 test 4 5 I like badminton ball ,billiard ball and chinese chess! 6 my blog is http: blog.51cto.com 7 our site is http:www.lizhenya.com 8 my qq num is 593528156 9 10 aaa, 11 not 572891888887. 案例1.sed输出第三行 [root@linux:~]# sed -n '3p' test.txt test 案例2.sed输出最后一行 [root@linux:~]# sed -n '$p' test.txt not 572891888887. 案例3.sed输出第3~5行 [root@linux:~]# sed -n '3,5p' test.txt test I like badminton ball ,billiard ball and chinese chess! 案例4.sed输出第10行到最后一行 [root@linux:~]# sed -n '10,$p' test.txt aaa, not 572891888887.2.2.2. sed增删改查——查找字符串作用:过滤文件的字符串语法结构:sed -n '/字符串/p' 文件sed -n '/[字符串]/p' 文件案例实践:环境准备: [root@linux:~]# head -10 /etc/passwd >passwd.txt [root@linux:~]# cat -n passwd.txt 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 案例1.查找passwd.txt中包含root的行 [root@linux:~]# sed -n '/root/p' passwd.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin 案例2.查找passwd.txt中包含bash的行 [root@linux:~]# sed -n '/bash/p' passwd.txt root:x:0:0:root:/root:/bin/bash 案例3.使用正则,查找以r开头的行 [root@linux:~]# sed -n '/^r/p' passwd.txt root:x:0:0:root:/root:/bin/bash 案例4.查找以n结尾的行 [root@linux:~]# sed -n '/n$/p' passwd.txt bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例5.查找包含root或者sync的行 [root@linux:~]# sed -rn '/root|sync/p' passwd.txt root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync operator:x:11:0:operator:/root:/sbin/nologin 案例6.查找包含或a 或b 或 c的行 [root@linux:~]# sed -rn '/a|b|c/p' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例7.查找字符串的区间范围使用逗号,查找adm和sync中间的行 [root@linux:~]# sed -rn '/adm/,/sync/p' passwd.txt adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync匹配区间范围注意:(1)如果有两个结尾只匹配到第一个结尾终止(2)如果只有开头,没有匹配到结尾,则输出开头到文件末尾2.2.3. sed增删改查——删除实践案例:环境准备: [root@linux:~]# cat -n a.txt 1 111111 2 222222 3 333333 4 444444 5 555555 6 666666 案例1.指定行删除 [root@linux:~]# sed '3d' a.txt 111111 222222 444444 555555 666666 案例2.删除3~5行 [root@linux:~]# sed '3,5d' a.txt 111111 222222 6666662.2.4. sed增删改查——增加语法结构:sed '3c test' file #将第三行替换成testsed '3i test ' file #在第三行处插入test字符串sed '3a test' file #在第三行的下面追加test字符串实践案例:环境准备: [root@linux:~]# cat -n passwd.txt 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 案例: 将第3行替换成test [root@linux:~]# sed '3c test' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin test adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例2.在第3行插入test字符串 [root@linux:~]# sed '3i test' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin test daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例3.在第3行处追加test字符串 [root@linux:~]# sed '3a test' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin test adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin2.2.5. sed增删改查——替换语法结构:sed 's#root#xiaozhou #g' filesed 's///g' filesed 's@@@g' file实践案例:环境准备: [root@linux:~]# cat -n passwd.txt 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 案例1.将root全部替换为xiaozhou [root@linux:~]# sed 's#root#xiaozhou#g' passwd.txt xiaozhou:x:0:0:xiaozhou:/xiaozhou:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/xiaozhou:/sbin/nologin 案例2.将文件开头全部替换成# “批量注释” [root@linux:~]# sed 's/^/#/g' passwd.txt #root:x:0:0:root:/root:/bin/bash #bin:x:1:1:bin:/bin:/sbin/nologin #daemon:x:2:2:daemon:/sbin:/sbin/nologin #adm:x:3:4:adm:/var/adm:/sbin/nologin #lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin #sync:x:5:0:sync:/sbin:/bin/sync #shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown #halt:x:7:0:halt:/sbin:/sbin/halt #mail:x:8:12:mail:/var/spool/mail:/sbin/nologin #operator:x:11:0:operator:/root:/sbin/nologin 案例3.只替换每行的第一个root为xiaozhou [root@linux:~]# sed 's/root/xiaozhou/' passwd.txt xiaozhou:x:0:0:root:/root:/bin/bash 案例4.将第三行的a替换为A [root@linux:~]# sed '3s/a/A/g' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin dAemon:x:2:2:dAemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin 案例5.第3-5行小a替换成A [root@linux:~]# sed '3,5s/a/A/g' passwd.txt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin dAemon:x:2:2:dAemon:/sbin:/sbin/nologin Adm:x:3:4:Adm:/vAr/Adm:/sbin/nologin lp:x:4:7:lp:/vAr/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例6.将包含root的行 将bash 替换成test [root@linux:~]# sed '/root/s/bin/test/g' passwd.txt root:x:0:0:root:/root:/test/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/stest/nologin 案例7.只替换bin为test [root@linux:~]# sed 's#\bbin\b#test#g' passwd.txt root:x:0:0:root:/root:/test/bash test:x:1:1:test:/test:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/test/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin 案例8.替换passwd.txt中所有的字符 [root@linux:~]# sed 's#[0-Z]##g' passwd.txt :::::/:// :::::/:// :::::/:// ::::://:// :::::///:// :::::/:// :::::/:// :::::/:// :::::///:// :::::/://一、awk技巧💞1.语法结构取行: awk 'NR==3' file取列: awk '{print $3} ' file****同时取行取列: awk 'NR==3{print $3}' file取出含有某个字符串的行:awk '/字符串/' file区间范围 :awk '/字符串/,/字符串/' file2.使用awk命令获取文本的某一列技巧#打印文件的第一列 awk '{print $1}' filename #打印文件的前两列 awk '{print $1,$2}' filenameNR 存放每行的行号NF 存放最后一列的列号$NF 取最后一列******$(NF-N) 取倒数第几列$0 取所有列的信息(取所有行)3.awk 获取某些列的某些行(打印或不打印第几行)#打印文本的第一行 awk 'NR==1' filename #取test.txt文件中的第1,2列,不打印第一行 awk 'NR!=1 {print $1,$2}' test.txt #取test.txt文件中的第2行的第3列 awk 'NR==2 {print $3}' test.txt #输出小于3的行 awk 'NR<3' passwd.txt #查找大于2并且小于6的行 awk 'NR>2&&NR<6' passwd.txt #查找小于2或者大于5的行 awk 'NR<2||NR>5' passwd.txt NR==n 表示打印第n行NR!=n 表示不打印第n行&& 并且|| 或者4.使用awk命令取某一行数据中的倒数第N列#取/etc/passwd文件中的第2列、倒数第1、倒数第2、倒数第4列(以冒号为分隔符) awk -F":" '{print $2,$NF,$(NF-1),$(NF-2),$(NF-3)}' /etc/passwd参数:-F 指定分隔符5.使用awk命令取包含某个字符串的行#查找包含root的行 awk '/root/' passwd.txt #区间范围 awk '/adm/,/sync/' passwd.txt #找出第一列是oldboy的行 awk '$1=="oldboy"' b.txt 6.awk中的"匹配"与"不匹配"[root@kevin~]# cat test.txt afjdkj 80 lkdjfkja 8080 dfjj 80 jdsalfj 808080 jasj 80 jg 80 linuxidc 80 80 ajfkj asf 80 80 linuxidc wang bo kevin grace ha 80880 #打印上面test文件中第二列匹配80开头并以80结束的行 [root@kevin~]# awk '{if($2~/^80$/)print}' test.txt afjdkj 80 dfjj 80 jasj 80 jg 80 linuxidc 80 asf 80 #打印上面test文件中第二列中不匹配80开头并以80结束的行 [root@kevin~]# awk '{if($2!~/^80$/)print}' test.txt lkdjfkja 8080 jdsalfj 808080 80 ajfkj 80 linuxidc wang bo kevin grace ha 80880 #打印上面test文件中第二列是"bo"的行 [root@kevin~]# cat test.txt |awk '{if($2=="bo")print}' wang bo~ 匹配正则 !~ 不匹配正则 == 等于 != 不等于7.AWK的内置变量(NF、NR、FNR、FS、OFS、RS、ORS)NF 字段个数,(读取的列数) NR 记录数(行号),从1开始,新的文件延续上面的计数,新文件不从1开始 FNR 读取文件的记录数(行号),从1开始,新的文件重新从1开始计数 FS 输入字段分隔符,默认是空格 OFS 输出字段分隔符 默认也是空格 RS 输入行分隔符,默认为换行符 ORS 输出行分隔符,默认为换行符二、sed技巧💞1.语法结构取第3行: sed -n '3p' file 取最后一行: sed -n '$p' file 取3~5行: sed -n '3,5' file 取第3行到最后一行: sed -n '3,$p' file 查找包含某字符串的行: sed -n '/字符串/p' file 查找以a开头的行: sed -n '/^a/' file 查找以a结尾的行: sed -n '/a$/' file 将第三行替换成test: sed '3c test' file 第三行处插入test字符串: sed '3i test ' file 在第三行的下面追加test字符串:sed '3a test' file 将内容替换成XXX: sed 's#内容#替换成#g' file sed '2s#xx#xx#g' sed '//s#xx#xx#g' file2.参数选项-n 取消默认sed输出 (与p连用)-r 支持扩展正则-i 直接修改文件的内容3.动作参数a 增加ci 插入d 删除4.查找第几行#sed输出第三行 sed -n '3p' test.txt #sed输出最后一行 sed -n '$p' test.txt #sed输出第3~5行 sed -n '3,5p' test.txt #sed输出第10行到最后一行 sed -n '10,$p' test.txt 5.查找包含字符串的行#查找passwd.txt中包含root的行 sed -n '/root/p' passwd.txt #查找passwd.txt中包含bash的行 sed -n '/bash/p' passwd.txt #使用正则,查找以r开头的行 sed -n '/^r/p' passwd.txt #查找以n结尾的行 sed -n '/n$/p' passwd.txt #查找包含root或者sync的行 sed -rn '/root|sync/p' passwd.txt #查找包含或a 或b 或 c的行 sed -rn '/a|b|c/p' passwd.txt #查找字符串的区间范围使用逗号,查找adm和sync中间的行 sed -rn '/adm/,/sync/p' passwd.txt 6.删除指定行#删除第3行 sed '3d' a.txt #删除3~5行 sed '3,5d' a.txt7.替换#将root全部替换为xiaozhou sed 's#root#xiaozhou#g' passwd.txt #将文件开头全部替换成# “批量注释” sed 's/^/#/g' passwd.txt #只替换每行的第一个root为xiaozhou (后边不要加g,,只替换每行的第一个) sed 's/root/xiaozhou/' passwd.txt #将第三行的a替换为A sed '3s/a/A/g' passwd.txt #第3-5行小a替换成A sed '3,5s/a/A/g' passwd.txt #将包含root的行 将bash 替换成test sed '/root/s/bin/test/g' passwd.txt #只替换bin为test sed 's#\bbin\b#test#g' passwd.txt #替换passwd.txt中所有的字符 sed 's#[0-Z]##g' passwd.txt /b字符串/b 边界/<字符串/> 边界8.后向引用#使用后向引用取出IP地址 ifconfig ens33|sed -rn '2s#^.*inet (.*) netmask.*#\1#gp' #取出IP地址和子网掩码 ifconfig ens33|sed -rn '2s#^.*inet (.*) netmask (.*) bro.*$#\1\2#gp'三、 grep技巧💞1.语法结构grep 参数 '字符串' file2.参数选项-n 显示匹配内容的行号-r 递归过滤文件-w 匹配整词-v 取反-o 显示匹配过程-E 支持扩展正则 ==egrep四、awk/grep/awk区别对比💫💯💨 grepsedawk参数选项-n 显示匹配内容的行号-r 递归过滤文件-w 匹配整词-v 取反-o 显示匹配过程-E 支持扩展正则egrep-n 取消默认sed输出(与p连用)-r 支持扩展正则-i 直接修改文件的内容-F 指定分隔符取行grep 参数 "字符串" filesed -n '3p' filesed -n '$p' fileawk 'NR==3' fileawk 'NR!=3' file取列 awk '{print $3}' file<br />awk '{print $NF}' fileawk '{print $(NF-1)}' file<br />awk 'NR==5{print $3}' file按字符串查找grep 参数 "字符串" filesed -n '/root/p' filesed -rn '/root/,/adm/p' filesed -rn '/root\adm/p' fileawk '/root/' fileawk '/root/,/adm/' fileawk '/root\adm/' file查找替换 sed '2s#a#A#g' filesed '/root/s#a#A#g' file 后向引用 ifconfig ens33\sed -rn '2s#^.*inet (.*) netmask.*#\1#gp' 其他 动作参数:a 增加c 替换i 插入d 删除NR 存放每行的行号NF 存放最后一列的列号$NF 取最后一列<br />$(NF-N) 取倒数第几列$0 取所有列的信息(取所有行)endl
2024年07月18日
56 阅读
0 评论
1 点赞
2024-07-03
Linux三剑客【grep、sed、awk】-01
Linux三剑客【grep、sed、awk】-01Linux三剑客是什么?linux中的文本处理三剑客分别是grep、awk、sed,它们都以正则表达式作为基础,而在Linux系统中,支持两种正则表达式,分别为"标准正则表达式"和"扩展正则表达式",正则表达式的内容我们后续会讲,首先我们先明确一下三剑客的特点及应用场景,如下表所示:命令特点应用场景grep文本过滤包括从文件中进行过滤和从标准输入进行过滤,其过滤速度最快sed取行文件内容新增、删除、替换、取出某个范围的内容awk取列编写awk脚本对文本进行格式化输出(可进行统计计算)grep:主要用于文本内容查找,支持正则表达式。sed:主要用于文本内容的编辑,默认只处理模式空间,不改变原数据,而且sed使用逐行模式读取的方式处理数据。awk:主要用于文本内容的分析处理,也常用于处理数据,生成报告,非常适用于需要按列处理的数据。grep-搜索指定的内容grep命令用于在文本文件中搜索指定的内容,并返回匹配的行。以下是grep命令的10个用法案例:含义表达式搜索包含指定关键词的行grep "keyword" file.txt忽略大小写的关键词grep -i "keyword" file.txt不显示匹配的行grep -v "keyword" file.txt统计匹配行的数量grep -c "keyword" file.txt显示匹配行之前的内容grep -B 2 "keyword" file.txt显示匹配行之后的内容grep -A 2 "keyword" file.txt显示匹配行及其上下文的内容grep -C 2 "keyword" file.txt把每个匹配的内容用独立的行显示grep -o "keyword" file.txt递归搜索目录 及其子目录下的文件grep -r "keyword" directory使用扩展正则表达式进行高级搜索grep -E "pattern" file.txtgrep示例Shell脚本代码ps -ef | grep bash echo "ABC" | grep -i abc ps -ef | grep bash | grep -v bash echo "1234 7654" | grep -o "[0-9]4" echo "1234 7654" | grep -oE "[0-9]4|76"grep实战演练题目找出nginx.log中所有404和503报错的log数据,取出前3条数据,把命令贴到回复里。找出testerhome首页的所有http和https的链接。sed-流式编辑sed命令用于对文本进行流式编辑,可以进行替换、删除、插入等操作。以下是sed命令的10个用法举例:含义表达式替换文本中的指定字符串sed 's/old/new/' file.txt替换文本中的所有匹配字符串sed 's/old/new/g' file.txt删除匹配指定模式的行sed '/pattern/d' file.txt删除空白行sed '/^$/d' file.txt在匹配行之前插入新行sed '/pattern/i new line' file.txt在匹配行之后插入新行sed '/pattern/a new line' file.txt仅打印匹配的行sed -n '/pattern/p' file.txt仅打印指定行范围内的内容sed -n '2,5p' file.txt将文本中的所有字母转为大写sed 's/[a-z]/\U&/g' file.txt将文本中的所有字母转为小写sed 's/[a-z]/\L&/g' file.txtsed pattern表达式20,30,35 行数与行数范围/pattern/正则匹配//,//正则匹配的区间actiond删除p打印,通常结合-n参数s/REGXP/REPLACEMENT/[FLAGS]替换时引用\1 \2 匹配的字段sed示例Shell脚本代码ps | sed -n 1,3p ps | sed 's/CMD/command/' ps | sed -n '/ps/p' echo '1 2 3 4 5' | sed -n '/3/,/4/p' echo '1 2 3 4 5' | sed '/3/,/4/d' ps | sed -e 's/CMD/command/' -e 's#00#20#g'awk-文本处理awk命令式一种强大的文本处理工具,可以根据指定的规则从文本中提取信息并进行处理。以下是awk命令的10个用法举例:含义表达式打印指定列的内容awk '{print $1}' file.txt根据指定的分隔符切割文本并打印指定列awk -F',' '{print $2}' file.txt根据指定条件筛选行并打印awk '/pattern/{print}' file.txt计算指定列的总和awk '{sum+=$1}END{print sum}' file.txt根据指定条件进行行和列的求和awk '{rowsum+=$1;colsum+=$2}END{print rowsum,colsum}' file.txt根据 指定条件进行行的分组并计数awk '{count[$1]++}END{for (item in count) print item,count[item]}' file.txt根据指定条件进行行的分组并求平均值awk '{sum[$1]+=$2;count[$1]++}END{for (item in sum)print item,sum[item]/count[item]}' file.txt格式化输出awk '{print "%-10s %-5d\n",$1,$2}' file.txt自定义变量 并进行计算awk 'BEGIN{x=5;y=10;print x+y}'执行自定义函数awk 'function myfunc(x) {return x*2}{print myfunc($1)}' file.txtawk pattern语法awk理论上可以代替grepawk 'pattern{action}'awk 'BEGIN{}END{}' 开始和结束awk '/Running/' 正则匹配awk '/aa/,/bb/' 区间选择awk '$2~/xxx/' 字段匹配awk 'NR==2' 取第二行awk 'NR>1' 去掉第一行awk的字段数据处理-F参数指定字段分隔符BEGIN{FS="_"} 也可以表示分隔符$0代表原来的行$1代表第一个字段$N代表第N个字段$NF代表最后一个字段awk行处理把单行分拆为多行echo $PATH | awk 'BEGIN{RS=":"} {print $0}'echo $PATH | awk 'BEGIN{RS=":"} {print NR,$0}'echo $PATH | awk 'BEGIN{RS=":"} {print NR}'多行组合为单行echo $PATH | awk 'BEGIN{RS=":"} {print $0}' | awk 'BEGIN{FS="\n";ORS=":"} {print $0}'awk示例Shell脚本代码ps | awk 'BEGIN{print "start"} {print $0} END {print "end"}' awk '/404 | 500 /' /usr/local/nginx/log/nginx.log echo '1 2 3 4 5' | awk '/2/,/4/' echo '1 2 3 4 5' | awk '$0>3' ps | awk 'NR>1' ps | awk '{print $NF}' echo $PATH | awk 'BEGIN{RS=":"} {print $0}' | grep -v "^$" | awk 'BEGIN{FS="\n";ORS=":"} {print $0} END {printf "\n"}' echo '1,10 2,20 3,30' | awk 'BEGIN{a=0;FS=","} {a+=$2} END {print a,a/R}' awk 'BEGIN{print 33*20*76/200/3}' echo "123|456_789" | awk 'BEGIN{FS="\\||_"} {print $2}' echo "123|456_789" | awk 'BEGIN{FS=\"\\\\||_\"} {print \$2}' #尽量使用单引号awk实战演练题目找出404和503的数据,只打印状态码这一列,然后排序去重,把命令贴到回复里。找出testerhome首页找到所有的http的链接,然后打印不带http的纯域名部分。endl
2024年07月03日
71 阅读
0 评论
0 点赞
2024-06-06
IDC机房和机柜知识
@TOC一、IDC机房和机柜知识【了解】1.1.IDC机房为企业提供存放服务器的空间和提供大量带宽,宽带分为单线带宽和多线(BGP线路)带宽。联通、移动、电信、教育、广电、铁通。不同线路之间通信很慢。单线,1M/30元,只有某一个线路比如,联通,做CDN服务的企业,或者CDN服务器。多路(BGP线路):4线 联通、移动、电信、教育,1M/120-300元,企业多数都会买BGP机房。1.2.机柜和服务器通常机柜都是42U约2米高,一般每个服务器之间都会留有散热的空余空间。1.3.IDC机房服务器上架流程1.3.1.购买服务器服务器配置清单,价格清单(找不低于三家),发给老大选择。老大审核后,申请财务支付。走==钉钉==流程。1.3.2.上架服务器:one: 第一种上架方式:把购买的服务器直接送到托管的IDC机房,由IDC机房的人负责上架(7--8 个)然后由运维人员去装系统,初始化,测试网络连接回公司远程连接,配置各种服务。:two: 第二种上架方式:把购买的服务器送到公司,做好RAID,做好系统,然后送到IDC机房运维人员自行上架,装系统,初始化,测试回公司远程连接,配置各种服务、压力测试,正式上线1.3.3.和IDC约定上架日期确定好上架日期之后,提上架工单上架人员:姓名、身份证、服务器型号、机柜号、IP地址1.3.4.去IDC机房需要准备的工具身份证U盘、光盘、移动光驱、空白光盘、螺丝刀(也可以提前配置好==无人值守安装服务==)1.3.5.去机房配置服务器细节:one: 先去IDC机房等记去前台等记,拿到工牌寄存背包,登机领取==显示器、键盘或者小推车==:two: 配置服务器做RAID、装系统、配网络、==配置防火墙==初始化、测试SSH连接,确保投过跳板机能正常连接:three: 收尾去前台归还工牌,小推车(放显示器和键盘)拿回自己的东西手工回公司endl
2024年06月06日
23 阅读
0 评论
0 点赞
2024-05-17
Rocky、Ubuntu、RHEL初始化
@TOC麒麟openKylin ----------> ubuntu统信UOS ----------- rocky ------------- centos ----------- rhel一、==Rocky、Ubuntu、RHEL初始化==1.最小化安装 2.关闭防火墙 systemctl disable --now firewalld 3.关闭SELinux vim /etc/selinux/config SELINUX=disabled 4.重启生效 reboot 5. 实现邮件通信 yum -y install postfix mailx systemctl enable --now postfix 6. yum 源 CentOS8 : BaseOS , appstream,epel CentOS7: BaseOS, epel 7. 最小化安装常用软件: 8. 网卡NAT 地址:10.0.0.X/24 GATEWAY:10.0.0.2,dns:10.0.0.2,180.76.76.76 名称修改:ethX 9. 时间同步1.1.最小化安装1.2.关闭防火墙#关闭防火墙,为了学习方便。 systemctl stop firewalld ##关闭防火墙 systemctl disable firewalld ##禁止开机自启动 systemctl status firewalld ##查看关闭结果systemctl disable --now firewalld# centos6 service iptables stop chkconfig iptables off1.3.关闭SELinux##关闭selinux,它是一个安全模块,不关闭,老管着你。 setenforce 0 ##临时关闭selinux getenforce ##查看是否关闭 sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config ##永久关闭vim /etc/selinux/config SELINUX=disabled1.4.重启生效reboot1.5. 实现邮件通信yum -y install postfix mailx systemctl enable --now postfix 1.6. yum 源 和dnfCentOS8 : BaseOS , appstream,epel CentOS7: BaseOS, epel yum install remove repolist list search provides history clean all yum clean all # 清缓存1.6.1.配置yum源/etc/yum.repos.d/xxx.repo [repoid] 唯一 name=说明 baseurl=http:// https:// ftp:// file:// gpgcheck=0 enabled=1 #gpgkey=cd /etc/yum.repos.d[11:53:05 root@Rocky85 yum.repos.d]# cat test.repo [BaseOS] name=aliyun BaseOS baseurl=https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/ gpgcheck=1 # 本地的key更安全,检查是否合法,避免病毒,更加安全 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial # 网络的key # gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-rockyofficial [AppStream] name=aliyun BaseOS baseurl=https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/ gpgcheck=1 # 本地的key更安全,检查是否合法,避免病毒,更加安全 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial [extras] name=aliyun extras baseurl=https://mirrors.aliyun.com/rockylinux/8/extras/x86_64/os/ gpgcheck=0 [epel] name=aliyun epel baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/ gpgcheck=0 [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true 1.6.2.只下载安装包,不安装yum -y install --downloadonly --downloaddir=/data/nginx/ nginx yum -y install --downloadonly --downloaddir=/data/httpd/ httpd1.6.3.配置本地光盘ISO文件安装# 自动挂载光盘 yum -y install autofs systemctl enable --now autofs # 访问 lsblk ls /misc/cd[11:52:58 root@Rocky85 yum.repos.d]# cat cdrom.repo [BaseOS] name=cdrom BaseOS baseurl=file:///misc/cd/BaseOS gpgcheck=0 [AppStream] name=cdrom AppStream baseurl=file:///misc/cd/AppStream gpgcheck=0 1.6.4.配置阿里云epel源[11:58:20 root@Rocky85 yum.repos.d]# vim cdrom.repo [BaseOS] name=cdrom BaseOS baseurl=file:///misc/cd/BaseOS gpgcheck=0 [AppStream] name=cdrom AppStream baseurl=file:///misc/cd/AppStream gpgcheck=0 [extras] name=aliyun extras baseurl=https://mirrors.aliyun.com/rockylinux/8/extras/x86_64/os/ gpgcheck=0 [epel] name=aliyun epel baseurl=https://mirrors.aliyun.com/epel/8/Everything/x86_64/ gpgcheck=01.6.5.base-for-centos6.repo# 备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup # centos6(centos6官方源已下线,建议切换centos-vault源) wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo1.6.6.base-for-centos7.repo1.6.6.1.打补丁安装常用工具1.6.6.1.a.配置软件下载,将从官方下载改到阿里云地址下载:curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo成功标志,就是没有反应。1.6.6.1.b.配置第三方的软件下载的epel源更新地址为阿里云的地址:curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo成功标志,就是没有反应。1.6.6.1.c.CentOS7要安装的企业运维常用基础工具包yum install psmisc bash-completion lrzsz wget -y1.6.6.2.base-for-centos7.repo[base] name=base baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/ https://mirrors.huaweicloud.com/centos/\$releasever/os/\$basearch/ https://mirrors.cloud.tencent.com/centos/\$releasever/os/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/ http://mirrors.163.com/centos/\$releasever/os/\$basearch/ http://mirrors.sohu.com/centos/\$releasever/os/\$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/ https://mirrors.huaweicloud.com/epel/\$releasever/\$basearch/ https://mirrors.cloud.tencent.com/epel/\$releasever/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/ https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/ https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/ http://mirrors.163.com/centos/\$releasever/extras/\$basearch/ http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever [updates] name=updates baseurl=https://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/ https://mirrors.huaweicloud.com/centos/\$releasever/updates/\$basearch/ https://mirrors.cloud.tencent.com/centos/\$releasever/updates/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/updates/\$basearch/ http://mirrors.163.com/centos/\$releasever/updates/\$basearch/ http://mirrors.sohu.com/centos/\$releasever/updates/\$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever [centosplus] name=centosplus baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/ https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/ https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/ http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/ http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever[base] name=base baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/ https://mirrors.huaweicloud.com/centos/$releasever/os/$basearch/ https://mirrors.cloud.tencent.com/centos/$releasever/os/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/ http://mirrors.163.com/centos/$releasever/os/$basearch/ http://mirrors.sohu.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch/ https://mirrors.huaweicloud.com/epel/$releasever/$basearch/ https://mirrors.cloud.tencent.com/epel/$releasever/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/$basearch/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/ https://mirrors.cloud.tencent.com/centos/$releasever/extras/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/ http://mirrors.163.com/centos/$releasever/extras/$basearch/ http://mirrors.sohu.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever [updates] name=updates baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ https://mirrors.huaweicloud.com/centos/$releasever/updates/$basearch/ https://mirrors.cloud.tencent.com/centos/$releasever/updates/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/ http://mirrors.163.com/centos/$releasever/updates/$basearch/ http://mirrors.sohu.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever [centosplus] name=centosplus baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ https://mirrors.huaweicloud.com/centos/$releasever/centosplus/$basearch/ https://mirrors.cloud.tencent.com/centos/$releasever/centosplus/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/ http://mirrors.163.com/centos/$releasever/centosplus/$basearch/ http://mirrors.sohu.com/centos/$releasever/centosplus/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$releasever1.6.7.base-for-rocky8.repo[BaseOS] name=BaseOS baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/ https://mirrors.huaweicloud.com/centos/\$releasever/BaseOS/\$basearch/os/ https://mirrors.cloud.tencent.com/centos/\$releasever/BaseOS/\$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/BaseOS/\$basearch/os/ http://mirrors.163.com//centos/\$releasever/BaseOS/\$basearch/os/ http://mirrors.sohu.com/centos/\$releasever/BaseOS/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [AppStream] name=AppStream baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/ https://mirrors.huaweicloud.com/centos/\$releasever/AppStream/\$basearch/os/ https://mirrors.cloud.tencent.com/centos/\$releasever/AppStream/\$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/AppStream/\$basearch/os/ http://mirrors.163.com/centos/\$releasever/AppStream/\$basearch/os/ http://mirrors.sohu.com/centos/\$releasever/AppStream/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [EPEL] name=EPEL baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch/ https://mirrors.huaweicloud.com/epel/\$releasever/Everything/\$basearch/ https://mirrors.cloud.tencent.com/epel/\$releasever/Everything/\$basearch/ https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/Everything/\$basearch/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/ https://mirrors.huaweicloud.com/centos/\$releasever/extras/\$basearch/os/ https://mirrors.cloud.tencent.com/centos/\$releasever/extras/\$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/extras/\$basearch/os/ http://mirrors.163.com/centos/\$releasever/extras/\$basearch/os/ http://mirrors.sohu.com/centos/\$releasever/extras/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial enabled=1 [centosplus] name=centosplus baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/ https://mirrors.huaweicloud.com/centos/\$releasever/centosplus/\$basearch/os/ https://mirrors.cloud.tencent.com/centos/\$releasever/centosplus/\$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/centosplus/\$basearch/os/ http://mirrors.163.com/centos/\$releasever/centosplus/\$basearch/os/ http://mirrors.sohu.com/centos/\$releasever/centosplus/\$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial[BaseOS] name=BaseOS baseurl=https://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/ https://mirrors.huaweicloud.com/centos/$releasever/BaseOS/$basearch/os/ https://mirrors.cloud.tencent.com/centos/$releasever/BaseOS/$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/BaseOS/$basearch/os/ http://mirrors.163.com//centos/$releasever/BaseOS/$basearch/os/ http://mirrors.sohu.com/centos/$releasever/BaseOS/$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [AppStream] name=AppStream baseurl=https://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/ https://mirrors.huaweicloud.com/centos/$releasever/AppStream/$basearch/os/ https://mirrors.cloud.tencent.com/centos/$releasever/AppStream/$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/AppStream/$basearch/os/ http://mirrors.163.com/centos/$releasever/AppStream/$basearch/os/ http://mirrors.sohu.com/centos/$releasever/AppStream/$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial [EPEL] name=EPEL baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch/ https://mirrors.huaweicloud.com/epel/$releasever/Everything/$basearch/ https://mirrors.cloud.tencent.com/epel/$releasever/Everything/$basearch/ https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/Everything/$basearch/ gpgcheck=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/ https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os/ https://mirrors.cloud.tencent.com/centos/$releasever/extras/$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/os/ http://mirrors.163.com/centos/$releasever/extras/$basearch/os/ http://mirrors.sohu.com/centos/$releasever/extras/$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial enabled=1 [centosplus] name=centosplus baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/os/ https://mirrors.huaweicloud.com/centos/$releasever/centosplus/$basearch/os/ https://mirrors.cloud.tencent.com/centos/$releasever/centosplus/$basearch/os/ https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/os/ http://mirrors.163.com/centos/$releasever/centosplus/$basearch/os/ http://mirrors.sohu.com/centos/$releasever/centosplus/$basearch/os/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial1.7.ubuntu软件包管理dpkg和apt清华大学镜像官网: https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/阿里云镜像官网:https://developer.aliyun.com/mirror/ubuntu1.7.1.Ubuntu 软件仓库vim /etc/apt/sources.list# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # 预发布软件源,不建议启用 # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse # # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse1.8. 最小化安装常用软件:1.9. 网卡NAT设置--eth01.10. 时间同步#按计划时间同步 crontab -l #1. sync time */2 * * * * /sbin/ntpdate ntp1.aliyun.com &>/dev/null1.10.1.通用修改时间#设置时区 timedatectl set-timezone Asia/Shanghai #配置时间同步 vi /etc/chrony.conf server ntp.aliyun.com iburst server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst #allow 192.168.0.0/16 allow 0.0.0.0/0 #加此行,指定允许同步的网段 # Serve time even if not synchronized to a time source. local stratum 10 #删除此行注释,当互联网无法连接,仍然可以为客户端提供时间同步服务 systemctl restart chronyd #启用Chrony服务 #运行以下命令启动chronyd服务并设置开机自启动。 systemctl start chronyd.service systemctl enable chronyd.service #运行以下命令查看本机时间同步状态,用于验证服务是否已启动 chronyc tracking #运行以下命令查看时间同步服务器列表。 chronyc -n sources -v1.10.2.ubuntu2004修改时区1.10.3.CentOS6更新系统时间# 设置时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 安装ntpdate yum -y install ntpdate # 联网更新时间,如果成功,将系统时间写入BOIS ntpdate time.windows.com && hwclock -w1.11.Linux彩色命令提示符prompt1.11.1.Rocky81.11.1.1.ROOT用户[root@example ~]# echo $PS1 [\u@\h \W]\$PS1='\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\$ ' PS1='\[\e[1;32m\[[\[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\$ ' PS1='\e[31;1m[\u@\h \W]\$ \e[0m' # 如果是永久保存,则要写入文件 echo "PS1='\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\$ '" > /etc/profile.d/env.sh echo "PS1='\e[31;1m[\u@\h \W]\$ \e[0m'" > /etc/profile.d/env.sh source /etc/profile.d/env.sh1.11.1.2.普通用户PS1='\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ ' PS1='\[[\[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ ' PS1='\[\e[1;32m\][\t\e[31;1m \u@\h \W]\\$ \e[0m' PS1='\e[31;1m[\u@\h \W]\\$ \e[0m' # 如果是永久保存,则要写入文件 echo "PS1='\[\e[1;32m\][\t \[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ '" > /etc/profile.d/env.sh echo "PS1='\[[\[\e[1;33m\]\u\[\e[35m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$ '" > /etc/profile.d/env.sh echo "PS1='\[\e[1;32m\][\t\e[31;1m \u@\h \W]\\$ \e[0m'" > /etc/profile.d/env.sh echo "PS1='\e[31;1m[\u@\h \W]\\$ \e[0m'" > /etc/profile.d/env.sh source /etc/profile.d/env.sh1.11.2.ubuntu2004[13:49:10 dange@ubuntu2004 ~]$ echo $PS1 \[\e[1;32m\][\t \e[1;35m\]\u@\h \W]\\$\[\e[0m\] [13:49:15 dange@ubuntu2004 ~]$ [13:49:21 dange@ubuntu2004 ~]$ su - root Password: [13:49:27 root@ubuntu2004:~]# echo $PS1 \[\e[1;32m\][\t \e[1;31m\u@\h:\w]\$\e[0m1.11.2.1.ROOT用户root PS1='\e[1;31m[\u@\h:\W]\$\e[0m ' PS1='\[\e[1;32m\][\t \e[1;31m\u@\h:\W]\$\e[0m ' echo "PS1='\e[1;31m[\u@\h:\W]\$\e[0m '" >> .bashrc echo "PS1='\[\e[1;32m\][\t \e[1;31m\u@\h:\W]\$\e[0m '" >> .bashrc source .bashrc1.11.2.2.普通用户普通 PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\] ' PS1='\[\e[1;32m\][\t \e[1;35m\]\u@\h \W]\\$\[\e[0m\] ' 普通 echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\] '" >> .bashrc echo "PS1='\[\e[1;32m\][\t \e[1;35m\]\u@\h \W]\\$\[\e[0m\] '" >> .bashrc source .bashrc1.12.xshell7 vim后文件内容仍停留在屏幕的问题[18:12:34 root@host-200-king /]#echo $TERM linux 输出的是linux 如果不想让文件显示在屏幕上面解决办法:vim /etc/profile # 查看 /etc/profile 中的 export TERM=xterm # 在左后一行加上 export TERM=xterm source /etc/profile1.13.设置主机名hostnamectl set-hostname Rocky9.2# centos6 sed -Ei.bak '/^(HOSTNAME=)/s#^(HOSTNAME=).*#\1centos6#' /etc/sysconfig/network1.14.设置root用户并远程登录【ubuntu】使用apt安装openssh服务。 apt-get install openssh-server想让root恢复成初始时候一样: 没有密码; 切换root账号输入密码错误; 使用下面的命令: #删除root账号密码 sudo passwd -d root #锁定root账号 sudo passwd -l rootsystemctl restart sshd.service sudo /etc/init.d/ssh restart1.15.添加额外网卡第1块网卡 nat模式 : eth0 10.0.0.7 模拟公网 第2块网卡 : eth1 172.16.1.7 局域网 eth1: lan区段网卡(添加1次所有虚拟机都可以识别与使用)添加eth1网卡配置文件[root@localhost network-scripts]# cat ifcfg-eth1 NAME=eth1 DEVICE=eth1 IPADDR=172.16.1.210 PREFIX=24 ONBOOT=yes BOOTPROTO=static #不需要网关 #不用配置DNS systemctl restart network #重启网卡生效1.16.优化ssh连接速度sshd服务问题.sed -i '/^GSSAPIAuthentication/s@^@#@g' /etc/ssh/sshd_config cat >>/etc/ssh/sshd_config<<EOF UseDNS no #相当于网络命令的-n选项. GSSAPIAuthentication no #关闭GSS认证. EOF systemctl restart sshd #检查 egrep '^(GSSAPIAuthentication|UseDNS)' /etc/ssh/sshd_config注: sshd_config后面讲解sshd服务会有详解,目前配置即可.1.17.实现邮件通信:star::star::star::star::star:# 告警消息脚本 [root@mha-manager ~]# cat /usr/local/bin/sendmail.sh #!/bin/bash echo "MHA is failover!" | mail -s "MHA Warning" XXXXX@qq.com &>/dev/null [root@mha-manager ~]# chmod +x /usr/local/bin/sendmail.sh # 安装邮件服务 [root@mha-manager ~]# yum install mailx postfix # 邮件服务配置 [root@mha-manager ~]# vim /etc/mail.rc # 加在最下面 #发件箱 set from="xxxxxxxxx@qq.com" # 配置的第三方smtp服务器的地址及端口 set smtp=smtp://smtp.qq.com:587 #发件人 set smtp-auth-user="xxxxxxxxx@qq.com" #授权码 set smtp-auth-password=xxxxxxxxx # 认证方式 set smtp-auth=login #开启ssl set ssl-verify=ignore set smtp-use-starttls=yes #证书目录,下方为centos系统证书默认位置,也自行生成证书并指定 set nss-config-dir=/etc/pki/nssdb --说明 from:对方收到邮件时显示的发件人 smtp:指定第三方发邮件的smtp服务器地址,云服务器必须使用465端口默认25端口被禁用 set smtp-auth-user:第三方发邮件的用户名 set smtp-auth-password: 邮箱的授权码注意不是密码 #echo "测试邮件" | mail -s "发送成功" XXXXXXXXX@qq.com &>/dev/null # 重新启动postfix [root@mha-manager ~]# systemctl restart postfix.service # 测试告警邮件 [root@mha-manager ~]# sendmail.sh二、==Linux终端显示中文改成英文==终端显示(改动前)[10:13:41 root@host-200-king ~]#mkdir /a/a/a/a/a mkdir: 无法创建目录"/a/a/a/a/a": 没有那个文件或目录centos 7[10:18:19 root@centos ~]#cat /etc/locale.conf LANG="zh_CN.UTF-8"centos 6[10:18:19 root@centos ~]#cat /etc/sysconfig/i18n LANG="zh_CN.UTF-8"更改为LANG="en_US.UTF-8" # 需要重启 # reboot终端显示(改动后)[10:21:53 root@host-200-king ~]#mkdir a/a/a/a/a/a mkdir: cannot create directory ‘a/a/a/a/a/a’: No such file or directory[10:22:07 root@host ~]#cat /etc/locale.conf #LANG="zh_CN.UTF-8" LANG="en_US.UTF-8"三、==安装Linux常用工具==3.1.生产实践:基于最小化安装的系统建议安装下面相关包# 自动挂载光盘 yum -y install autofs systemctl enable --now autofs # 访问 lsblk ls /misc/cd# 在安装或升级软件包之前,建议先执行apt update命令,以确保获取到最新的软件包信息 apt update # 最小化安装常用工具 yum -y tmux man-pages strace # 安装Linux常用工具 yum -y install tree vim wget lrzsz psmisc wget tcpdump ftp rsync lsof yum -y install net-tools sysstat iotop iftop htop zip unzip bzip2 nc nmap telnet bc yum -y install psmisc httpd-tools bind-utils nethogs expect # 终端复用器软件就是会话与窗口的“解绑”工具 yum -y install epel-release yum -y install screen # 命令补全 yum -y install bash-completion # 安装linux娱乐工具 yum install -y sl cowsay3.2.centosyum -y install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel nfs-utilsyum -y install tmux man-pages strace tree vim wget lrzsz psmisc wget tcpdump ftp rsync lsof net-tools sysstat iotop iftop htop zip unzip bzip2 nc nmap telnet bc psmisc httpd-tools bind-utils nethogs expect epel-release screen bash-completion sl cowsay3.3.ubuntuapt -y install tmux strace tree vim wget lrzsz psmisc wget tcpdump ftp rsync lsof net-tools sysstat iotop iftop htop zip unzip bzip2 nmap telnet bc psmisc nethogs expect ntpdate screen bash-completion sl cowsay # ubuntu 16.04 apt -y install tmux strace vim wget psmisc wget tcpdump ftp rsync lsof net-tools bzip2 telnet psmisc screen bash-completion运行 tree 命令,运行 sl 命令命令作用tree以树形显示目录结构psmiscpstree 等命令vimvi升级版wget下载工具==bash-completion(默认源)====自动补全增强工具,可以补全命令参数====bash-completion-extras(epel)====自动补全超级增强工具,可以补全命令参数==lrzsz上传下载工具 rz和sznet-toolsifconfig route命令属于的软件包sysstatsar iostat 属于的软件包iotop查询系统每个进程的io读写情况 swap写入情况iftop查询网络流量情况nethogs显示每个进程的流量情况htoptop升级版unzip解压ncncat 类似telnetnmap网络扫描工具telnet检查端口是否开启bc基础计算器lsof显示所有被打开的文件digDNS解析 bind-utils软件包nslookupDNS解析 bind-utils软件包hostDNS解析 bind-utils软件包htpasswd创建密码文件 存放用户名和加密的密码 一般用于 nginx 简单认证中mkpasswd生成随机密码 属于expect软件包3.4.装机必备软件【装机必备软件】sysstat iotop pcp-system-tools net-tools freerdp tcpdump telnet nmap mtr nmap-ncat lsof xfsdump strace boom-boot smartmontools yum-utils tmux psmisc rsync rsyslog sos strace perf valgrind bcc-tools# 系统监控和诊断工具 sysstat iotop pcp-system-tools # 网络 net-tools freerdp tcpdump telnet nmap mtr nmap-ncat # 文件系统与磁盘 lsof xfsdump strace boom-boot smartmontools # 系统管理 yum-utils tmux psmisc rsync # 日志 rsyslog sos # 调试和分析 strace perf valgrind bcc-tools四、==修改网卡名称为eth0==# 获取网卡文件名 ip addr | sed -En '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p'地址:10.0.0.X/24 GATEWAY:10.0.0.2,dns:10.0.0.2,180.76.76.76 名称修改:ethX:one: IP:two: netmask:three: gateway 路由表[root@rocky86 ~]# ll /etc/sysconfig/network-scripts/ total 4 -rw-r--r-- 1 root root 244 Aug 10 14:57 ifcfg-eth0 [root@rocky86 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eth0 UUID=b984cc1e-e109-4d5d-8c62-77714323de08 DEVICE=eth0 ONBOOT=yes常用配置项配置项变量名备注设备名==DEVICE==指定网卡设备名称连接名==NAME==命令行下显示的配置名称IP地址==IPADDR==IPV4的IP地址,多个地址可以写成 IPADDR2,IPADDR3子网掩码NETMASK传统写法 NETMASK=255.255.255.0子网掩码==PREFIX==新写法 PREFIX=24,多地址可以写成 PREFIX2,PREFIX3网关==GATEWAY==提供跨网段通讯功能IP地址类型==BOOTPROTO==获取IP方式,==dhcp 动态获取,none\static 静态地址==网卡设备是否开机启用==ONBOOT==是否开机启用,==yes== 启用,no 禁用域后缀DOMAIN域后缀主DNSDNS1主DNS次DNSDNS2次DNS第三DNSDNS3第三DNS没有子网掩码就搞不清在哪个网段4.1.rocky8、rhel8 网卡设置4.1.1.设置ip地址10.0.0.8/24 10.0.0.24.1.2.设置eth0步骤# 调整网卡名称 mv /etc/sysconfig/network-scripts/ifcfg-ens160 /etc/sysconfig/network-scripts/ifcfg-eth0 # 修改网卡的配置信息 vim /etc/sysconfig/network-scripts/ifcfg-eth0 # 在GRUB_CMDLINE_LINUX 这一行末尾,添加内核参数 vim /etc/default/grub net.ifnames=0 biosdevname=0 #sed -Ei.bak 's/^(GRUB_CMDLINE_LINUX=.*)"$/\1 net.ifnames=0 biosdevname=0"/' /etc/default/grub sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0 biosdevname=0"#' /etc/default/grub # 运行命令如下命令 来重新生成GRUB配置并更新内核参数,然后reboot系统 grub2-mkconfig -o /boot/grub2/grub.cfg # 重启生效 reboot4.1.3.设置静态ipDEVICE=eth0 NAME=eth0 # IP 的配置方法[none|static|bootp|dhcp](引导时不使用协议|静态分配 IP|BOOTP 协议|DHCP 协议) BOOTPROTO=static IPADDR=10.0.0.156 # NETMASK="255.255.255.0" PREFIX=24 GATEWAY=10.0.0.2 DNS1=10.0.0.2 DNS2=180.76.76.76 DNS3=119.29.29.29 ONBOOT=yes4.1.4.重新启动加载网卡信息nmcli connection nmcli connection reload nmcli connection up eth04.1.5.Rocky85系列GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rl-swap rd.lvm.lv=rl/root rd.lvm.lv=rl/swap rhgb quiet net.ifnames=0 biosdevname=0" GRUB_DISABLE_RECOVERY="true" GRUB_ENABLE_BLSCFG=true4.1.6.redhat红帽系列4.2.centos7网卡设置4.2.1.调整网卡名称mv /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-eth04.2.2.调整网卡配置信息vim /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0 NAME=eth0 BOOTPROTO=static IPADDR=10.0.0.7 PREFIX=24 GATEWAY=10.0.0.2 DNS1=10.0.0.2 DNS2=180.76.76.76 ONBOOT=yes# CentOS 7使用的是NetworkManager服务来管理网络配置 systemctl restart network 确认NetworkManager服务是否正在运行:systemctl status NetworkManager 如果NetworkManager正在运行,尝试停止该服务:systemctl stop NetworkManager ,然后重新启动网络服务:systemctl restart network4.2.3.修改生成grub配置vim /etc/sysconfig/grub net.ifnames=0 biosdevname=04.2.4.生成grub启动菜单grub2-mkconfig -o /boot/grub2/grub.cfg reboot4.3.centos6网卡设置vim /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0 NAME=eth0 BOOTPROTO=static IPADDR=10.0.0.157 PREFIX=24 GATEWAY=10.0.0.2 DNS1=10.0.0.2 DNS2=180.76.76.76 ONBOOT=yes# centos6 service network restart4.4.centos6网卡设置【克隆机网卡报错】问题:重启网卡提示Bringing up interface eth0: Device eth0 does not seem to be present,delaying initialization.或者ifconfig无eth0.原因:这是因为克隆的机器没有正确的mac,UUID信息冲突导致的。 方法:1)首先将/etc/udev/rules.d/70-persistent-net.rules文件删除;2)然后将网卡配置文件/etc/sysconfig/network-scripts/ifcfg-eth0的uuid和hwaddr这两行删除;3)执行reboot命令,重启系统;4)这时重启网卡就能正常重启了:/etc/init.d/network restart;5)ifconfig查看ip信息。4.5.rocky9网卡设置vim /etc/default/grub net.ifnames=0 biosdevname=0 # 为grub2生成其配置文件 grub2-mkconfig -o /etc/grub2.conf #重启系统 reboot #网卡名就更改了# 将网卡配置文件先备份一下 cd /etc/NetworkManager/system-connections mv ens160.nmconnection eth0.nmconnection[14:13:24 root@Rocky92 system-connections]# cat eth0.nmconnection [connection] id=eth0 uuid=95556cb9-5f0e-333f-a767-cfe755026399 type=ethernet autoconnect-priority=-999 interface-name=eth0 timestamp=1727873253 [ethernet] [ipv4] address1=10.0.0.9/24,10.0.0.2 dns=10.0.0.2;180.76.76.76;119.29.29.29 method=manual [ipv6] addr-gen-mode=eui64 method=auto [proxy]4.6.ubuntu1604网卡设置【不同】# 注意:不同的linux系统,网卡配置文件是不同的,这里ubuntu的网卡配置文件是/etc/network/interfaces vim /etc/network/interfaces# The primary network interface auto eth0 iface eth0 inet static address 10.0.0.100 netmask 255.255.0.0 gateway 10.0.0.2 dns-nameservers 8.8.8.8 10.0.0.2 180.76.76.764.7.ubuntu2004网卡设置ens33修改为eth0【不同】4.7.1.相关命令及配置vim /etc/default/grub net.ifnames=0 grub-mkconfig -o /boot/grub/grub.cfg reboot # 更改网卡名# 查看当前网络信息 ifconfig,将此处的ens33修改为eth0 # 编辑 vi /etc/default/grub # 将字段GRUB_CMDLINE_LINUX="" # 修改为GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" sed -Ei.bak 's/^(GRUB_CMDLINE_LINUX=".*)"$/\1net.ifnames=0 biosdevname=0"/' /etc/default/grub # 修改完更新 update-grub # 修改网络配置,ens33修改为eth0 vim /etc/netplan/00-installer-config.yaml #上述配置后执行指令 sudo netplan apply # 重启Ubuntu,ifconfig查看网卡是否更新[10:59:06 root@ubuntu2004:/etc/netplan]# cat 01-netcfg.yaml # This file describes the network interfaces available on your system # For more information, see netplan(5) network: version: 2 renderer: networkd ethernets: eth0: addresses: - 10.0.0.153/24 gateway4: 10.0.0.2 nameservers: search: - baidu.com addresses: [8.8.8.8, 129.19.19.19, 233.6.6.6]4.7.2.配置步骤4.7.3.更新grub启动菜单,重启生效4.7.4.更改配置文件4.7.5.查看是否生效4.7.6.新增加一个网卡4.7.7.查看是否生效4.7.8.查看路由4.8.ubuntu2204网卡设置【不同】vim /etc/default/grub net.ifnames=0 grub-mkconfig -o /boot/grub/grub.cfg reboot # 更改网卡名[20:07:41 root@ubuntu24:~]# chmod 600 /etc/netplan/01.netcfg.yaml [20:07:41 root@ubuntu24:~]# vim /etc/netplan/01.netcfg.yaml [20:07:41 root@ubuntu24:~]# cat /etc/netplan/01.netcfg.yaml network: version: 2 renderer: networkd ethernets: eth0: addresses: - 10.0.0.100/24 routes: - to: default via: 10.0.0.2 nameservers: search: - baidu.com addresses: [8.8.8.8, 129.19.19.19, 233.6.6.6] 最近在使用Ubuntu Server 22.04做项目开发测试时发现每次重启和关机后,所设置的静态IP地址都会回复到安装系统时所设置的ip [root@ubuntu24-13:~]# ls /etc/cloud/cloud.cfg.d 05_logging.cfg 90-installer-network.cfg 99-installer.cfg curtin-preserve-sources.cfg 20-disable-cc-dpkg-grub.cfg 90_dpkg.cfg README [root@ubuntu24-13:~]# vim /etc/cloud/cloud.cfg.d/90-installer-network.cfg [root@ubuntu24-13:~]# cat /etc/cloud/cloud.cfg.d/90-installer-network.cfg # This is the network config written by 'subiquity' network: ethernets: eth0: dhcp4: false version: 2 [root@ubuntu24-13:~]# echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg [root@ubuntu24-13:~]# cloud-init clean [root@ubuntu24-13:~]# reboot修改DHCP获取IP地址如果需要修改为DHCP服务器自动获取IP地址,可以配置dhcp4为true如果需要修改为DHCP服务器手动获取IP地址,可以配置dhcp4为falsevim 50-cloud-init.yamlendl
2024年05月17日
6 阅读
0 评论
0 点赞
1
...
10
11
12
...
15