Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:17:23+00:00 2026-06-12T05:17:23+00:00

Often I find myself needing to write scripts that have to execute some portions

  • 0

Often I find myself needing to write scripts that have to execute some portions as a normal user and other portions as a super user. I am aware of one similar question on SO where the answer was to run the same script twice and execute it as sudo, however that is not sufficient for me. Some times I need to revert to being a normal user after a sudo operation.

I have written the following in Ruby to do this

#!/usr/bin/ruby
require 'rubygems'
require 'highline/import'
require 'pty'
require 'expect'

def sudorun(command, password)
  `sudo -k`
  PTY.spawn("sleep 1; sudo -u root #{command} 2>&1") { | stdin, stdout, pid |
  begin
    stdin.expect(/password/) {
    stdout.write("#{password}\n")
    puts stdin.read.lstrip
                              }
  rescue Errno::EIO
  end
 }
end

Unfortunately, using that code if the user enters the wrong password the script crashes. Ideally it should give the user 3 tries to get the sudo password right. How do I fix this?

I am running this on Linux Ubuntu BTW.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:17:24+00:00Added an answer on June 12, 2026 at 5:17 am

    In my opinion, running a script that does stuff internally with sudo is wrong. A better approach is to have the user run the whole script with sudo, and have the script fork lesser-privileged children to do stuff:

    # Drops privileges to that of the specified user
    def drop_priv user
      Process.initgroups(user.username, user.gid)
      Process::Sys.setegid(user.gid)
      Process::Sys.setgid(user.gid)
      Process::Sys.setuid(user.uid)
    end
    
    # Execute the provided block in a child process as the specified user
    # The parent blocks until the child finishes.
    def do_as_user user
      unless pid = fork
        drop_priv(user)
        yield if block_given?
        exit! 0 # prevent remainder of script from running in the child process
      end
      puts "Child running as PID #{pid} with reduced privs"
      Process.wait(pid)
    end
    
    at_exit { puts 'Script finished.' }
    
    User = Struct.new(:username, :uid, :gid)
    user = User.new('nobody', 65534, 65534)
    
    do_as_user(user) do
      sleep 1 # do something more useful here
      exit! 2 # optionally provide an exit code
    end
    
    puts "Child exited with status #{$?.exitstatus}"
    puts 'Running stuff as root'
    sleep 1
    
    do_as_user(user) do
      puts 'Doing stuff as a user'
      sleep 1
    end
    

    This example script has two helper methods. #drop_priv takes an object with username, uid, and gid defined and properly reduces the permissions of the executing process. The #do_as_user method calls #drop_priv in a child process before yielding to the provided block. Note the use of #exit! to prevent the child from running any part of the script outside of the block while avoiding the at_exit hook.

    Often overlooked security concerns to think about:

    • Inheritance of open file descriptors
    • Environment variable filtering
    • Run children in a chroot?

    Depending on what the script is doing, any of these may need to be addressed. #drop_priv is an ideal place to handle all of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself needing reference to an object that is several objects away,
I often find myself needing to write functions to load/save from/to ASCII (or similar)
I often find myself writing R scripts that generate a lot of output. I
I often find myself needing to write code with the following logical pattern: $foo
I often find myself asserting that an object isKindOfClass of some class in Objective-C.
I often find myself needing a tool that would allow me to: search for
I find myself often needing to use int.TryParse() to test if a value is
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself with a list of disconnected Linq2Sql objects or keys that
I often find myself with a file that has one number per line. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.