0

If I try

pkexec echo -e '\nwl2k something\n' >> /etc/ax25/axports 

in terminal to have a user add a line to a file, I get a

bash: /etc/ax25/axports: Permission denied

How do I make it so the whole command goes to pkexec - that is, ask user for sudo to make the change?

NoBugs
  • 56

1 Answers1

0
pkexec echo -e '\nwl2k something\n' >> /etc/ax25/axports
bash: /etc/ax25/axports: Permission denied

Output redirects have the permissions of the user who invoked the shell, not the command that produced the output. See https://unix.stackexchange.com/a/6714/411962.

Instead pipe the echo output to pkexec tee -a file to append it to a file requiring elevated permissions: echo -e '\nwl2k something\n' | pkexec tee -a /etc/ax25/axports

fuzzydrawrings
  • 1,656
  • 5
  • 12
  • 2
    The question seems to be using "ask user for sudo to make the change" as a shorthand to "request authentication to run the command with administrative privileges". The echo command needs no special privileges, but writing to a specific file does. So echo -e '\nwl2k something\n' | pkexec tee -a /etc/ax25/axports is what the question seems to be looking for. – telcoM Jan 26 '22 at 07:04
  • @telcoM You're correct of course. I just saw the >> redirect to a root-owned location, which of course never works, and gave the sudo tee method as the fix. I'll edit the answer. – fuzzydrawrings Jan 26 '22 at 18:58