How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable

Posted on in Categories , , , , , , , , , , , , , last updated June 3, 2014

I‘m behind a squid proxy server. How do I access internet via proxy server when I use wget, lynx and other utilities from a shell prompt on a Linux or Unix-like systems?

Linux and UNIX-like systems has environment variable called http_proxy. It allows you to connect text based session and/or applications via the proxy server. All you need is proxy server IP address (URL) and port values. This variable is almost used by all utilities such as elinks, lynx, wget, curl and others commands.

Set http_proxy shell variable on Linux/OS X/Unix bash shell

Type the following command to set proxy server:
$ export http_proxy=http://server-ip:port/
$ export http_proxy=http://127.0.0.1:3128/
$ export http_proxy=http://proxy-server.mycorp.com:3128/

If the proxy server requires a username and password then add these to the URL. For example, to include the username foo and the password bar:
$ export http_proxy=http://foo:[email protected]:port/
$ export http_proxy=http://foo:[email protected]:3128/
$ export http_proxy=http://USERNAME:[email protected]:3128/

How do I setup proxy variable for all users?

To setup the proxy environment variable as a global variable, open /etc/profile file:
# vi /etc/profile
Add the following information:
export http_proxy=http://proxy-server.mycorp.com:3128/
OR
export http_proxy=http://USERNAME:[email protected]:3128/
Save and close the file.

A note about enabling proxy access to a specific user

To enable proxy access for a specific user, add the lines user shell profile. For the default bash shell, the profile is the file .bash_profile. For example, enable proxy access for a specifc user called vivek, type:
$ vi $HOME/.bash_profile
OR
# vi /home/vivek/.bash_profile
Append the following line:
export http_proxy=http://USERNAME:[email protected]:3128/
Save and close the file.

How do I use password protected proxy server using various command line options?

You can simply use wget command as follows:
$ wget --proxy-user=USERNAME --proxy-password=PASSWORD http://path.to.domain.com/some.html
Lynx command has the following syntax:
$ lynx -pauth=USER:PASSWORD http://domain.com/path/html.file
Curl command has following syntax:
$ curl --proxy-user user:password http://url.com/

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

42 comment

    1. My proxy login id contains ‘@’ what should I do now?
      eg.
      export http_proxy=http://abc:[email protected]@192.168.25.10:3128/
      Is there anyway to sort this out?

      P.S – I cannot request to change my password since It is associated with many other services.

  1. Hi

    I tried this, but still, I cannot ping google or any other site from within the shell.. it says ‘destination host unreachable’..

    I’m using Redhat Enterprise 2.4..

    Any idea what might be going wrong?

    Thanks
    Pravin

  2. Is there support for excluding sites or a regex from using the proxy?

    It seems that there is options for some commands, but is there a centralized option?

    I.E. something like:

    export no_http_proxy=*.ibm.com,10.0.0.0\24,…

  3. Hey People,

    I got a doubt. My password is have a special character ‘@’. Due to this the character before the ‘@’ in my password are ignored. Can anyone include ‘@’ in your password and let me know if that works.

    Thanks,
    SP

  4. Hello!
    I need to configure complete internet access behind a proxy.
    I can access 80 port, but i need 443 and all ports for Skype for example.
    Thanks for anyone who can help me.

    Thanks

  5. Hi guys,How can i remove the proxy from elinks?i was accessing a server that needed validation and that worked fine,then when i went to a no proxy server,i couldint access interned due to earlier proxy setting,please help me with the command or file to remove the proxy settings.

  6. I just curious and my password doesn’t work because it has a character ‘@’ it generates error says unable to resolve host address especially using wget. How do i overcome it?Thanks

  7. cd /etc
    sudo gedit bash.bashrc

    once gedit opens the file in text form,goto the end and type the following,edit if already exists:

    export http_proxy=http://:@:8080/
    export ftp_proxy=http://:@:/

    example: lets say your username is simpson , password is bartrocks, the proxy server for your connection is http://10.1.1.32 ,port address is 8080, then to set up http proxy you have to write:

    export http_proxy=http://simpson:[email protected]:8080/

    Then Save and exit as you do for text files.Close and reopen the terminal and now you can use tools like wget to download web-pages from terminal :)

  8. cd /etc
    sudo gedit bash.bashrc

    once gedit opens the file in text form,goto the end and type the following,edit if already exists:

    export http_proxy=http://username:[email protected]:8080/
    export ftp_proxy=http://username:[email protected]:port/

    example: lets say your username is simpson , password is bartrocks, the proxy server for your connection is http://10.1.1.32 ,port address is 8080, then to set up http proxy you have to write:

    export http_proxy=http://simpson:[email protected]:8080/

    Then Save and exit as you do for text files.Close and reopen the terminal and now you can use tools like wget to download web-pages from terminal :)

  9. I was trying to set permanently environment variable for proxy in /etc/profile as mentioned in guide. But it wont work even on reboot.
    I explored and figured out that users ~/.bashrc file is used for permanent save environment variable.

  10. Hello folks,

    I’m using WebSphere Application server 8.0 which is hosted in the linux server, How can I enable the proxy so that the Applicaition server picks up the proxy settings

Leave a Comment