Linux Proxy Settings – Kind of SSO? :-)

Posted on October 25, 2016

0



Set proxy only at login and unset at logout

On some Linux Servers I must reach to the Internet through web proxy with user credentials and i dont have a so called service user everytime.
Generally I must set the wget and apt-get and the system wide proxy everytime I login and delete before logout. Every time edit the 3 settings is simple timeconsuming…
I put that task in bashrc and bash_logout hidden files to make it quickly every time.
It saves your ass too when you let your password there and it expires but the server tries to use it further and you realize on another system that you cannot login anymore since your account locked out according to the local policy. :-)

.bashrc –  Add the proxy settings to wget to apt (in case of ubuntu) and system wide with variables:

# Set the fuckin proxy
echo -n "Proxy Username:"
read PROXYUSER 

echo -n "Proxy Password:"
read -s PROXYPASSWD

SERVER=proxy.mycompany.com
PORTNUM=80
PROXY="http://$PROXYUSER:$PROXYPASSWD@$SERVER:$PORTNUM"
export http_proxy=$PROXY
export https_proxy=$PROXY
export ftp_proxy=$PROXY

# enable wget proxy
echo "use_proxy = on" | sudo tee --append /etc/wgetrc > /dev/null

for pname in http https ftp;
do
        proxyname=$pname"_proxy"
        # set wget proxy credentials
        echo "$proxyname=$PROXY" | sudo tee --append /etc/wgetrc > /dev/null
        # apt proxy credentials
        echo "Acquire::$pname::Proxy \""$PROXY"\";" | sudo tee --append /etc/apt/apt.conf > /dev/null
done 

unset PROXY
unset PROXYUSER
unset PROXYPASSWD

.bash_logout –  here just delete the settings, but if you are not a root, it will ask for a password for sudo.

# delete the fuckin proxy
unset http_proxy
unset ftp_proxy
unset https_proxy

#delete wget proxy credentials
grep -v \[http\|https\|ftp\]_proxy /etc/wgetrc > tmp_wgetrc; sudo mv tmp_wgetrc /etc/wgetrc
grep -v "^use_proxy = on" /etc/wgetrc > tmp_wgetrc; sudo mv tmp_wgetrc /etc/wgetrc

#delete apt proxy credentials
grep -v Proxy /etc/apt/apt.conf > tmp_apt.conf; sudo mv tmp_apt.conf /etc/apt/apt.conf

Advertisement
Posted in: Linux, proxy, Security