Ubuntu - “sudo iptables” command works in terminal, doesn't work in bash script












1















I'm using a simple terminal command that adds a rule to iptables and it executes without problems:



sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT



When I run that command in a bash script, it returns sudo: ./script.sh: command not found



The script looks like this:



#!/bin/bash

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT









share|improve this question









New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Is script.sh executable? chmod +x script.sh

    – Thomas
    1 hour ago











  • Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

    – Marin Leontenko
    1 hour ago













  • @MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

    – multithr3at3d
    52 secs ago
















1















I'm using a simple terminal command that adds a rule to iptables and it executes without problems:



sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT



When I run that command in a bash script, it returns sudo: ./script.sh: command not found



The script looks like this:



#!/bin/bash

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT









share|improve this question









New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Is script.sh executable? chmod +x script.sh

    – Thomas
    1 hour ago











  • Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

    – Marin Leontenko
    1 hour ago













  • @MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

    – multithr3at3d
    52 secs ago














1












1








1








I'm using a simple terminal command that adds a rule to iptables and it executes without problems:



sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT



When I run that command in a bash script, it returns sudo: ./script.sh: command not found



The script looks like this:



#!/bin/bash

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT









share|improve this question









New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm using a simple terminal command that adds a rule to iptables and it executes without problems:



sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT



When I run that command in a bash script, it returns sudo: ./script.sh: command not found



The script looks like this:



#!/bin/bash

sudo iptables -A OUTPUT -p tcp -m string --string "facebook.com" --algo kmp -j REJECT






linux scripting sudo






share|improve this question









New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 53 mins ago









Jeff Schaller

44.4k1162143




44.4k1162143






New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Marin LeontenkoMarin Leontenko

82




82




New contributor




Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Marin Leontenko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    Is script.sh executable? chmod +x script.sh

    – Thomas
    1 hour ago











  • Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

    – Marin Leontenko
    1 hour ago













  • @MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

    – multithr3at3d
    52 secs ago














  • 1





    Is script.sh executable? chmod +x script.sh

    – Thomas
    1 hour ago











  • Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

    – Marin Leontenko
    1 hour ago













  • @MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

    – multithr3at3d
    52 secs ago








1




1





Is script.sh executable? chmod +x script.sh

– Thomas
1 hour ago





Is script.sh executable? chmod +x script.sh

– Thomas
1 hour ago













Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

– Marin Leontenko
1 hour ago







Apparently it wasn't. It works after I changed permission. I thought all scripts are executable by default. Thanks!

– Marin Leontenko
1 hour ago















@MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

– multithr3at3d
52 secs ago





@MarinLeontenko a script is just another file. By that logic, all files would be executable by default.

– multithr3at3d
52 secs ago










1 Answer
1






active

oldest

votes


















1














If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.



sudo /bin/sh ./script.sh


To run the script as inteded by OP you have to give the script execution rights.



chmod +x script.sh
sudo ./script.sh


In this case the script is executed with the shell of the shebang line.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    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: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    });


    }
    });






    Marin Leontenko is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509721%2fubuntu-sudo-iptables-command-works-in-terminal-doesnt-work-in-bash-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.



    sudo /bin/sh ./script.sh


    To run the script as inteded by OP you have to give the script execution rights.



    chmod +x script.sh
    sudo ./script.sh


    In this case the script is executed with the shell of the shebang line.






    share|improve this answer




























      1














      If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.



      sudo /bin/sh ./script.sh


      To run the script as inteded by OP you have to give the script execution rights.



      chmod +x script.sh
      sudo ./script.sh


      In this case the script is executed with the shell of the shebang line.






      share|improve this answer


























        1












        1








        1







        If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.



        sudo /bin/sh ./script.sh


        To run the script as inteded by OP you have to give the script execution rights.



        chmod +x script.sh
        sudo ./script.sh


        In this case the script is executed with the shell of the shebang line.






        share|improve this answer













        If a script is not executable, one has to call it with an appropriate shell like /bin/sh or /bin/bash. This will ignore the shebang line and the script will be executed with the calling shell.



        sudo /bin/sh ./script.sh


        To run the script as inteded by OP you have to give the script execution rights.



        chmod +x script.sh
        sudo ./script.sh


        In this case the script is executed with the shell of the shebang line.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 40 mins ago









        ThomasThomas

        4,07561430




        4,07561430






















            Marin Leontenko is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Marin Leontenko is a new contributor. Be nice, and check out our Code of Conduct.













            Marin Leontenko is a new contributor. Be nice, and check out our Code of Conduct.












            Marin Leontenko is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f509721%2fubuntu-sudo-iptables-command-works-in-terminal-doesnt-work-in-bash-script%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

            Ottavio Pratesi

            Tricia Helfer

            15 giugno