ssh and sudo login

Hi,

I am trying to execute some command, via ssh and sudo.

Here is what i want to do.

ssh localhost | sudo su - ldaprole | ls -ltrh 

However, this command gives me listing of my home directory, and not of ldaprole.

If I logic directly, when i perform sudo su - ldaprole, it directly goes to ldaprole home.

Any help in this regard is appreciated.

Thanks. John

Hi,

Why are you piping to ls? (this will get your home dir contents as you mentioned).

You could do the following the the effect you're after (but you'll have to add the -c flag for su to sudoers I suspect)

ssh localhost "sudo su - ldaprole -c \"ls -ltr\""

Thanks for reply.

This command works, but i cannot excecute any command after i sudo to the role.

ssh -t localhost ' sudo su - ldaprole | id'

How to run command after i sudo to ldaprole.?

I don't think you understand what | means. Do you really want to channel the text output of the su command into the id command? id reads no input, it just prints!

This will log you in as ldaprole, run something, then give you a prompt: $ ssh -t localhost 'sudo su - ldaprole -c "id ; exec bash"'

The -c tells su to run the given command after it. We're telling it to run id, then run a shell... Running a shell is crucial because without it, ssh and su will both consider themselves out of input and return without waiting.

Substitute your shell of choice for bash. Absolute paths preferred.

Hi,

Actually i am not allowed to run with "-c", it says permission denied. and i believe it is not there in the sudo list. Do you know any other alternative for this.

Thanks, John.

Hi John,

Please see my earlier post, specifically:

I suggest you request that your Sysadmin "does the needful", because sudo will consume arguments passed and check whether they correspond to your allowed entries in the sudoers file.

1 Like