我正在编写一个程序来设置新的Debian
安装,但我无法sed
做我想做的事。我想将新用户添加到sshd_config
asallowed users
但我明白了:这是我现在得到的结果:
6
7 AllowUsers user
8 AllowUsers something78
9 AllowUsers something7
10 AllowUsers something78
11 AllowUsers something79
12 AllowUsers something78
13 AllowUsers something7
14 AllowUsers something78
它应该在哪里:预期结果是这样的:
AllowUsers user
AllowUsers something7
AllowUsers something78
AllowUsers something79
这是代码:
setUPsshd()
4 {
5 if grep "Port $PORT" /etc/ssh/sshd_config
6 then
7 echo "sshd already set, skipping!"
8 else
9 #/bin/cp -f "$CURRENTDIR"/sshd_config /etc/ssh/sshd_config
10 sed -i "s/Port 22/Port $PORT/" /etc/ssh/sshd_config
11 for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
12 do
13 sed -i "/AllowUsers/a AllowUsers $user" /etc/ssh/sshd_config
14 done
15 USERNAME=`awk -F: '$3 == 1000 { print $1 }' /etc/passwd`
16 if ! grep "AllowUsers $USERNAME" /etc/ssh/sshd_config
17 then
18 sed -i "/AllowUsers/a AllowUsers $USERNAME" /etc/ssh/sshd_config
19 fi
20 echo "chmod 644 /etc/ssh/sshd_config"
21 echo "/etc/init.d/ssh restart"
22 fi
23 }
这是调试输出:
+ PORT=22301
+ setUPsshd
+ grep 'Port 22' /etc/ssh/sshd_config
+ /bin/cp -f /tmp/svaka/sshd_config /etc/ssh/sshd_config
+ sed -i 's/Port 22/Port 22301/' /etc/ssh/sshd_config
++ awk -F: '$3 > 1000 { print $1 }' /etc/passwd
+ for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
+ sed -i '/AllowUsers/a AllowUsers something79' /etc/ssh/sshd_config
+ for user in `awk -F: '$3 > 1000 { print $1 }' /etc/passwd`
+ sed -i '/AllowUsers/a AllowUsers something7' /etc/ssh/sshd_config
++ awk -F: '$3 == 1000 { print $1 }' /etc/passwd
+ USERNAME=something78
+ grep 'AllowUsers something78' /etc/ssh/sshd_config
+ sed -i '/AllowUsers/a AllowUsers something78' /etc/ssh/sshd_config
+ echo 'chmod 644 /etc/ssh/sshd_config'
chmod 644 /etc/ssh/sshd_config
+ echo '/etc/init.d/ssh restart'
/etc/init.d/ssh restart
问题:
如何AllowedUsers
在sshd_config
没有重复项的情况下添加用户?你也知道我的代码中发生了什么吗?
在每个现有的 AllowUsers 行之后添加一个“AllowUsers $user”。
我只需将 seds 替换为
输出不是我最初要求的,但它可以工作,所以我就是这样做的:
此行找到带有模式
AllowUsers
的行并用新的替换整行AllowUsers $users
此行在if 文件还没有包含它写在两行的通知
AllowUsers $users
之后附加文本:6th line
AllowUsers
sshd_config
现在程序以这种方式编写,与单独的用户列表AllowUsers
一起使用一次:space