1

I need to run the following command:

sed -i 's/^/command="\/bin\/false",no-agent-forwarding,no-pty,no-X11-forwarding, /' ~/.ssh/authorized_keys

within sudo su user -c

I think I've spent about 3 hours reading quoting guides, similar examples, and testing within bash and I still can't find a way that works :(

here is my history if you want a good laugh at my misfortune http://pastebin.com/K6YMm9mp

bVector
  • 140
  • 2
  • 10
  • well I found one problem, somewhere along the way I lost my escapes for `"/bin/false"`. it should have been `"\/bin\/false"` I think it may have been working somewhere along the line if I had those – bVector Mar 28 '12 at 01:25

1 Answers1

4

sudo -u user command will run command as user without the need to invoke su. You will need to set it up in your sudoers file first.

Also, you could put the command you want to run in a small script and sidestep the quoting problems.

pgs
  • 3,521
  • 19
  • 19
  • Also running it directly in sudo without the `su` resolves the quoting nightmare. Should work exactly as is in the question. – phemmer Mar 28 '12 at 01:37
  • will this execute the command as the user with the proper environment/permissions? I didn't try sudo -u as I had been advised that su would make sure the permissions were proper for the ssh keys to be used without having to manually chmod 400 them afterwards – bVector Mar 28 '12 at 01:45
  • You can think of sudo without specifying a user as being the same as running `sudo -u root` - it runs the command as root, inheriting root's environment (with a couple of well documented exceptions). Changing the user will inherit that user's environment and should be the same as using su. If you have any doubts, test it. Also, seriously consider making it a small script - if you need to do this more than once it's worth the tiny bit of effort. Plus, if you're worried about it, you can easily stick `chmod 400` at the end to force the correct permissions. – pgs Mar 28 '12 at 02:38