使用sed显示/etc/ssh/sshd_config文件中所有不是空白的行。
时间: 2023-09-16 17:08:53 浏览: 48
可以使用如下的sed命令来显示/etc/ssh/sshd_config文件中所有不是空白的行:
```
sed -n '/\S/p' /etc/ssh/sshd_config
```
解释一下这个命令:
- `-n` 参数表示只输出匹配的行,不输出其他行。
- `\S` 是一个正则表达式,表示匹配非空白字符。
- `p` 命令表示输出匹配的行。
- `/pattern/` 是一个正则表达式匹配模式,用来匹配文件中的行。
所以,上述命令就是匹配所有不是空白字符的行,并输出它们。
相关问题
脚本 用sed语句显示/etc/ssh/sshd_config文件中所有不是空白的行
您可以使用以下`sed`命令来显示`/etc/ssh/sshd_config`文件中所有不是空白的行:
```
sed -n '/\S/p' /etc/ssh/sshd_config
```
其中,`-n`参数表示只显示匹配的行,`/\S/`表示匹配所有非空白字符的行,`p`表示打印匹配的行。
逐条注释 ``` if grep -q "Protocol" /etc/ssh/sshd_config then sed -i '/Protocol/s/^#//' /etc/ssh/sshd_config sed -i "s/Protocol.*/Protocol 2/g" /etc/ssh/sshd_config else echo 'Protocol 2' >> /etc/ssh/sshd_config fi if grep -q "PrintMotd" /etc/ssh/sshd_config then sed -i '/PrintMotd/s/^#//' /etc/ssh/sshd_config sed -i "s/PrintMotd.*/PrintMotd yes/g" /etc/ssh/sshd_config else echo "PrintMotd yes" >> /etc/ssh/sshd_config fi service sshd restart systemctl restart sshd.service ```
这段脚本的作用是修改/etc/ssh/sshd_config文件中的SSH配置,并重新启动SSH服务。具体解释如下:
```shell
if grep -q "Protocol" /etc/ssh/sshd_config
then
sed -i '/Protocol/s/^#//' /etc/ssh/sshd_config
sed -i "s/Protocol.*/Protocol 2/g" /etc/ssh/sshd_config
else
echo 'Protocol 2' >> /etc/ssh/sshd_config
fi
```
- 检查/etc/ssh/sshd_config文件中是否存在"Protocol"这一行。
- 如果存在,则使用sed命令将该行的行首的注释符号#去掉,即取消注释。
- 然后使用sed命令将该行的值修改为"Protocol 2",即设置SSH协议版本为2。
- 如果不存在,则在文件末尾添加一行"Protocol 2"。
```shell
if grep -q "PrintMotd" /etc/ssh/sshd_config
then
sed -i '/PrintMotd/s/^#//' /etc/ssh/sshd_config
sed -i "s/PrintMotd.*/PrintMotd yes/g" /etc/ssh/sshd_config
else
echo "PrintMotd yes" >> /etc/ssh/sshd_config
fi
```
- 检查/etc/ssh/sshd_config文件中是否存在"PrintMotd"这一行。
- 如果存在,则使用sed命令将该行的行首的注释符号#去掉,即取消注释。
- 然后使用sed命令将该行的值修改为"PrintMotd yes",即启用SSH登录时显示欢迎信息。
- 如果不存在,则在文件末尾添加一行"PrintMotd yes"。
```shell
service sshd restart
systemctl restart sshd.service
```
- 重新启动SSH服务,使配置生效。这两行命令可能根据Linux发行版的不同而有所区别,用于重启SSH服务。