【发布时间】:2015-11-16 13:00:52
【问题描述】:
我正在尝试自动禁用通过 root 登录 SSH。我知道我可以手动编辑文件并这样做,但我想通过 Bash 脚本(我用来初始化我的服务器)禁用 root 登录。
sed 我用的不多,但我认为基于this question,我应该使用它。
我试图在/etc/ssh/sshd_config 中替换的行是PermitRootLogin {any_value}。 {any_value} 的默认值为 yes,但我希望它适用于任何值(在同一行上)。
我尝试了命令sudo sed -i "/PermitRootLogin/c\PermitRootLogin no" /etc/ssh/sshd_config,但这也替换了包含文本“PermitRootLogin”的随机评论。
所以,我不想替换以注释标记 # 开头的行。
这是我正在尝试编辑的文件的相关部分(我的 cmets 添加了“###”):
# Authentication:
LoginGraceTime 120
PermitRootLogin yes ### I want to replace this line with "PermitRootLogin no"
StrictModes yes
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
预期的输出是:
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
然后,我尝试了这个:sed -i "\(^[\# ]\)+/PermitRootLogin/c\PermitRootLogin no" /etc/ssh/sshd_config。
这给了我一个错误:sed: -e expression #1, char 48: unterminated address regex。
我怎样才能完成我想做的事情?谢谢!
【问题讨论】:
-
...和预期的输出。