标签:sed实战案例
整理sed实战修改多行配置技巧,以下部分内容转自老男孩老师博客!
http://oldboy.blog.51cto.com/2561410/1610998
老男孩老师有关sed实战技巧分享,来自课堂教学内容实战
1、在指定行前插入两行内容,分别为oldboy和oldgirl。
提示:被修改的文件内容必须要大于等于2行
1  | sed -i ‘2 ioldboy\noldgirl‘ sshd_config | 
2、企业实战例子:快速更改SSH配置(一键完成增加若干参数)
   2.1 在文件sshd_config 中的第13行前增加或插入内容:
1  | [root@oldboy ssh]# sed -i ‘13 iPort 52113\nPermitRootLogin no\nPermitEmptyPasswords no\nUseDNS no\nGSSAPIAuthentication no‘  sshd_config | 
查看:
1 2 3 4 5 6  | [root@oldboy ssh]# sed -n ‘13,17p‘ sshd_config Port 52113PermitRootLogin noPermitEmptyPasswords noUseDNS noGSSAPIAuthentication no | 
2.2 在文件sshd_config 中的最后一行之后增加或插入内容:
[root@c6-6moban ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-eth0
1 DEVICE=eth0
2 HWADDR=00:0C:29:E1:47:FC
3 TYPE=Ethernet
4 UUID=89886144-a7a8-4c97-8045-808f5c671464
5 ONBOOT=yes
6 NM_CONTROLLED=yes
7 BOOTPROTO=static
[root@c6-6moban ~]#sed -i ‘$a IPADDR=192.168.0.166\nNETMASK=255.255.255.0\nGATEWAY=192.168.0.1\nDNS1=202.100.64.68\nDNS2=61.178.0.93‘ /etc/sysconfig/network-scripts/ifcfg-eth0
[root@c6-6moban ~]# cat -n /etc/sysconfig/network-scripts/ifcfg-eth0
1 DEVICE=eth0
2 HWADDR=00:0C:29:E1:47:FC
3 TYPE=Ethernet
4 UUID=89886144-a7a8-4c97-8045-808f5c671464
5 ONBOOT=yes
6 NM_CONTROLLED=yes
7 BOOTPROTO=static
8 IPADDR=192.168.0.166
9 NETMASK=255.255.255.0
10 GATEWAY=192.168.0.1
11 DNS1=202.100.64.68
12 DNS2=61.178.0.93
:3、如果是快速修改参数可以用如下方法(企业实战例子)
1 2 3 4 5 6 7 8  | echo "#--------sshConfig修改ssh默认登录端口,禁止root登录----------------------------#"\cp /etc/ssh/sshd_config /etc/ssh/sshd_config.$(date +"%F"-$RANDOM)sed -i ‘s%#Port 22%Port 52113%‘ /etc/ssh/sshd_configsed -i ‘s%#PermitRootLogin yes%PermitRootLogin no%‘ /etc/ssh/sshd_configsed -i ‘s%#PermitEmptyPasswords no%PermitEmptyPasswords no%‘ /etc/ssh/sshd_configsed -i ‘s%#UseDNS yes%UseDNS no%‘ /etc/ssh/sshd_configsed -i ‘s%GSSAPIAuthentication yes%GSSAPIAuthentication no%‘ /etc/ssh/sshd_configegrep "UseDNS|52113|RootLogin|EmptyPass|GSSAPIAuthentication" /etc/ssh/sshd_config | 
提示:如果是指定行修改还可以是:
1  | sed -i ‘2735s/admin_tenant_name=service/admin_tenant_name=admin/‘ nova.conf | 
本文出自 “兰州linux运维” 博客,请务必保留此出处http://linuxzkq.blog.51cto.com/9379412/1634843
标签:sed实战案例
原文地址:http://linuxzkq.blog.51cto.com/9379412/1634843