Pipe (|) and redirections (<, <<, >, >>) both using standard streams (stdin, stdout, stderr), but although only pipe can keep sudo privileges, why?
Works:
sudo echo "hello" | tee /root/test
Doesn't work:
sudo echo "hello" > /root/test
Pipe (|) and redirections (<, <<, >, >>) both using standard streams (stdin, stdout, stderr), but although only pipe can keep sudo privileges, why?
Works:
sudo echo "hello" | tee /root/test
Doesn't work:
sudo echo "hello" > /root/test
Pipe (|) and redirections (<, <<, >, >>) both using standard streams (stdin, stdout, stderr), but altough only pipe can keep sudo prividgles, why?
That's simply not true. You must be mixing things up
sudo echo "hello" | tee /root/test
here echo is run as root, but tee is run as your current user, which doesn't seem to be root.
This would be different
echo "hello" | sudo tee /root/test
here, the tee program is executed as root, and hence gets access to /root/test as file
Redirection (>, <, etc) and piplines (|, etc) are initialized by the parent process before any of your commands is run.
Once the parent process decides to run something, e.g. sudo ls /root | grep test, it creates two processes, setting their Standard I/O Steams (STDIN,STDOUT, STDERR) appropriately.
For the process that will tun sudo, its STDOUT is connected to the STDIN of the process that will run grep.
After this setup (using the parent process's UID:GID) the sudo and grep binaries are loaded into their processes and executed.
Programs can simply read STDIN, write STDOUT, and send errors to STDERR, and leave the "plumbing" to the parent process.
This is a major design feature of Unix/Linux. I've programmed systems with Job Control Languages that made me specify all those inter-program connections via temporary storage. Ugh.