1

In my CMS (Rails 4), I need to let a user make CRUD processes on CIFS mount points. In order to have a permanent CIFS mount on a CentOS (6) system, I need to edit the /etc/fstab file and update the mount list with the sudo mount -a command. When I try to open /etc/fstab file with File.open:

File.open("/etc/fstab", "a") do |f|
  f.puts "\n test"
end

I get this error:

Errno::EACCES: Permission denied - /etc/fstab

It's normal because /etc/fstab file belongs to the root user. Is there a way to open a file as super user? At the other hand, I'm open to different ideas. My requirement is to edit the fstab file from inside of my application.

sawa
  • 165,429
  • 45
  • 277
  • 381
scaryguy
  • 7,720
  • 3
  • 36
  • 52

2 Answers2

1

See this answer in the ruby forum:

Start your script using

sudo ruby myscript.rb

You can't gain root privileges mid-script, short of

system("sudo ruby anotherscript.rb")
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
0

This one is tricky but you could add your Web user to the sudoers list and submit the sudo password when trying to edit the file. Make sure your connection is secured via https

lsaffie
  • 1,764
  • 1
  • 17
  • 22