element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
BeagleBoard
  • Products
  • Dev Tools
  • Single-Board Computers
  • BeagleBoard
  • More
  • Cancel
BeagleBoard
Forum BeagleBone command works as root but not with sudo
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join BeagleBoard to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 6 replies
  • Answers 1 answer
  • Subscribers 99 subscribers
  • Views 2245 views
  • Users 0 members are here
  • beagle bone black
  • beagle_bone
  • sudo
  • linux
Related

BeagleBone command works as root but not with sudo

Jan Cumps
Jan Cumps over 8 years ago

I'm tryiong to set the direction of a pin to output by streaming to it's character device.

When logging in as the user 'debian' (the account that is creeted by default in the latest debian image for BBB),

and executing the command sudo echo out > /sys/class/gpio/gpio49/direction,

I get Permision denied.

 

When switching to Root, it works.

 

 

debian@beaglebone:~$ sudo echo out > /sys/class/gpio/gpio49/direction
-bash: /sys/class/gpio/gpio49/direction: Permission denied

debian@beaglebone:~$ su root
Password:
root@beaglebone:/home/debian# echo out > /sys/class/gpio/gpio49/direction

 

What am I doing wrong?

  • Sign in to reply
  • Cancel

Top Replies

  • cstanton
    cstanton over 8 years ago in reply to Jan Cumps +2
    If my understanding is correct, the shell environment (sh) needed to be elevated so that the echo command is run within it, because otherwise the redirected output would go via the user (debian) shell…
  • bwelsby
    bwelsby over 8 years ago +1 verified
    Hi Jan, The problem is that even though you run echo with sudo the output is redirected by your command shell which hasn't got the privileges. one way around this would be : sudo sh -c "echo out > /sys…
  • Jan Cumps
    Jan Cumps over 8 years ago in reply to bwelsby +1
    or in my case, where I use bash? sudo /bin/bash -c "echo out > /sys/class/gpio/gpio49/direction"
  • bwelsby
    0 bwelsby over 8 years ago

    Hi Jan,

     

    The problem is that even though you run echo with sudo   the output is redirected by your command shell which hasn't got the privileges.

    one way around this would be :

     

    sudo sh -c "echo out > /sys/class/gpio/gpio49/direction"

    hope that makes sense.

    Brian

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 8 years ago in reply to bwelsby

    Yes!! that's it. So I was running the echo as root, but accessed the file as "myself", right?

     

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 8 years ago in reply to bwelsby

    or in my case, where I use bash?

     

    sudo /bin/bash -c "echo out > /sys/class/gpio/gpio49/direction"

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • cstanton
    0 cstanton over 8 years ago in reply to Jan Cumps

    If my understanding is correct, the shell environment (sh) needed to be elevated so that the echo command is run within it, because otherwise the redirected output would go via the user (debian) shell which doesn't have privileges to access the destination location, because of how the shell environment works.

     

    So it's as though you needed to elevate the privileges of the standard input/output of the environment so it had permissions to be passed to the destination, because even though echo itself was elevated, the environment wasn't until sh was ran with sudo.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • bwelsby
    0 bwelsby over 8 years ago in reply to cstanton

    Christopher Stanton wrote:

     

    If my understanding is correct, the shell environment (sh) needed to be elevated so that the echo command is run within it, because otherwise the redirected output would go via the user (debian) shell which doesn't have privileges to access the destination location, because of how the shell environment works.

     

    So it's as though you needed to elevate the privileges of the standard input/output of the environment so it had permissions to be passed to the destination, because even though echo itself was elevated, the environment wasn't until sh was ran with sudo.

    yes exactly.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 8 years ago in reply to Jan Cumps

    Jan Cumps - bash or sh - it doesn't matter.  Any command is elevated to user id 0 with sudo.  The redirect has to be part of the command (hence, -c ".....") as opposed to being "owned" by sudo which was executed by an ordinary user.

     

    By the way, if you are going to execute several root commands, just do

     

         sudo -i

     

    once before all of the commands.  For example,

     

         sudo -i

         apt update > log

         apt dist-upgrade >> log

         apt autoclean >> log

     

    Added benefit: No worries about the redirection belonging to the root or the ordinary user who invoked sudo.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube