1

I have the following command that correctly creates the /etc/hosts file that I want to build. However, I cannot write it per the 2nd form due to permissions, of course. So I tried variations of the 3rd form, but it still errors on permissions. Can someone give me a suggestion to try?

cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt

cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt > /etc/hosts

cat hosts.tmp ; echo -n "myremotehost " ; echo mypassword | sudo -S cat ip.txt > /etc/hosts
  • I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-) – Sadi Sep 26 '13 at 12:43
0

This was hard for me to find out! (Relative newbie to Linux.)

In terminal, type sudo -i, then log in with your user password. You will be in the root directory, with God-like powers... :>)

I also found bash filename.sh will start the script. Forgive me, I feel a great sense of accomplishment having written a script (per suggestions in another post) which killed all ads in Youtube!

-1

I would put the instructions to modify your /etc/hosts file in a shell script, say modify-hosts.sh, then execute

sudo modify-hosts.sh

If you insist on running the script without having to provide a password, then add a NOPASSWD entry specifically for this script and your username to /etc/sudoers.

As for the contents of the file, you could do:

cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts

More conveniently, if myhostname already has an entry in /etc/hosts you can edit it in place:

sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts

And that statement you can even sudo on one line

sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts

without running into redirection issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.