sshd_configを編集すると、sedから目的の出力が得られません。

sshd_configを編集すると、sedから目的の出力が得られません。

新しいインストールを設定するプログラムを作成していますが、必要に応じて行うことはDebianできません。新しいユーザーをsed追加したいsshd_configallowed 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

質問:

AllowedUserssshd_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

おすすめ記事