I was trying to use an alias which should run two commands, where the first is fine to be run with normal user privileges and the second one needs sudo privileges.
alias hosts-get="scp [email protected]:/etc/hosts /tmp/ && sudo cat /tmp/hosts >> /etc/hosts"
The usecase is to pull the hosts file of a remote server and then append it to the hosts file on my own computer (the remote server contains all the IP to domain information of client systems).
However, when running the alias I get zsh: permission denied: /etc/hosts
, so it looks like the sudo is being ignored or rather zsh can't interpret it or something.
After some searching around I changed the alias to this:
alias hosts-get="scp [email protected]:/etc/hosts /tmp/ && cat /tmp/hosts | sudo tee -a /etc/hosts"
This looks to work correctly. Can someone please explain the background of this?