How to solve “permission denied” when using sudo with redirection in Bash?












126















When using sudo to allow edits to files, I regularly get 'permission denied'.



For example, my mouse is jittery and sluggish, so I want to disable polling:



sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf


I'm prompted for a password, and then get:



bash: /etc/modprobe.d/local.conf: Permission denied


So I tried to do a temporary change to disable polling by using:



sudo echo N> /sys/module/drm_kms_helper/parameters/poll


Yet again the system responded with:



bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied


Any ideas?










share|improve this question





























    126















    When using sudo to allow edits to files, I regularly get 'permission denied'.



    For example, my mouse is jittery and sluggish, so I want to disable polling:



    sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf


    I'm prompted for a password, and then get:



    bash: /etc/modprobe.d/local.conf: Permission denied


    So I tried to do a temporary change to disable polling by using:



    sudo echo N> /sys/module/drm_kms_helper/parameters/poll


    Yet again the system responded with:



    bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied


    Any ideas?










    share|improve this question



























      126












      126








      126


      28






      When using sudo to allow edits to files, I regularly get 'permission denied'.



      For example, my mouse is jittery and sluggish, so I want to disable polling:



      sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf


      I'm prompted for a password, and then get:



      bash: /etc/modprobe.d/local.conf: Permission denied


      So I tried to do a temporary change to disable polling by using:



      sudo echo N> /sys/module/drm_kms_helper/parameters/poll


      Yet again the system responded with:



      bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied


      Any ideas?










      share|improve this question
















      When using sudo to allow edits to files, I regularly get 'permission denied'.



      For example, my mouse is jittery and sluggish, so I want to disable polling:



      sudo echo "options drm_kms_helper poll=N">/etc/modprobe.d/local.conf


      I'm prompted for a password, and then get:



      bash: /etc/modprobe.d/local.conf: Permission denied


      So I tried to do a temporary change to disable polling by using:



      sudo echo N> /sys/module/drm_kms_helper/parameters/poll


      Yet again the system responded with:



      bash: /sys/module/drm_kms_helper/parameters/poll: Permission denied


      Any ideas?







      command-line bash sudo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 18 '18 at 14:21









      Ciro Santilli 新疆改造中心 六四事件 法轮功

      10.2k44751




      10.2k44751










      asked Dec 19 '12 at 4:12









      JackJack

      1,13921521




      1,13921521






















          6 Answers
          6






          active

          oldest

          votes


















          142














          Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root



          sudo -i


          Then you can use redirection



          echo N> /sys/module/drm_kms_helper/parameters/poll


          Otherwise you can run bash string with sudo



          sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"





          share|improve this answer





















          • 1





            You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

            – saji89
            Dec 19 '12 at 4:41













          • you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

            – shantanu
            Dec 19 '12 at 4:55






          • 4





            Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

            – saji89
            Dec 19 '12 at 4:59











          • You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

            – kojiro
            Dec 19 '12 at 13:48













          • If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

            – Harsha
            Feb 26 '17 at 13:12



















          73














          The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:




          • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line


          • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"


          • echo, running with root privileges, prints the string to its standard output.


          • echo command terminates, superuser shell exits, sudo terminates


          • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.



          For the ways to fix this see @shantanu answer.





          (*) - while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo ... command. When opening the file fails the shell doesn't even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).



          Here's a quick test:



          $ touch ./onlyroot.txt
          $ sudo chown root:root ./onlyroot.txt
          $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt
          bash: onlyroot.txt: Permission denied


          In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word "root". However, when the output redirection fails in the calling shell, "who.txt" file is also missing because the command was not invoked.






          share|improve this answer


























          • More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

            – Panos Rontogiannis
            May 30 '16 at 9:32








          • 1





            @PanosRontogiannis: thanks, I've updated the answer

            – Sergey
            Jun 6 '16 at 20:35



















          57














          Adding to Shantanu's answer:



          ... Or you could use a tee command like this:



          sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10


          or if its a command's output:



          echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll





          share|improve this answer





















          • 3





            +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

            – l0b0
            Dec 19 '12 at 16:41






          • 1





            Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

            – Fabian Tamp
            Oct 27 '15 at 7:46



















          14














          An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:




          To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.




          $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"





          share|improve this answer































            3














            Another option is to use a temporary file. This is useful in a bash script.



            temp=$(mktemp)
            echo "Hello, world!" > $temp
            sudo cp $temp /etc/wherever





            share|improve this answer































              3














              sudo dd of=



              To append as you want:



              echo inbytes | sudo dd of=outfile oflag=append conv=notrunc


              or to recreate the file from scratch:



              echo inbytes | sudo dd of=outfile


              Advantages:




              • nicer than tee since no /dev/null redirection

              • nicer than sh since no explicit subshell (but an implicit one for the redirection)


              • dd has many powerful options, e.g. status=progress to see transfer progress


              Works because sudo forwards stdin to the command.






              share|improve this answer





















              • 1





                This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                – Eliah Kagan
                Oct 24 '17 at 16:00











              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%2f230476%2fhow-to-solve-permission-denied-when-using-sudo-with-redirection-in-bash%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              142














              Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root



              sudo -i


              Then you can use redirection



              echo N> /sys/module/drm_kms_helper/parameters/poll


              Otherwise you can run bash string with sudo



              sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"





              share|improve this answer





















              • 1





                You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

                – saji89
                Dec 19 '12 at 4:41













              • you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

                – shantanu
                Dec 19 '12 at 4:55






              • 4





                Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

                – saji89
                Dec 19 '12 at 4:59











              • You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

                – kojiro
                Dec 19 '12 at 13:48













              • If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

                – Harsha
                Feb 26 '17 at 13:12
















              142














              Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root



              sudo -i


              Then you can use redirection



              echo N> /sys/module/drm_kms_helper/parameters/poll


              Otherwise you can run bash string with sudo



              sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"





              share|improve this answer





















              • 1





                You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

                – saji89
                Dec 19 '12 at 4:41













              • you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

                – shantanu
                Dec 19 '12 at 4:55






              • 4





                Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

                – saji89
                Dec 19 '12 at 4:59











              • You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

                – kojiro
                Dec 19 '12 at 13:48













              • If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

                – Harsha
                Feb 26 '17 at 13:12














              142












              142








              142







              Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root



              sudo -i


              Then you can use redirection



              echo N> /sys/module/drm_kms_helper/parameters/poll


              Otherwise you can run bash string with sudo



              sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"





              share|improve this answer















              Output redirection (via the > operator) is done by the shell, not by echo. You have to login as root



              sudo -i


              Then you can use redirection



              echo N> /sys/module/drm_kms_helper/parameters/poll


              Otherwise you can run bash string with sudo



              sudo bash -c "echo N> /sys/module/drm_kms_helper/parameters/poll"






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 19 '12 at 14:10









              Andrea Corbellini

              12.3k24566




              12.3k24566










              answered Dec 19 '12 at 4:22









              shantanushantanu

              4,681125189




              4,681125189








              • 1





                You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

                – saji89
                Dec 19 '12 at 4:41













              • you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

                – shantanu
                Dec 19 '12 at 4:55






              • 4





                Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

                – saji89
                Dec 19 '12 at 4:59











              • You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

                – kojiro
                Dec 19 '12 at 13:48













              • If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

                – Harsha
                Feb 26 '17 at 13:12














              • 1





                You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

                – saji89
                Dec 19 '12 at 4:41













              • you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

                – shantanu
                Dec 19 '12 at 4:55






              • 4





                Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

                – saji89
                Dec 19 '12 at 4:59











              • You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

                – kojiro
                Dec 19 '12 at 13:48













              • If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

                – Harsha
                Feb 26 '17 at 13:12








              1




              1





              You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

              – saji89
              Dec 19 '12 at 4:41







              You can't run echo with sudo? the what about the result I got: saji@laptop:~$ sudo echo "Hi" [sudo] password for saji: Hi

              – saji89
              Dec 19 '12 at 4:41















              you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

              – shantanu
              Dec 19 '12 at 4:55





              you can write on file, echo "something" > somewhre. It's using pipe.. That is the problem.

              – shantanu
              Dec 19 '12 at 4:55




              4




              4





              Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

              – saji89
              Dec 19 '12 at 4:59





              Ok, if that's the case, then please update your answer to reflect that running echo is a problem in that case only.

              – saji89
              Dec 19 '12 at 4:59













              You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

              – kojiro
              Dec 19 '12 at 13:48







              You can't simply run the shell builtin echo as sudo, unless you do something like sudo bash -c 'echo …'; however, POSIX systems usually supply an external echo command such as /bin/echo on OS X, which sudo can execute without rigamarole. Thus, the echo command you usually run and the echo command you run with sudo are probably two different, but similar commands.

              – kojiro
              Dec 19 '12 at 13:48















              If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

              – Harsha
              Feb 26 '17 at 13:12





              If that is the case, why did the answers to this question suggest echo? askubuntu.com/questions/840431/…

              – Harsha
              Feb 26 '17 at 13:12













              73














              The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:




              • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line


              • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"


              • echo, running with root privileges, prints the string to its standard output.


              • echo command terminates, superuser shell exits, sudo terminates


              • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.



              For the ways to fix this see @shantanu answer.





              (*) - while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo ... command. When opening the file fails the shell doesn't even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).



              Here's a quick test:



              $ touch ./onlyroot.txt
              $ sudo chown root:root ./onlyroot.txt
              $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt
              bash: onlyroot.txt: Permission denied


              In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word "root". However, when the output redirection fails in the calling shell, "who.txt" file is also missing because the command was not invoked.






              share|improve this answer


























              • More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

                – Panos Rontogiannis
                May 30 '16 at 9:32








              • 1





                @PanosRontogiannis: thanks, I've updated the answer

                – Sergey
                Jun 6 '16 at 20:35
















              73














              The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:




              • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line


              • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"


              • echo, running with root privileges, prints the string to its standard output.


              • echo command terminates, superuser shell exits, sudo terminates


              • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.



              For the ways to fix this see @shantanu answer.





              (*) - while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo ... command. When opening the file fails the shell doesn't even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).



              Here's a quick test:



              $ touch ./onlyroot.txt
              $ sudo chown root:root ./onlyroot.txt
              $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt
              bash: onlyroot.txt: Permission denied


              In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word "root". However, when the output redirection fails in the calling shell, "who.txt" file is also missing because the command was not invoked.






              share|improve this answer


























              • More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

                – Panos Rontogiannis
                May 30 '16 at 9:32








              • 1





                @PanosRontogiannis: thanks, I've updated the answer

                – Sergey
                Jun 6 '16 at 20:35














              73












              73








              73







              The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:




              • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line


              • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"


              • echo, running with root privileges, prints the string to its standard output.


              • echo command terminates, superuser shell exits, sudo terminates


              • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.



              For the ways to fix this see @shantanu answer.





              (*) - while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo ... command. When opening the file fails the shell doesn't even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).



              Here's a quick test:



              $ touch ./onlyroot.txt
              $ sudo chown root:root ./onlyroot.txt
              $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt
              bash: onlyroot.txt: Permission denied


              In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word "root". However, when the output redirection fails in the calling shell, "who.txt" file is also missing because the command was not invoked.






              share|improve this answer















              The output redirection is done by the shell from which the command has been invoked. So, breaking everything into bits, here what is happening*:




              • shell invokes sudo echo "options drm_kms_helper poll=N", which executes sudo command with echo "options drm_kms_helper poll=N" command line


              • sudo asks for a password, opens superuser shell and invokes echo "options drm_kms_helper poll=N", which runs echo command passing it "options drm_kms_helper poll=N"


              • echo, running with root privileges, prints the string to its standard output.


              • echo command terminates, superuser shell exits, sudo terminates


              • the shell from which the command has been invoked collects the output and tries to redirect it to /etc/modprobe.d/local.conf, which is writeable only by root. It gets "permission denied" error.



              For the ways to fix this see @shantanu answer.





              (*) - while the above sequence helps to understand why the command fails, in reality things happen somewhat out-of-order: the original shell notices the redirection and tries to open the file for writing before invoking the sudo ... command. When opening the file fails the shell doesn't even invoke the command which was supposed to write to the file (thanks to @PanosRontogiannis for pointing this out).



              Here's a quick test:



              $ touch ./onlyroot.txt
              $ sudo chown root:root ./onlyroot.txt
              $ sudo bash -c "whoami | tee who.txt" > onlyroot.txt
              bash: onlyroot.txt: Permission denied


              In the test above the whoami | tee who.txt was going to create a file named who.txt containing the word "root". However, when the output redirection fails in the calling shell, "who.txt" file is also missing because the command was not invoked.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 6 '16 at 20:35

























              answered Dec 19 '12 at 5:19









              SergeySergey

              36.6k98799




              36.6k98799













              • More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

                – Panos Rontogiannis
                May 30 '16 at 9:32








              • 1





                @PanosRontogiannis: thanks, I've updated the answer

                – Sergey
                Jun 6 '16 at 20:35



















              • More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

                – Panos Rontogiannis
                May 30 '16 at 9:32








              • 1





                @PanosRontogiannis: thanks, I've updated the answer

                – Sergey
                Jun 6 '16 at 20:35

















              More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

              – Panos Rontogiannis
              May 30 '16 at 9:32







              More likely the shell 'forks' itself and tries to open /etc/modprobe.d/local.conf before trying to 'exec' sudo which means that the first 4 steps your describe never actually happen because the file cannot be opened.

              – Panos Rontogiannis
              May 30 '16 at 9:32






              1




              1





              @PanosRontogiannis: thanks, I've updated the answer

              – Sergey
              Jun 6 '16 at 20:35





              @PanosRontogiannis: thanks, I've updated the answer

              – Sergey
              Jun 6 '16 at 20:35











              57














              Adding to Shantanu's answer:



              ... Or you could use a tee command like this:



              sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10


              or if its a command's output:



              echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll





              share|improve this answer





















              • 3





                +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

                – l0b0
                Dec 19 '12 at 16:41






              • 1





                Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

                – Fabian Tamp
                Oct 27 '15 at 7:46
















              57














              Adding to Shantanu's answer:



              ... Or you could use a tee command like this:



              sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10


              or if its a command's output:



              echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll





              share|improve this answer





















              • 3





                +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

                – l0b0
                Dec 19 '12 at 16:41






              • 1





                Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

                – Fabian Tamp
                Oct 27 '15 at 7:46














              57












              57








              57







              Adding to Shantanu's answer:



              ... Or you could use a tee command like this:



              sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10


              or if its a command's output:



              echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll





              share|improve this answer















              Adding to Shantanu's answer:



              ... Or you could use a tee command like this:



              sudo tee /sys/module/drm_kms_helper/parameters/poll <<<10


              or if its a command's output:



              echo 10 | sudo tee /sys/module/drm_kms_helper/parameters/poll






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 19 '12 at 15:50









              Brendan Long

              36419




              36419










              answered Dec 19 '12 at 6:50









              UntitledUntitled

              9282915




              9282915








              • 3





                +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

                – l0b0
                Dec 19 '12 at 16:41






              • 1





                Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

                – Fabian Tamp
                Oct 27 '15 at 7:46














              • 3





                +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

                – l0b0
                Dec 19 '12 at 16:41






              • 1





                Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

                – Fabian Tamp
                Oct 27 '15 at 7:46








              3




              3





              +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

              – l0b0
              Dec 19 '12 at 16:41





              +1 logging in as root is a bad idea for manual work, and a really bad idea for scripted tasks.

              – l0b0
              Dec 19 '12 at 16:41




              1




              1





              Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

              – Fabian Tamp
              Oct 27 '15 at 7:46





              Also, sudo tee /sys/module/drm_kms_helper/parameters/poll > /dev/null if you don't want it printing to stdout as well.

              – Fabian Tamp
              Oct 27 '15 at 7:46











              14














              An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:




              To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.




              $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"





              share|improve this answer




























                14














                An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:




                To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.




                $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"





                share|improve this answer


























                  14












                  14








                  14







                  An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:




                  To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.




                  $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"





                  share|improve this answer













                  An approach I haven't seen mentioned here is to simply execute the entire commandline in its own shell. The sudo manpage itself gives an example of this approach:




                  To make a usage listing of the directories in the /home partition. Note that this runs the commands in a sub-shell to make the cd and file redirection work.




                  $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 19 '12 at 13:53









                  kojirokojiro

                  26118




                  26118























                      3














                      Another option is to use a temporary file. This is useful in a bash script.



                      temp=$(mktemp)
                      echo "Hello, world!" > $temp
                      sudo cp $temp /etc/wherever





                      share|improve this answer




























                        3














                        Another option is to use a temporary file. This is useful in a bash script.



                        temp=$(mktemp)
                        echo "Hello, world!" > $temp
                        sudo cp $temp /etc/wherever





                        share|improve this answer


























                          3












                          3








                          3







                          Another option is to use a temporary file. This is useful in a bash script.



                          temp=$(mktemp)
                          echo "Hello, world!" > $temp
                          sudo cp $temp /etc/wherever





                          share|improve this answer













                          Another option is to use a temporary file. This is useful in a bash script.



                          temp=$(mktemp)
                          echo "Hello, world!" > $temp
                          sudo cp $temp /etc/wherever






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 29 '14 at 15:51









                          user545424user545424

                          1455




                          1455























                              3














                              sudo dd of=



                              To append as you want:



                              echo inbytes | sudo dd of=outfile oflag=append conv=notrunc


                              or to recreate the file from scratch:



                              echo inbytes | sudo dd of=outfile


                              Advantages:




                              • nicer than tee since no /dev/null redirection

                              • nicer than sh since no explicit subshell (but an implicit one for the redirection)


                              • dd has many powerful options, e.g. status=progress to see transfer progress


                              Works because sudo forwards stdin to the command.






                              share|improve this answer





















                              • 1





                                This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                                – Eliah Kagan
                                Oct 24 '17 at 16:00
















                              3














                              sudo dd of=



                              To append as you want:



                              echo inbytes | sudo dd of=outfile oflag=append conv=notrunc


                              or to recreate the file from scratch:



                              echo inbytes | sudo dd of=outfile


                              Advantages:




                              • nicer than tee since no /dev/null redirection

                              • nicer than sh since no explicit subshell (but an implicit one for the redirection)


                              • dd has many powerful options, e.g. status=progress to see transfer progress


                              Works because sudo forwards stdin to the command.






                              share|improve this answer





















                              • 1





                                This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                                – Eliah Kagan
                                Oct 24 '17 at 16:00














                              3












                              3








                              3







                              sudo dd of=



                              To append as you want:



                              echo inbytes | sudo dd of=outfile oflag=append conv=notrunc


                              or to recreate the file from scratch:



                              echo inbytes | sudo dd of=outfile


                              Advantages:




                              • nicer than tee since no /dev/null redirection

                              • nicer than sh since no explicit subshell (but an implicit one for the redirection)


                              • dd has many powerful options, e.g. status=progress to see transfer progress


                              Works because sudo forwards stdin to the command.






                              share|improve this answer















                              sudo dd of=



                              To append as you want:



                              echo inbytes | sudo dd of=outfile oflag=append conv=notrunc


                              or to recreate the file from scratch:



                              echo inbytes | sudo dd of=outfile


                              Advantages:




                              • nicer than tee since no /dev/null redirection

                              • nicer than sh since no explicit subshell (but an implicit one for the redirection)


                              • dd has many powerful options, e.g. status=progress to see transfer progress


                              Works because sudo forwards stdin to the command.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 24 '17 at 15:51

























                              answered May 28 '17 at 6:39









                              Ciro Santilli 新疆改造中心 六四事件 法轮功Ciro Santilli 新疆改造中心 六四事件 法轮功

                              10.2k44751




                              10.2k44751








                              • 1





                                This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                                – Eliah Kagan
                                Oct 24 '17 at 16:00














                              • 1





                                This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                                – Eliah Kagan
                                Oct 24 '17 at 16:00








                              1




                              1





                              This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                              – Eliah Kagan
                              Oct 24 '17 at 16:00





                              This is good. We think of dd as how we overwrite our once-great filesystems, and don't realize it's for mundane tasks too--and that other commands as root also cause great harm if used on on the wrong files/devices. Like sudo tee, sudo dd will of course also work with here strings, e.g., sudo dd of=outfile <<<'hello world'. [Thanks for editing. NB with sh -c 'cmd', sh is a subprocess that's a shell, but not really a subshell except in the sense all external commands begin as one.]

                              – Eliah Kagan
                              Oct 24 '17 at 16:00


















                              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%2f230476%2fhow-to-solve-permission-denied-when-using-sudo-with-redirection-in-bash%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

                              GameSpot

                              connect to host localhost port 22: Connection refused

                              Getting a Wifi WPA2 wifi connection