0

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.

1

2 Answers 2

3

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

sudo -u <username> command
Sign up to request clarification or add additional context in comments.

3 Comments

It has only one user, its root user.
I don't really understand what you are trying to do. sudo is for a (non root) user to be granted root permisions. If user is already root, then there is no difference in running with sudo or without it. As explained, you can use it for the root to run as another user. If there is no other user, then, as I understand, there is nothing sudo can do
Problem is with gem, when you install gem, shouldn't be using with sudo gem install..
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.