0

I'm writing a program to setup new Debian installs but I can't get sed to do what I want. I want to add new users to sshd_config as allowed users

but I get this: This is the result I get now:

  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

where it should be: The expected result is this:

     AllowUsers user
     AllowUsers something7
     AllowUsers something78
     AllowUsers something79

Here is the code:

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 }

Here is the debug output:

+ 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

QUESTION:

How do I add the users to AllowedUsers in the sshd_config without the duplicates?? also do you know what is happening in my code?

  • 1
    What doesn't happen, exactly? – Jeff Schaller Sep 17 '18 at 19:05
  • @Kusalananda I dont understand, where should I add $ – somethingSomething Sep 17 '18 at 19:50
  • @Kusalananda It just adds the character to the end – somethingSomething Sep 17 '18 at 19:58
  • 1
    Ok, I'm not reading your code properly. Sorry for the noise. I will have to come back to this after some sleep. – Kusalananda Sep 17 '18 at 20:00
  • 1
    AFAIK AllowUsers accepts a space-separated list of names, so there's really no need to duplicate the key over and over; if you just append usernames to the same line (something like sed -i "/^AllowUsers/ s/$/ $user/") you avoid the runaway appending problem altogether – steeldriver Sep 17 '18 at 23:49
1
sed -i "/AllowUsers/a AllowUsers $user" /etc/ssh/sshd_config

adds an "AllowUsers $user" after each existing AllowUsers line.

I'd simply replace the seds by a

echo "AllowUsers $user" >> /etc/ssh/sshd_config
echo "AllowUsers $USERNAME" >> /etc/ssh/sshd_config
  • @GeradHPille Hi thanks for your answer, don't know why I didn't think of that but there is one problem, this adds the lines to the end of the file and then it doesn't have this cool professional look where the lines get added where the other lines that were already there are – somethingSomething Sep 17 '18 at 20:13
  • @somethingSomething What do you mean by "other lines that were already there are"? If you just want to add new users then what does it matter? It makes no difference where the lines are in the file. – Nasir Riley Sep 17 '18 at 21:39
0

The output is not as I originally asked for but it works so this is how I did it:

    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

This line finds the line with the pattern AllowUsers and replaces the whole line with the new AllowUsers $users

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

This line appends the text AllowUsers $users after the 6th line if the file doesn't already contain AllowUsers notice that it is written on two lines:

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

Now the program writes sshd_config this way, AllowUsers is used once with a space separated list of users:

 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.