Permission denied while writing to /etc/hosts file

Hi, I am trying to add hosts to /etc/hosts file using ioutil.Writefile fucntion. But I get permission denied error.
err := ioutil.WriteFile(fileName, hfData, 0644) This line throws open /etc/hosts: permission denied error. Can anyone guide how to overcome this problem?

Thanks in advance!

As which user to you execute this code?

Thank you, I just write go code and run so, a user who can use sudo and have root access. But the when I issue go run, it does not ask for root password.

Then execute the Go code using sudo.

$ sudo go run code.go

Oh, but the program is not that small, I thought running entire code as sudo is not the best way to do. Is there anyway, I can use the root priviledge only during this function call?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.