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
sudo applies to the complete command line.
– Paul_Pedant
Aug 29 '23 at 08:29
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.
sudo ls /root | grep test, how parent process can know the output before sudo ls /root is ran?
– linuxer
Aug 29 '23 at 16:30
xargs. Do: xargs --show-limits
– waltinator
Aug 31 '23 at 00:33
cmd1 | cmd2. The parent shell sets up the connection between cmd1's STDOUT and cmd2s STDIN. Both processes must exist for this to happen. After this connection is made, the parent shell has no more to do with it. If cmd1 writes STDOUT constanly, and cmd2 does not read from STDIN, evrntually, cmd1 will fill its buffers, and will "block" (become temporarily unrunnable) until cmd2 reads its STDIN. In this fashion, data flows, in lockstep from command to command all the way down the pipeline. Sorry this is long.
– waltinator
Sep 14 '23 at 05:52
bash shell, explore https://www.gnu.org/software/bash/ You can even download and read rhe source.
– waltinator
Sep 14 '23 at 05:59
ls is a bit fishy since it's not able to output all filenames unambiguously
– ilkkachu
Sep 14 '23 at 07:01
cmd1...": Why cmd1 fill its buffer in that case? cmd2 is in the same RAM I think. And what blocks cmd1 in that case? The kernel? "in lockstep"?
– linuxer
Sep 17 '23 at 02:42
sudo echo "hello" | tee /root/testdoesn't work to access the file if sudo is needed for that. In each of the cases, consider which program is opening the file? – ilkkachu Aug 28 '23 at 19:29