StefanJanssen
StefanJanssen

Reputation: 456

Change host file with script on Mac

I'm trying to append a line to the hosts file on a mac. The command I'm using is:

sudo echo "192.168.99.100 test" >> /private/etc/hosts

This method does work on windows & linux but on Mac I do not have the permissions to run this even when running it in sudo mode.

Can anybody tell me what I'm doing wrong and how I can fix this?

StefanJanssen

Upvotes: 2

Views: 5181

Answers (1)

Candy Gumdrop
Candy Gumdrop

Reputation: 2785

Try echo '192.168.99.100 test' | sudo tee -a /private/etc/hosts.

>> is syntax of the shell itself, which is running as your user. sudo echo "192.168.99.100 test" >> /private/etc/hosts runs echo "192.168.99.100 test" as root and the >> "pipe to file" is run as your user.

tee is an ordinary command you can run as root with sudo which outputs to both stdout and a file, so echo 'line' | sudo tee -a file will do what you want. tee -a will append to the file instead of overwriting it.

Upvotes: 4

Related Questions