摘要: 公司一般出於安全考慮, 在同一區域網路中只有一台機器可以訪問外網,營運進行了整體的限制, 但是在後面的工作中,需要在機器上安裝一些軟體,及命令,所以其他的機器需要訪問外網來簡化工作, 但又不能打亂原有營運的設定,所以需要在能訪問外網的機器上做個代理。
第一步、二步都是在能訪問外網的機器上操作, 第三步是在不能訪問外網機器上操作。
1、檢查機器
在操作中發現訪問外網的機器本身不能使用yum 安裝軟體,故需要配置yum安裝源。如果可以的,這步可忽略。
[root@localhost test]# lsb_release -aLSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarchDistributor ID: RedHatEnterpriseServerDescription: Red Hat Enterprise Linux Server release 5.7 (Tikanga)Release: 5.7Codename: Tikanga[root@localhost test]# cd /etc/yum.repos.d/[root@localhost test]# wget http://mirrors.163.com/.help/CentOS5-Base-163.repo
中間 wget 所擷取的檔案,需要將$releasever 修改為 5
# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client. You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the # remarked out baseurl= line instead.##[base]name=CentOS-5 - Base - 163.combaseurl=http://mirrors.163.com/centos/5/os/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=osgpgcheck=1gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5#released updates [updates]name=CentOS-5 - Updates - 163.combaseurl=http://mirrors.163.com/centos/5/updates/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=updatesgpgcheck=1gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5#packages used/produced in the build but not released[addons]name=CentOS-5 - Addons - 163.combaseurl=http://mirrors.163.com/centos/5/addons/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=addonsgpgcheck=1gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5#additional packages that may be useful[extras]name=CentOS-5 - Extras - 163.combaseurl=http://mirrors.163.com/centos/5/extras/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=extrasgpgcheck=1gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5#additional packages that extend functionality of existing packages[centosplus]name=CentOS-5 - Plus - 163.combaseurl=http://mirrors.163.com/centos/5/centosplus/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=centosplusgpgcheck=1enabled=0gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5#contrib - packages by Centos Users[contrib]name=CentOS-5 - Contrib - 163.combaseurl=http://mirrors.163.com/centos/5/contrib/$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=contribgpgcheck=1enabled=0gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
yum clean all 清除原有緩衝
yum list 擷取yum列表
如果出現列表資料如下這種表示設定成功
sqlite-devel.i386 3.3.6-7 base sqlite-devel.x86_64 3.3.6-7 base squashfs-tools.x86_64 3.0-4 base squirrelmail.noarch 1.4.8-21.el5.centos base srptools.x86_64 0.0.4-10.el5 base sssd.x86_64 1.5.1-70.el5 base sssd-client.i386 1.5.1-70.el5 base sssd-client.x86_64 1.5.1-70.el5 base sssd-tools.x86_64 1.5.1-70.el5 base
2、安裝squid
squid介紹及其簡單配置
前面準備好yum命令, 可以直接線上安裝
yum install squid
安裝完成後, cd /etc/squid/ 目下, 修改squid.conf 檔案中的內容,修改之前, 可以先備份該檔案:
cp squid.conf squid.conf_bak
然後找到 檔案中的 http_access deny all 將其修改為 http_access allow all 表示所有使用者都可以訪問這個代理,
還有找到 http_port 3128 修改為 http_port 192.168.3.171:3128 這裡的IP及連接埠是 squid的代理IP及連接埠,
該IP是能訪問外網機器的IP地址,如果是本機,則可以不用修改該地址, 下面啟動squid 代理
[root@localhost squid]# squid -k parse[root@localhost squid]# [root@localhost squid]# [root@localhost squid]# squid -z2014/07/22 14:43:01| Creating Swap Directories[root@localhost squid]# [root@localhost squid]# [root@localhost squid]# service squid startStarting squid: [ OK ][root@localhost squid]# [root@localhost squid]# [root@localhost squid]# [root@localhost squid]# netstat -nltpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 4341/portmap tcp 0 0 0.0.0.0:3128 0.0.0.0:* LISTEN 22862/(squid) tcp 0 0 0.0.0.0:792 0.0.0.0:* LISTEN 4426/rpc.statd
3、配置不能訪問外網的機器代理
在其他有需要訪問外網的內網機器上設定其訪問外部網路的代理機器
export http_proxy=http://192.168.3.171:3128
export的效力僅及於該此登陸操作,非全域,詳細內容可參考:linux環境變數 export命令詳解
這樣就可以讓不能訪問外部網路的內網機器通過可以訪問外網的機器做為代理訪問到外部網路,來方便安裝一些軟體,命令等操作。
4、測試
在配置的內網機器上執行:curl www.baidu.com,出現以下內容即訪問網路成功:
[cosw@platdevsxyd01 ~]$ curl www.baidu.com<!DOCTYPE html><!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a>...</html>