Wednesday, November 5, 2014

How to get proxy working for Ubuntu 14.04 without GUI (Works for all users and sudo)

By default proxy settings aren't enabled on Ubuntu servers and I've come to find that there are syntax changes in 14.04 that are drastically different compared to their last LTS (Long Term Support) release without it being very obvious. I've crawled the web and have come across various solutions, but those solutions only fix pieces like apt-get and what I wanted is something that works across all users and all calls even when using sudo. One of the things that I figured out was the when you elevate with sudo it doesn't necessarily mean that it will inherit user variables and that was a key discovery that ultimately pointed me on the right path. Without further ado here are the steps. Keep in mind that I prefer vim for a text editor and my examples will reflect that and you can use whichever editor you are comfortable with.

1.) Update your environmental variable

The first step is to create exports in your environmental variables and you can do that by editing /etc/environment

sudo vim /etc/environment

In the file you should add your exports below the PATH= line and the syntax will look like this for each type, http, https, and ftp. (In older versions of Ubuntu you could use all_proxy which I am not sure is still in use and haven't tested for). One you edit for the exports you need, save and exit.

export http_proxy=http://yourproxyserver:proxyserverport

2.) Create your proxy file

As I've mentioned that by default Ubuntu doesn't have proxy settings enabled and this means that there ins't a proxy setting file for apt-get either. You will need to create one and if you are new to Linux, the file be will automatically created when you open a text editor by the name you intend to use for the file.

sudo vim /etc/apt/apt.conf.d/30proxy

Keep in mind that the two digit number before the file name is arbitrary, it can be any number except for ones already in use. If you want to use a number and see what's there you can run this to see what other files are in that directory;

ls /etc/apt/apt.conf.d

Moving on to editing the file, the syntax is highly specific and I would suggest copying and pasting what I have written below. Short of that, pay close attention to double quotes (" ") and semicolons (;) and be sure to close every curly bracket used. Save the file once done and off to editing sudoers file next.


3.) Update sudoers file

The sudoers file needs to be edited to allow it to not only inherit, but to also keep the environmental variables being passed to it that you specified in /etc/environment. The best way to edit the sudoers file located /etc/sudoers is to use viduso. Keep in mind unlike vi or vim it doesn't require you to type "i" to input and to save you press ctrl+x, agree with "y" and take the default .tmp file that visudo wants to use to update the sudoers file. The section that you be editing is towards the top where it lists "Defaults". There you will add additional lines equal to each environmental variable and here is the syntax for adding the http_proxy example from before;

Defaults env_keep += http_proxy

Once you are done editing we will perform some tests to find out if sudo is inheriting the environmental variables. Run the following two commands and the output should be the same for both, if sudo is missing ant of the grep searches than try to reboot first and if still the same issue than double check your syntax in previous edits.

env | grep proxy

and sudo env | grep proxy

Finally, try to run sudo apt-get update or if you have ruby installed sudo gem update and see if the updates run successfully.

No comments:

Post a Comment