Linux系统通过Squid配置实现代理上网_linux使用代理上网squite,2024年最新吊打面试官-Linux运维中高级面试题

参数解释:
./configure:检查你的系统编译器是否可用
–preifx:指定安装路径
–enable-arp-acl:可以在规则中设置为直接通过客户端MAC进行管理,防止客户端使用IP欺骗
–enable-linux-netfilter:使用内核过滤
–enable-linux-tproxy:支持透明模式
–enable-async-io=100:异步I/O,提升存储性能,相当于 --enable-pthreads --enable-storeio=ufs,aufs
–enable-err-language=“Simplify_Chinese”:报错时显示的语音,这里指定为Chinese
–enable-underscore:允许URL中有下划线
–enable-poll:使用Poll()模式,提升性能
–enable-gnuregex:使用GUN正则表达式

make && make install



useradd -M -s /sbin/nologin squid
chown -R squid.squid /usr/local/squid/var
ln -s /usr/local/squid/sbin/squid /usr/local/sbin/


## 初始化并启动Squid


添加squid运行的用户及组



echo ‘cache_effective_user squid’ >> /usr/local/squid/etc/squid.conf
echo ‘cache_effective_group squid’ >> /usr/local/squid/etc/squid.conf


初始化缓存目录



[root@host-10-200-86-163 /]# squid -z
2019/08/08 17:04:40| Created PID file (/usr/local/squid/var/run/squid.pid)
[root@host-10-200-86-163 /]# 2019/08/08 17:04:40 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2019/08/08 17:04:40 kid1| Creating missing swap directories
2019/08/08 17:04:40 kid1| No cache_dir stores are configured.
2019/08/08 17:04:40| Removing PID file (/usr/local/squid/var/run/squid.pid)


启动Squid



[root@host-10-200-86-163 /]# squid
[root@host-10-200-86-163 /]# ss -anplt | grep 3128
LISTEN 0 128 :::3128 ::😗 users:((“squid”,pid=6304,fd=10)


查看Squid的运行用户



[root@host-10-200-86-163 /]# ps -ef|grep squid
root 6302 1 0 17:05 ? 00:00:00 squid
squid 6304 6302 0 17:05 ? 00:00:00 (squid-1) --kid squid-1
squid 6305 6304 0 17:05 ? 00:00:00 (logfile-daemon) /usr/local/squid/var/logs/access.log
root 6322 30305 0 17:06 pts/7 00:00:00 grep --color=auto squid


## 创建服务启动脚本



vim /etc/init.d/squid
#!/bin/bash
#chkconfig: 2345 90 25
PID=“/usr/local/squid/var/run/squid.pid”
CONF=“/usr/local/squid/etc/squid.conf”
CMD=“/usr/local/squid/sbin/squid”

case “$1” in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo “squid is running”
else
echo “正在启动 squid…”
$CMD
fi
;;
stop)
$CMD -k shutdown &> /dev/null #这里可以仔细看下
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -natp | grep squid
else
echo “squid is not running”
fi
;;
restart)
$0 stop &> /dev/null
echo “正在关闭 squid…”
$0 start &> /dev/null
echo “正在启动 squid…”
$CMD
fi
;;
stop)
$CMD -k shutdown &> /dev/null #这里可以仔细看下
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -natp | grep squid
else
echo “squid is not running”
fi
;;
restart)
$0 stop &> /dev/null
echo “正在关闭 squid…”
$0 start &> /dev/null
echo “正在启动 squid…”
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo “用法:$0{start|stop|status|reload|check|restart}”
;;
esac


加入开机启动



chmod +x /etc/init.d/squid
chkconfig --add squid
chkconfig --level 35 squid on


脚本测试



[root@host-10-200-86-163 init.d]# netstat -anplt | grep squid
[root@host-10-200-86-163 init.d]# service squid start
正在启动 squid…
[root@host-10-200-86-163 init.d]# netstat -anplt | grep squid
tcp6 0 0 :::3128 ::😗 LISTEN 8260/(squid-1)
[root@host-10-200-86-163 init.d]# service squid stop
[root@host-10-200-86-163 init.d]# netstat -anplt | grep squid


## 创建传统代理


主要修改下图中所圈出的内容  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200722104017135.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA3OTc3,size_16,color_FFFFFF,t_70)



And finally deny all other access to this proxy

http_access allow all #在deny all前添加allow all
http_access deny all

Squid normally listens to port 3128

http_port 3128 #squid对外端口
cache_mem 128 MB #指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB #允许用户下载的最大文件大小,以字节为单位。默认设置0表示不进行限制
maximum_object_size 4096 KB #允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户

Uncomment and adjust the following to add a disk cache directory.

#cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256

Leave coredumps in the first cache dir

coredump_dir /usr/local/squid/var/cache/squid

Add any of your own refresh_pattern entries above these.

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|?) 0 0% 0
refresh_pattern . 0 20% 4320
cache_effective_user squid #squid运行用户
cache_effective_group squid #squid运行组


重启Squid



#进行配置检查
[root@host-10-200-86-163 init.d]# /usr/local/squid/sbin/squid -k reconfigure
[root@host-10-200-86-163 init.d]# /usr/local/squid/sbin/squid -k check

#重启
[root@host-10-200-86-163 init.d]# service squid restart
正在关闭 squid…
正在启动 squid…

[root@host-10-200-86-163 init.d]# netstat -anplt | grep 3128
tcp6 0 0 :::3128 ::😗 LISTEN 8774/(squid-1)


**设置Linux服务器内网上网** 重新找一台内网的linux服务器 没有设置代理上网前,去curl百度是失败的



[root@sx-sj-mcn-redis-1 ~]# curl www.baidu.com -I
curl: (6) Could not resolve host: www.baidu.com; Unknown error


**临时设置代理**



[root@sx-sj-mcn-redis-1 ~]# export proxy=http://10.200.86.163:3128; #proxy=http代理http协议的请求
[root@sx-sj-mcn-redis-1 ~]# export http_proxy=“http://10.200.86.163:3128”;
[root@sx-sj-mcn-redis-1 ~]# export https_proxy=“http://10.200.86.163:3128”; #https=proxy代理https协议的请求


临时设置代理后再次curl百度



[root@sx-sj-mcn-redis-1 ~]# curl www.baidu.com -I
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Content-Length: 277
Content-Type: text/html
Date: Thu, 08 Aug 2019 12:40:01 GMT
ETag: “575e1f60-115”
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
X-Cache: MISS from host-10-200-86-163
Via: 1.1 host-10-200-86-163 (squid/4.8)
Connection: keep-alive


**永久设置代理**



#在/etc/profile中全局设置的最后添加以下配置
[root@sx-sj-mcn-vgateway-2 ~]# vim /etc/profile
export proxy=http://10.200.86.163:3128
export http_proxy=“http://10.200.86.163:3128”
export https_proxy=“http://10.200.86.163:3128”
export ftp_proxy=“http://10.200.86.163:3128”

[root@sx-sj-mcn-vgateway-2 ~]# source /etc/profile

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

img
img

tps_proxy=“http://10.200.86.163:3128”
export ftp_proxy=“http://10.200.86.163:3128”

[root@sx-sj-mcn-vgateway-2 ~]# source /etc/profile

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

[外链图片转存中…(img-uMObI0vV-1727249975549)]
[外链图片转存中…(img-ti6GPwLZ-1727249975550)]

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值