Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run commands as root from Rails?

I'm thinking about writing a Rails application to manage servers like cPanel.

The hardest part is to select the best way to run commands that require root privileges, like adduser, or doing things that require another user's privileges, like changing nginx config files.

I know only two ways to achieve this:

  1. I can write a standalone daemon that will be run as root and do all the work, receiving commands through IPC or something like this.
  2. Run Rail's server as the root user, and do anything from it or execute commands/bash scripts from it.

Which of these ways are best? Does there exist another way to to this?

like image 908
Drakmail Avatar asked May 25 '13 21:05

Drakmail


People also ask

How do I run a command in Rails?

2.1 bin/rails serverGo to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

How do I create a controller in Rails?

To generate a controller and optionally its actions, do the following: Press Ctrl twice and start typing rails g controller. Select rails g controller and press Enter . In the invoked Add New Controller dialog, specify the controller name.

How do you undo a command in Rails?

To undo a rails generate command, run a rails destroy command. You can then edit the file and run rake db:migrate again. (See how to roll back a Migration file to rollback a specific migration or multiple migrations.)


1 Answers

sudo -i

I tried doing things like sudo bundle exec rails console but it wouldn't quite run it as root so when I tried to create a directory from the console it let me know that I did not have the correct permissions.

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

Hopes that helps others.

like image 157
Joshua Pinter Avatar answered Sep 28 '22 14:09

Joshua Pinter