Find the answer to your Linux question:
Results 1 to 3 of 3
What I am trying to achive is the editing of /etc/ssh/sshd_config I have the line Code: AllowUsers usera userb And I want to be able to append to the end ...
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8

    sed append to end of a specific line

    What I am trying to achive is the editing of /etc/ssh/sshd_config

    I have the line

    Code:
    AllowUsers usera userb
    And I want to be able to append to the end of this line from a bash script.

    I have muddled together from other sites and got the following:-

    Code:
    sed -e "s/^AllowUsers/\0 userc/" -i /etc/ssh/sshd_config
    But this appears to add userc directly after AllowUsers and not after userb

    I'm not great with sed so it's proably something simple.

    Thanks in advance

  2. #2
    Just Joined!
    Join Date
    Sep 2010
    Posts
    8
    Ok have found the solution:-

    Code:
    sed -e '/AllowUsers/s/$/ userc/' -i /etc/ssh/sshd_config
    Seems to work for me.

  3. #3
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    I don't think a further reader would understand that (it took me some time to figure this out) and you could rewrite this to:

    Code:
    sed -e '/AllowUsers/s/AllowUsers\(.*\)$/AllowUsers\1 newuser/g' test > test
    I use here backreferences to make it more clear what the expression does and use piping to make it clear that this command also writes to the file. One could drive it further and parse each single username that is already configured by means of a sed subroutine.

    Code:
    sed -e '/AllowUsers/s/AllowUsers\({...}\)$/AllowUsers\1 newuser/g' test > test
    Thus you could add and remove users invoking this command only.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...