Skip to content

Instantly share code, notes, and snippets.

@ragnarok22
Created January 26, 2022 15:02
Show Gist options
  • Save ragnarok22/a1bb62ada14f8f4ddfba7df3bafde78b to your computer and use it in GitHub Desktop.
Save ragnarok22/a1bb62ada14f8f4ddfba7df3bafde78b to your computer and use it in GitHub Desktop.
Set system proxy in Linux
#!/bin/bash
# CONSTANTS
# In case your proxy is without user and password just replace this line with
# HTTP:_PROXY="http://host:port"
# host is the proxy host. Ex: proxy.example.com or 192.168.1.50
# port is the proxy port. Ex: 3128 or 8080
HTTP_PROXY="http://user:password@host:port"
HTTPS_PROXY=$HTTP_PROXY
set_proxy(){
echo "setting proxy";
echo "setting SNAP proxy";
sudo snap set system proxy.http=$HTTP_PROXY;
sudo snap set system proxy.https=$HTTPS_PROXY;
echo "setting APT proxy";
sudo echo "Acquire::http::Proxy \"$HTTP_PROXY\";" > /etc/apt/apt.conf.d/proxy.conf
sudo echo "Acquire::https::Proxy \"$HTTPS_PROXY\";" >> /etc/apt/apt.conf.d/proxy.conf
sudo echo "Acquire::http::Proxy::mirror.uho.edu.cu DIRECT;" >> /etc/apt/apt.conf.d/proxy.conf
echo "setting NPM proxy";
npm config set --global proxy $HTTP_PROXY;
npm config set --global https-proxy $HTTPS_PROXY;
echo "setting GIT proxy";
git config --global http.proxy $HTTP_PROXY;
}
unset_proxy(){
echo "unset proxy";
echo "removing proxy from SNAP";
sudo snap unset system proxy.http;
sudo snap unset system proxy.https;
echo "removing proxy from APT";
sudo echo '' > /etc/apt/apt.conf.d/proxy.conf;
echo "removing proxy from NPM";
npm config rm --global proxy;
npm config rm --global https-proxy;
echo "removin proxy from GIT";
git config --global --unset http.proxy
}
help(){
echo type setProxy.sh on to put the proxy $HTTP_PROXY on snap, apt, npm and git;
echo type setProxy.sh off to remove the proxy from snap, apt npm and git;
}
proxy(){
case "$1" in
on) set_proxy;;
off) unset_proxy;;
help) help;;
*) echo "only on or off supported"; exit 1;
esac
}
proxy $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment