Over a million developers have joined DZone.

Runas - Run A Program Under A Specific User / Group (poor Man's Suexec)

·
#!/usr/bin/env ruby

# runas - Run another program under the privileges of a specified user and group.
# This is necessary because sudo demands a password, as we need it to be hands off.
# A poor man's suexec basically.

require 'etc'

user, group, cmd = ARGV

begin
  uid = Etc.getpwnam(user).uid
  gid = Etc.getgrnam(group).gid

  unless Process.euid == uid && Process.egid == gid
    Process.initgroups(user, gid)
    Process::GID.change_privilege(gid)
    Process::UID.change_privilege(uid)
  end

  exec cmd
rescue
  puts "Could not run as #{user}:#{group}"
  exit 1
end

Usage example: ./runas username groupname "sleep 10"
Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}