内网服务器通过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

yaml 复制代码
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

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

3、内网服务器配置

yaml 复制代码
1、备份yum.repos.d目录下原来的repo文件
[root@i-kdscegtw yum.repos.d]#  mv ./*.repo /tmp/
yaml 复制代码
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
yaml 复制代码
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工具,生成用户名密码。

yaml 复制代码
[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、修改文件属性

yaml 复制代码
[root@fis xm]# chown squid /etc/squid/squid_users

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

yaml 复制代码
[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、内网服务器验证

yaml 复制代码
使用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-->

四、内网服务器上验证

yaml 复制代码
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命令执行也成功下载到了对应的文件


相关推荐
可儿·四系桜2 分钟前
如何在多台Linux虚拟机上安装和配置Zookeeper集群
linux·服务器·zookeeper
Flying_Fish_roe5 分钟前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian
BLEACH-heiqiyihu31 分钟前
红帽9中nginx-源码编译php
运维·nginx·php
666786661 小时前
Mysql高级篇(中)—— SQL优化
linux·运维·服务器·数据库·sql·mysql
企业管理8MSaaS1 小时前
了解CRM销售自动化:类型、优势、策略和工具
运维·自动化
宇宙第一小趴菜1 小时前
虚拟机安装xubuntu
linux·服务器·vmware
创小董2 小时前
智能机巢+无人机:自动化巡检技术详解
运维·自动化·无人机
henanxiaoman2 小时前
SaltStack自动化运维部署
运维·自动化·saltstack
Run_Snails2 小时前
hcia-openEuler V1.0师资题库-试卷3
运维·服务器·网络
翔云API2 小时前
身份证识别接口的应用场景和作用
运维·服务器·开发语言·自动化·ocr