Home > Bash, GNU/Linux > Bash http_proxy: from a user environment to sudo one

Bash http_proxy: from a user environment to sudo one

Hi. Sometimes you can’t connect directly to internet, because you have to go through a proxy (i.e. working environment).
Did you ever have to set up an http proxy on linux shell in order to (i.e) download a new package or manually update your distribution with a packet manager?
If so, you need to be a superuser. If you use the “sudo” command, you will probably stumbled across the inability to export variables from the user environment to the “sudo” one.
Something like this:

$ sudo export ...
sudo: export: command not found

I’ll start from the beginning: how to set up a proxy on linux shell (in this post I assume that you use the bash shell).
In order to set up an http proxy you have to write the following command:

$ http_proxy="http://username:password@host:port"
$ export http_proxy

These simple commands create a variable (http_proxy) and set it to “http://username:password@host:port”. Where:

– username: your username to access to the proxy;
– password: your password to access to the proxy;
– domain: the proxy’s host (i.e. or foo.asd.it);
– port: the proxy’s port.

In line 2, we “export” the “http_proxy” variable, in such way that any subsequent program that we’ll run can use that.
If the proxy doesn’t have an autentication access you can simply write:

$ http_proxy="http://host:port"
$ export http_proxy

Obviously you can write the two commands in a only one line:

$ export http_proxy="http://domain:port"

You can set also a ftp proxy, using the “ftp_proxy” variable:

$ export ftp_proxy="http://domain:port"

This is all very nice, but there is a little problem: if you run any subsequent program as “sudo” user, you may not use the variable, because the environment is different from that in which the variable has been defined. Indeed if you try to download a new packet using “apt-get”(or any other packet manager), you need to run it as superuser: so you have to use the “sudo” command. If you try that, you will notice that the program (apt-get) can’t connect to the WAN (World Area Network) and fails.
If you don’t want to login as root ($ sudo -i) every time you need to use the http_proxy variable in “super user mode”, you can simply add the following line to the “/etc/sudoers” file:

Defaults env_keep = "http_proxy ftp_proxy"

You must add the line above this line:

Defaults        env_reset

We “keep” the variables “http_proxy” and “ftp_proxy” because we want to export only this two to the “sudo” environment. If you want, you can only export “http_proxy” or add another one.
You can easily modify the sudoers file running this command:

$ sudo visudo

That’s all! I hope this will be useful as it was to me.

Categories: Bash, GNU/Linux Tags: , , ,
  1. procfs
    January 20, 2011 at 05:42


  2. Toptnc
    September 15, 2011 at 09:59

    Thanks. You save my day :)

  3. ghost79
    January 19, 2012 at 00:11

    Brilliant explanation! Works for OS X 10.6.8 running darwin 10.8.0 as well.

  4. April 24, 2013 at 03:17

    We stumbled over here from a different website and thought I may as well check things out.

    I like what I see so now i am following you.
    Look forward to exploring your web page for a second time.

  5. April 26, 2013 at 10:14

    Hello! I know this is kinda off topic however , I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
    My website discusses a lot of the same subjects as yours and I think
    we could greatly benefit from each other. If you happen to be interested feel free to
    shoot me an email. I look forward to hearing from you! Fantastic blog by the way!

  6. April 28, 2013 at 23:14

    Good day! I know this is kind of off topic but I was wondering
    which blog platform are you using for this website? I’m getting sick and tired of WordPress because I’ve had problems with
    hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.

  7. June 14, 2013 at 18:34

    Hey! This post could not be written any better!
    Reading through this post reminds me of my previous room mate!
    He always kept chatting about this. I will forward this page to him.
    Fairly certain he will have a good read. Thanks for sharing!

  8. September 13, 2013 at 06:23

    I adore this specific blog. It’s for me a secret haven where I always
    learn something that interests me.

  9. July 12, 2014 at 06:02

    Howdy! Someone in my Myspace group shared this site with us so I came to give it a look.
    I’m definitely loving the information. I’m book-marking and
    will be tweeting this to my followers! Superb blog and amazing design.

  10. Salamat
    June 6, 2017 at 16:10

    Very helpful information. Thank you for posting.

  1. May 10, 2018 at 19:41

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: