内网服务器通过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命令执行也成功下载到了对应的文件


相关推荐
Avan_菜菜18 小时前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB2 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
zzzzzz3103 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode3 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220704 天前
如何搭建本地yum源(上)
运维
大树887 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠7 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质7 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz7 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工7 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信