Operating System - Linux
1753797 Members
7270 Online
108799 Solutions
New Discussion юеВ

Running sudo on remote machine question

 
SOLVED
Go to solution
Vitaly Karasik_1
Honored Contributor

Running sudo on remote machine question

What a shame - it seems to be trivial, but I don't understand why it doesn't work.

I'm running a command on remote machine via "sudo". Why /tmp/kkk isn't created under root?

root@linux1 ~]# ssh -t vitaly@server sudo 'id; date >/tmp/kkk;ls -l /tmp/kkk'
uid=0(root) gid=0(root) groups=0(root)
-rw-r--r-- 1 vitaly vitaly 29 Nov 23 12:23 /tmp/kkk
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: Running sudo on remote machine question

It looks like you quoted it right. Is kkk created on the right machine?
I assume it works if you put the 'id ...' in a script?
Marco Wessel
Valued Contributor
Solution

Re: Running sudo on remote machine question

Because the stdout redirection ( >file) is done by the shell, which isn't running as root but as vitaly. Run the command from a script or run the command in a new shell, using bash -c.
Ivan Ferreira
Honored Contributor

Re: Running sudo on remote machine question

The problem is that after the the fist ";" all commands are different and not "sudoed". You should run sudo id; sudo date; and so.. Or as indicated above, all commands on a script.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Vitaly Karasik_1
Honored Contributor

Re: Running sudo on remote machine question

Thanks to all - you're rignt, only first command is running under root.

But, in fact, I created this three-command example just for debugging my problem - my original command was just

ssh -t vitaly@server sudo 'date >/tmp/kkk'

what is the problem here?
Ivan Ferreira
Honored Contributor

Re: Running sudo on remote machine question

Check the sudo man page:


To make a usage listing of the directories in the /home partition.
Note that this runs the commands in a sub-shell to make the cd and file redirection work.

sudo sh -c "ls > /tmp/ls.out"
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Vitaly Karasik_1
Honored Contributor

Re: Running sudo on remote machine question

Oh! I really should read manpage!
Vitaly Karasik_1
Honored Contributor

Re: Running sudo on remote machine question

manpage