Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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 1:25

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.

share|improve this answer
Also running it directly in sudo without the su resolves the quoting nightmare. Should work exactly as is in the question. – Patrick Mar 28 '12 at 1: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 1: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 2:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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