1

I set environment variables at .bash_profile as below for Oauth

export TWITTER_KEY=xxxxxxxxxxxxx
export TWITTER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxx

If I run just rails server without any option to boot WEBrick, ENV['TWITTER_KEY'] and ENV['TWITTER_SECRET'] can be accessed properly. But If running it with options like sudo rails server -b 127.0.0.1 -p 80, it can't. These doesn't seem to be passed to Rails.

Why?

I use

Mac OSX 10.9.5
ruby 2.1.2p95
Rails 4.2.0.beta2

Thank you, in advance.

tarky
  • 331
  • 1
  • 2
  • 13
  • Sounds like http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo – Frederick Cheung Oct 25 '14 at 07:25
  • Thanks, @FrederickCheung ! As you said, the reason is that environment variables aren't succeeded to `sudo`, because `visudo` doesn't specify the name of those variables. Now I've modified visudo and it works. – tarky Oct 25 '14 at 11:32

2 Answers2

1

I answer to own question myself. As Frederick and this page told, the reason was that environment variables aren't succeeded to sudo, because visudo doesn't specify the name of those variables.

So I added the lines below to visudo, and it works.

Defaults        env_keep += "TWITTER_KEY"
Defaults        env_keep += "TWITTER_SECRET"
Community
  • 1
  • 1
tarky
  • 331
  • 1
  • 2
  • 13
0

Some step by step instructions:

  1. Open up your bash profile vim ~/.bash_profile
  2. Add the environment variable to the file export TWITTER_KEY=xxxxxxxxxxxxx
  3. Open up the sudoers config file sudo visudo
  4. Add the following line to the file exactly as so Defaults env_keep +="TWITTER_KEY"

Now when you run the command with sudo it should work as desired, with TWITTER_KEY set to xxxxxxxxxxxxx.

Kyle Chadha
  • 2,719
  • 1
  • 24
  • 37