HlT
HlT

Reputation: 1

How to run specific command in shell script without sudo?

I running an shell script it is being run as root user with sudo privilege. It has gem install command which is also being run as sudo.

But I want to run gem install [command] without sudo

#!/usr/bin/env bash
curl -sSL https://get.rvm.io | bash -s stable
rvm install stable
gem install jekyll

It is an configuration management script for vagrant.

Upvotes: 0

Views: 3108

Answers (2)

chepner
chepner

Reputation: 532418

You should not be running the entire script with sudo; the script itself should call sudo only for those commands that need elevated privileges.

For example:

#!/usr/bin/env bash
curl -sSL https://get.rvm.io > installer  # Does not need sudo
bash installer stable                     # Maybe needs sudo?
sudo rvm install stable                   # I assume this needs sudo
gem install jekyll

Just running code directly from an external resource without verifying it, let alone running it as root, is a big security risk. You should download the code first, verify it, then pass that as an argument to your script.

#!/usr/bin/env bash
bash "$1" stable         # Again, maybe needs sudo
sudo rvm install stable
gem install jekyll

To run the script:

curl -sSL https://get.rvm.io > installer
# Check if installer is the right script and is safe
myscript installer

Upvotes: 0

jordi
jordi

Reputation: 1187

If you want to run it as an non priviledged specific user you can use

sudo -u <username> command

Upvotes: 3

Related Questions