ssh and redirection

hi,

i was using with success the following rsh statement :

rsh server -l user1 "> /var/spool/mail/user1"

to clear all the mail by remote

now i try to do the same with ssh without success :

ssh -t newuser@server  "sudo -u user1 '> /var/spool/mail/user1'"

i'm not authorized to access "user1" by ssh so i use the ssh keys created by newuser

i have the following error :

sudo: > /var/spool/mail/user1: command not found

regards
Christian

this looks like a fault with the second part of your command, try removing the single quotes from around your redirection.

i believe this removes the special meaning of the > character so instead of redirecting output it sees that as a command to be run by the user1.

but this is what i want :

connecting by SSH using sudo :

ssh -t newuser@server "sudo -u user1 

then running the ">" to erase the mail file :

> /var/spool/mail/user1

with the rsh this ">" works but any others commands like : touch or echo "" didn't have the permissions.

regards
Christian

i mean do exactly the same but just remove the single quotes so:

ssh -t newuser@server  "sudo -u user1 > /var/spool/mail/user1"

In that case it is trying to create the file by redirecting the sudo :!
ksh: cannot create /var/spool/mail/notes03: Permission denied

i want to use the ">" as a command to erase the file because we have the rights to do it.

regards
Christian

then maybe try:

ssh -t newuser@server  "sudo -u user1 -i > /var/spool/mail/user1"

the -i option opening a new shell for the user reading the .profile and executing the command provided.

can you try this

ssh -t newuser@server  "sudo -u user1 echo -n > /var/spool/mail/user1"

thanks for your perseverance !

the command is ok but it does nothing at all , surprising !

# ssh -t newuser@server  "sudo -u user1 echo -n /var/spool/mail/user1"
# ssh -t newuser@server  "sudo -u user1 ls -al /var/spool/mail/user1" 
-rw-rw-r-- 1 user1 mail 804915 Oct  6 16:13 /var/spool/mail/user1

the file is still full

regards
Christian

ssh -t newuser@server  "sudo -u user1 cp /dev/null /var/spool/mail/user1"

Might work

That may delete and recreate the file and leave the old file still open hogging disk, unfixable without rebooting the daemon or server! Don't do it!

---------- Post updated at 08:38 AM ---------- Previous update was at 08:36 AM ----------

The reason none of the commands using redirection worked is that the redirection happens before the su. So you don't get permission to truncate the file.

To get permissions to run the file, the command to truncate the file will need to run in a shell with permissions to do so.

Try this:

echo ": > /var/spool/mail/user1" | ssh -t newuser@server sudo -u user1 /bin/sh

This should run : > /var/spool/mail/user1 in a shell belonging to user1.

Well the "cp /dev/null" is ok but seems to be dangerous...

and the last solution gives this result

thanks for your help

Christian

where is redirection sign?

# ssh -t newuser@server  "sudo -u user1 echo -n > /var/spool/mail/user1"

same result with the ">"

regards
Christian

Putting that in quotes instead of quote tags means when I try and quote you, the error message disappears. Use code tags for code.

"no askpass program" means it needs to ask you for a password and can't because it's not in a terminal. Run it in a terminal.

---------- Post updated at 09:12 AM ---------- Previous update was at 09:11 AM ----------

This won't work, again, because the redirection happens before the sudo. The shell doesn't have permissions to overwrite the file, and will fail to redirect into it.

---------- Post updated at 09:14 AM ---------- Previous update was at 09:12 AM ----------

Perhaps:

ssh -t newuser@server echo "': > /var/spool/mail/user1'" '|' sudo -u user1 /bin/sh

you are right but if newuser hasnot a entry in sudoers then your example also is not solution to me.
in this case sudo is not required and sudo does not get extra rights on files.
maybe `su` is can be usable instead of sudo.

ssh -tt newuser@server "su -l user1 -c 'echo -n "">/var/spool/mail/user1'"

Thanks for all , it works :

ssh -t newuser@server echo "': > /var/spool/mail/user1'" '|' sudo -u user1 /bin/sh

regards
Christian

I had a poke around, but apparently my GoogleFu is weak today. Do you have a link that explains the issues surrounding this?

Also, would

cat /dev/null > file

have the same potential problem (sudo/redirect issues aside)?

(apologies for drifting off-topic)

I can do better than that, I can give you a working example:

## Demo of deleting a logfile in use
$ echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ rm filename # delete filename.
$ ls -l filename # The directory entry will be gone -
ls: cannot access filename: No such file or directory
cat <&5 # ...but the file still exists, since we have it open.
the owls are not what they seem
$ exec 5<&- # Only on close will it TRULY be deleted from disk.

## Demo of truncating a file in use
echo "the owls are not what they seem" > filename # Create a file
$ exec 5<filename # Open filename for reading, into FD 5
$ : > filename # truncate filename
$ ls -l filename # The same file exists, with zero size
-rw-r--r-- 1 username users 0 Oct  7 11:40 filename
$ cat <&5 # Read from the file.  Nothing there.
$ exec 5<&- # close the file.

cp might overwrite a file, or might delete and recreate it, I wouldn't depend on either -- implementations of things can vary. But a shell redirect > always truncates.

Also, would cat /dev/null > file

have the same potential problem (sudo/redirect issues aside)?[/code] Nope. cat /dev/null is effectively the same statement as : -- a statement that prints no output. It's the > that's important.

I used to use echo > filename to truncate things until I realized echo does indeed print slightly more than nothing there -- it prints one blank line.

This is entirely on-topic.