Sudo -u username -s “cmd arg” returns command not found

quotingsudo

I used to execute the command:

sudo -u elasticsearch -s "ulimit -Hn"

and it was returning the value of ulimit -Hn as seen by user elasticsearch until I tried it on Ubuntu 11.10. There it returns:

/bin/bash: ulimit -Hn: command not found

I tried a few different commands, and when I use an argument I always get "command not found":

$ sudo -u elasticsearch -s "ls all.sh"
/bin/bash: ls all.sh: command not found

Any ideas on how can I execute a command that requires arguments through sudo?

Best Answer

You need to remove the double-quotes. It is trying to run a command called "ulimit -Hn" as a single command, spaces and all. -s needs to be the last sudo option on the sudo command line, and all following arguments are passed to $SHELL -c to execute.

sudo -u elasticsearch -s ulimit -Hn

I guess the way -s is processed has changed, as the current way allows you to pass arguments with spaces to $SHELL by escaping them on the command line:

$ touch '/tmp/foo bar'
$ sudo -s rm '/tmp/foo bar'

The old method of argument handling would split that '/tmp/foo bar' argument into two, breaking the command.

Related Question