After reading this question, I know that I have to do
sudo sh -c 'ls -hal /root/ > /root/test.out'
to avoid the "Permission denied" error.
But when I do
ssh hostname sudo sh -c 'ls -hal /root/ > /root/test.out'
from another machine, I get
bash: /root/test.out: Permission denied
Why? And how can I get this to work?
Edit: There is the line
Defaults:<username> !requiretty
before
<username> ALL= NOPASSWD: ALL
in /etc/sudoers
.
I can do
ssh remote-machine 'sudo ls /'
without any problem. So I don't think it is a tty problem. Adding -t does not solve the problem.