内网服务器通过squid代理访问外网

一、背景

现在要对172.16.58.158服务器进行openssh升级操作,我用之前写好的升级脚本执行后,发现没有备份旧的ssh程序文件,然后还卸载了oenssl-devel,然后我发现其他服务器ssh该服务器失败。同时脚本执行时报错“ configure: error: *** zlib.h missing - please install first or check config.log ” 我查看这个错误需要安装openssl-devel和zlib相关的包,但是我这台服务器本身就不通外网,而且没有内网的yum源,scp和rz等命令都因为没有ssh已经用不了了。此时此刻,手已经开始抖了,我排查服务器表后,发现同一网段有一台172.16.58.52服务器可以ping通外网,也可从外网wget下载文件,命好,找到了一台通外网的服务器,因此想到了squid代理,做一个代理给内网服务器用,因此有了这篇文章。

二、操作

1、安装squid

172.16.58.52这台服务器能通外网,因此再这台服务器上进行安装
[root@fis xm]# ping www.baidu.com
[root@fis xm]# yum install -y squid
#cd到配置文件目录下
[root@fis xm]# cd  /etc/squid/
#备份原始配置文件
[root@fis xm]# cp squid.conf squid.conf_bak
#修改配置文件
[root@fis xm]# vi squid.conf
#将http_access deny all注释修改为http_access allow all
#http_access deny all
http_access allow all
# 修改端口为代理的端口
http_port 19191

2、启动squid

[root@fis xm]# squid -k parse          #检查语法是否错误
[root@fis xm]# squid -z    #初始化缓存空间
[root@fis xm]# service squid start && ss -lntp |grep 19191  #启动squid并检查19191端口是否开启

3、内网服务器配置

1、备份yum.repos.d目录下原来的repo文件
[root@i-kdscegtw yum.repos.d]#  mv ./*.repo /tmp/
2、打开profile文件,添加以下文件配置
[root@i-kdscegtw yum.repos.d]# vim /etc/profile
[root@i-kdscegtw yum.repos.d]# 
export http_proxy=http://172.16.58.52:19191
export https_proxy=http://172.16.58.52:19191
export no_proxy="127.0.0.1, localhost, 172.16.58.158,172.16.58.52"

[root@i-kdscegtw yum.repos.d]#  source /etc/profile
3、因为使用yum和wget功能,所以还需要添加以下配置
[root@i-kdscegtw yum.repos.d]#  vim /etc/yum.conf
[main]
#无密码形式
proxy=http://172.16.58.52:19191
[root@i-kdscegtw yum.repos.d]# vim /etc/wgetrc
http_proxy=http://172.16.58.52:19191

三、squid配置密码(补充知识)

1、使用htpasswd工具,生成用户名密码。

[root@fis xm]# yum install httpd-tools
[root@fis xm]# htpasswd -c /etc/squid/squid_user ops_test
New password: ops
Re-type new password: ops
Adding password for user ops_test

2、修改文件属性

[root@fis xm]# chown squid /etc/squid/squid_users

3、修改squid配置文件/etc/squid/squid.conf,添加认证相关的配置

[root@fis xm]#  vim /etc/squid/squid.conf
# Insert your own rules here to allow access from your clients

# http_access allow localhost 加注释,表示localhost也需要认证

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/squid_user
auth_param basic children 5
auth_param basic realm Proxy Authentication Required
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive on

acl authUsers proxy_auth ops_test
http_access allow authUsers

http_access deny all

[root@fis xm]#  systemctl restart squid  #修改外配置重启

3、内网服务器验证

使用curl测试Squid配置的用户名密码
不使用用户名密码认证,访问失败,返回401
curl https://www.baidu.com


使用用户名密码认证,访问成功
curl -x http_proxy://ops_test:ops@172.16.58.52:19191 www.baidu.com
<!DOCTYPE html>
<!--STATUS OK-->

四、内网服务器上验证

1、在内网服务器上执行以下命令
因为第一步就已经移除掉了内网服务器yum.repos.d目录下的所有repo文件,因此,下载新的阿里云源repo文件
看是否能下载成功

[root@i-kdscegtw yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

[root@i-kdscegtw yum.repos.d]# 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
如下图1所示,wget下载文件成功,说明了内网服务器现在可以访问外网下载文件了。
如图2所示,yum命令执行也成功下载到了对应的文件

在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值