Execute alias with sudo at session startup I created the following alias and put it into my ~/.zshrc:

alias kiosk-proxy="echo \"rdr pass inet proto tcp from any to any port 80 -&gt; 127.0.0.1 port 8080\" | sudo pfctl -ef -"


It's basically a pf rule which redirects all incoming traffic with the destination port 80 to 127.0.0.1:8080 (but what it does is not important, it's just to give you some context)

Currently, I have to manually run this at every session startup, I'd like to automate it.

It would be rather simple if it didn't need sudo rights, I'd just put kiosk-proxy in my ~/.bash_profile, I guess. 

But since it needs sudo, it's gonna ask for the password and I doubt it's gonna prompt it. So... How can I execute this alias with sudo rights without asking for the password? Also, it needs to know there is such alias existing in ~/.zshrc, which may not have been loaded yet. Worse case, I can move the alias to another file.
Enabling pf (with the rdr rule) with a command/alias or a shell script/function in your bash/zsh profile like in your question is neither common nor good practice. It is possible though:

To disable a password prompt modify the sudoers file:

Open Terminal and enter sudo visudo to modify the file /etc/sudoers.

Change the part:

## User privilege specification
##
root ALL=(ALL) ALL
%admin  ALL=(ALL) ALL


to

## User privilege specification
##
root ALL=(ALL) ALL
%admin  ALL=(ALL) ALL
your_username ALL=(ALL) NOPASSWD: /sbin/pfctl


and save the file.

If you don't know vi: after entering sudo visudo you have to change to insert mode by hitting i. Enter the additional line as indicated above. To leave insert mode hit esc. Then enter :wq and the Enter key to write the modified file to disk and quit vi.

bash:

Entering kiosk-proxy shouldn't require a password anymore. You can then simply add kiosk-proxy as a separate line in your bash_profile (after the alias kiosk-proxy ...line)

After exiting the shell, pf won't be disabled and the passed rule is still redirecting! To disable pf and the rdr rule while exiting the (bash) shell, create a file with touch ~/.bash_logout and the content sudo pfctl -d. 

zsh:

The same works with ~/.zprofile containing:

alias kiosk-proxy="echo \"rdr pass inet proto tcp from any to any port 80 -&gt; 127.0.0.1 port 8080\" | sudo pfctl -ef -"

kiosk-proxy


and ~/.zlogout containing

sudo pfctl -d




A more recommended way is to simply add the rdr rule in pf.conf and enable pf while booting.