编辑 sshd_config 时,我没有从 sed 获得所需的输出

编辑 sshd_config 时,我没有从 sed 获得所需的输出

我正在编写一个程序来设置新Debian安装,但我无法sed执行我想要的操作。我想添加新sshd_config用户allowed 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不重复的情况下添加用户?你也知道我的代码中发生了什么吗?

答案1

sed -i "/AllowUsers/a AllowUsers $user" /etc/ssh/sshd_config

在每个现有的AllowUsers 行后添加一个“AllowUsers $user”。

我只需将 sed 替换为

echo "AllowUsers $user" >> /etc/ssh/sshd_config
echo "AllowUsers $USERNAME" >> /etc/ssh/sshd_config

答案2

输出并不像我最初要求的那样,但它有效,所以我就是这样做的:

    users=""
    /bin/cp -f "$CURRENTDIR"/sshd_config /etc/ssh/sshd_config
    sed -i "s/Port 22/Port $PORT/" /etc/ssh/sshd_config
    for user in `awk -F: '$3 >= 1000 { print $1 }' /etc/passwd`
    do
        users+="${user} "
    done
    if grep "AllowUsers" /etc/ssh/sshd_config
    then
        sed -i "/AllowUsers/c\AllowUsers $users" /etc/ssh/sshd_config
    else
        sed -i "6 a \
        AllowUsers $users" /etc/ssh/sshd_config
    fi

该行找到具有模式的行AllowUsers,并用新的行替换整行 AllowUsers $users

sed -i "/AllowUsers/c\AllowUsers $users" /etc/ssh/sshd_config

如果文件尚未包含通知,则此行将文本追加到AllowUsers $users以下两行之后:6th lineAllowUsers

sed -i "6 a \
AllowUsers $users" /etc/ssh/sshd_config

现在程序sshd_config这样写,与单独的用户列表AllowUsers一起使用一次:space

 1 # Package generated configuration file
 2 # See the sshd_config(5) manpage for details
 3
 4 # What ports, IPs and protocols we listen for
 5 Port 22300
 6
 7 AllowUsers user something78 something79 something7
 8

相关内容