How to run commands as root from Rails?

I am thinking of writing a Rails application for managing servers like cPanel.

The most difficult task is to choose the best way to run commands that require root privileges, for example adduser , or to do what other user privileges require, for example, modify nginx configuration files.

I know only two ways to achieve this:

  • I can write a stand-alone daemon that will run as administrator and do all the work, receive commands through IPC or something like that.
  • Run the Rail server as the root user and do something from it or run the / bash scripts from it.

Which of these methods is better? Is there any other way of doing this?

+4
source share
2 answers

You need to do # 2.

Run the application with administrator rights, and you can perform all operations with administrator rights.

Writing code to run as a daemon will also work, but it's harder to debug.

In any case, you need to protect against hacking attempts through unauthorized access. One miss and your system will be compromised.

+2
source

sudo -i

I tried to do things like sudo bundle exec rails console , but it would not run it as root, so when I tried to create a directory from the console, it informed me that I did not have the correct permissions.

However, using sudo -i , it enters you into the "interactive console" as the root sudo user, and then you can run bundle exec rails console as the root sudo user.

Hopes that help others.

+3
source

Source: https://habr.com/ru/post/1482761/