Script to Update Host File












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









share|improve this question














bumped to the homepage by Community 13 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • 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
















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









share|improve this question














bumped to the homepage by Community 13 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • 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














1












1








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









share|improve this question














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






scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 26 '13 at 12:14









EdgeCaseEdgeCase

2861616




2861616





bumped to the homepage by Community 13 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 13 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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



















  • 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

















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





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










2 Answers
2






active

oldest

votes


















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!






share|improve this answer

































    -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.






    share|improve this answer

























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f350299%2fscript-to-update-host-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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!






      share|improve this answer






























        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!






        share|improve this answer




























          0












          0








          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!






          share|improve this answer















          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!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 13 '15 at 9:45









          hg8

          9,839125591




          9,839125591










          answered Jun 1 '15 at 4:17









          B. FifeB. Fife

          247




          247

























              -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.






              share|improve this answer






























                -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.






                share|improve this answer




























                  -1












                  -1








                  -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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 26 '13 at 12:54

























                  answered Sep 26 '13 at 12:41









                  zwetszwets

                  8,29922340




                  8,29922340






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f350299%2fscript-to-update-host-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      數位音樂下載

                      When can things happen in Etherscan, such as the picture below?

                      格利澤436b