一个CentOS7的开发环境部署,包括防火墙|VPN|HTTP代理服务器设置等

这是AmazonBigSpider的环境准备记录。

一.基础安装

先从 CentOS 下载镜像

选择中文,安装模式图形界面,标注(图形界面消耗的内存大概600M,硬盘3.7G),我的硬盘是300G,请划盘如下:

95G /home 
200G / 
4000 swap   
250 /boot

以上/boot不可改,其他随便,可以不选swap,建议/划大一点,示例如下:

设置账号,密码,然后安装完重启登陆:

查看硬盘大小,内存大小:

free -h
df -h

二.机器网络设置

进入图形界面,请一定在图形界面里设置,然后你就可以弃用,改用命令行了。

1.时间同步

先设置时间同步(貌似失效,哈哈哈):

timedatectl 
timedatectl set-ntp true

linux服务器时间校准方法

首先安装NTP:

yum install -y ntpdate

然后执行NTP命令:

ntpdate time.windows.com

想要偷懒,让机器自动校准时间的话,只需要将命令加入到cron就可以了。

crontab -e
* */1 * * * /sbin/ntpdate time.windows.com #每隔一个小时同步一次

保存退出即可。

http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html

2.设置网络

查看网卡状态:

ip addr 查看网卡状态

填入IP和网关:

禁掉IP6:

查看配置,命令行手工的在这里改:

cat /etc/sysconfig/network-scripts/ifcfg-eno16777728

这个时候还上不了网!请手动修改DNS(永生生效)

vim /etc/resolv.conf

修改网关!

vim /etc/sysconfig/network

主机名请改掉

3.禁掉虚拟网卡

有虚拟网卡的要先禁用虚拟网卡,可能有,为什么禁,不知道!

virsh net-list
virsh net-destroy default
virsh net-undefine default
systemctl restart libvirtd.service

三.防火墙设置

#先检查是否安装了iptables
service iptables status

#安装iptables
yum install -y iptables

#升级iptables
yum update iptables 

#安装iptables-services
yum install iptables-services

#停止firewalld服务
systemctl stop firewalld

#禁用firewalld服务
systemctl mask firewalld

#查看iptables现有规则
iptables -L -n

#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
#开放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放5902端口(VCN)
iptables -A INPUT -p tcp --dport 5902 -j ACCEPT
#开放5901端口(VNC root)
iptables -A INPUT -p tcp --dport 5901 -j ACCEPT
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#开放3306端口(MYSQL)
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
iptables -P INPUT DROP
#所有出站一律绿灯
iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
iptables -P FORWARD DROP

#保存上述规则
service iptables save

#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service

#开启服务
systemctl start iptables.service

#查看状态
systemctl status iptables.service

#重启
systemctl restart iptables.service

http://blog.chinaunix.net/uid-26495963-id-3279216.html

http://www.linuxso.com/linuxpeixun/10332.html

vim /etc/sysconfig/iptables

四.安装VNC虚拟主机远程连接

VNC分为服务端和客户端,linux服务器主机需要安装vncserver,centos7下一般使用tigervnc。

yum install -y tigervnc-server

安装完毕后需要配置,配置vnc-server,进入目录

cd /lib/systemd/system
ls

我们会看到有个service叫做vncserver@.service,这就是我们需要的vnc服务。但是需要对它进行配置才可以使用。假设我们当前为root用户配置远程桌面,配置流程如下: 首先,复制该service,命名为vncserver@:1.service

cp vncserver@.service vncserver@:1.service

然后修改vncserver@:1.service

vim vncserver@:1.service

这里需要且只需要做一种替换:将替换为需要配置的用户。注意因为root的home目录就是/root/,而不是/home/root/,所以替换后文本如下:

[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
# Clean any existing files in /tmp/.X11-unix environment
ExecStartPre=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :’
ExecStart=/sbin/runuser -l root -c “/usr/bin/vncserver %i ”
PIDFile=/root/.vnc/%H%i.pid
ExecStop=/bin/sh -c '/usr/bin/vncserver -kill %i > /dev/null 2>&1 || :’
[Install]
WantedBy=multi-user.target

好了,运行:

systemctl daemon-reload
systemctl enable vncserver@:1.service
systemctl start vncserver@:1.service
systemctl status vncserver@:1.service

出错!!哈哈哈,没错,上面都是浪费的流程!

只需要:

systemctl daemon-reload
runuser -l root -c /usr/bin/vncserver 1 
runuser -l smart -c /usr/bin/vncserver 2 

#开放5902端口(VCN)
iptables -A INPUT -p tcp --dport 5902 -j ACCEPT
#开放5901端口(VNC root)
iptables -A INPUT -p tcp --dport 5901 -j ACCEPT

#保存上述规则
service iptables save

#重启
systemctl restart iptables.service

五.安装开发环境

1.Python大法

yum install epel-release
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y openssl openssl-deve

wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz
tar xvf Python-3.4.3.tar.xz
mkdir /usr/local/python3 
cd Python-3.4.3
./configure --prefix=/usr/local/python3
make
make install

安装好后请

vim /etc/profile.d/myenv.sh

export PATH=/usr/local/python3/bin:$PATH
export PYTHONPATH=/data/www/python/smartdo

source /etc/profile.d/myenv.sh

python3
pip3

2.MYSQL好大法

下载官方源
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server
1,进入yum源配置目录
cd /etc/yum.repos.d
2,备份系统自带的yum源
mv CentOS-Base.repo CentOS-Base.repo.bk
下载163网易的yum源:
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
3,更新玩yum源后,执行下边命令更新yum配置,使操作立即生效
yum makecache
4,除了网易之外,国内还有其他不错的yum源,比如中科大和搜狐的,大家可以根据自己需求下载
中科大的yum源:
wget http://centos.ustc.edu.cn/CentOS-Base.repo
sohu的yum源
wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo
授权
chown -R mysql:mysql /var/lib/mysqlc
service mysqld restart

#授权远程和改密码(123456是密码)
mysql -uroot
use mysql
update user set host = '%' where user ='root' and host="localhost";
select host, user from user;
update user set password=password('123456') where user='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'  IDENTIFIED BY '123456'  WITH GRANT OPTION;
flush privileges;
service mysqld restart

然后远程登陆后干掉其他东西,留下:

设置权限!!

六.VPN代理服务器

1.安装PPTP,以用来提供VPN服务.

yum install pptpd.x86_64 按tab

2.进行配置

vim /etc/pptpd.conf
localip 192.168.0.1
remoteip 192.168.0.12-238,192.168.0.245

localip是主机VPN内网地址,remoteip是连接VPN分配的内网地址

3.分配账号给自己使用.

vim /etc/ppp/chap-secrets

在里面添加账户按如下格式

xxxx  pptpd  123456  *
xxxx1  pptpd  123456  *

xxxx为你的用户名,123456为你的密码,最后的*号表示允许在任意IP连接到服务

service pptpd restart

重启服务后发现还访问不了外网。然后我们需要让他能访问外网。首先,

vim /etc/ppp/pptpd-options

文件名可能为
vim /etc/ppp/options.pptpd

找到ms-dns,取消掉注释,改成你喜欢的DNS比如8.8.8.8,8.8.4.4

要开启内核IP转发

sudo vi /etc/sysctl.conf
取消掉 net.ipv4.ip_forward=1 这一行的注释. 可能找不到这一句

然后执行

sudo sysctl -p

使修改后的文件配置立即生效。

开启NAT转发.

iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eno16777728 -j MASQUERADE
service iptables save
service iptables restart

192.168.0.0/24是你在上面设置的IP段,让这个段转发,注意eno16777728是你连接外网的那块网卡,这样就以NAT的方式请求外网的东西了。 不知道你的机器哪块网卡连的外网的话ifconfig一下!!

再来一次

service pptpd restart
NAT(Network Address Translation,网络地址转换)是1994年提出的。当在专用网内部的一些主机本来已经分配到了本地IP地址(即仅在本专用网内使用的专用地址),但现在又想和因特网上的主机通信(并不需要加密)时,可使用NAT方法。
这种方法需要在专用网连接到因特网的路由器上安装NAT软件。装有NAT软件的路由器叫做NAT路由器,它至少有一个有效的外部全球IP地址。这样,所有使用本地地址的主机在和外界通信时,都要在NAT路由器上将其本地地址转换成全球IP地址,才能和因特网连接。
另外,这种通过使用少量的公有IP 地址代表较多的私有IP 地址的方式,将有助于减缓可用的IP地址空间的枯竭。在RFC 1632中有对NAT的说明

如果不行的话防火墙上,全部接受,完美!!

iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P INPUT ACCEPT
service iptables save
service iptables restart

查看/etc/sysconfig/iptables,结果是这样的,这样肯定不行,求告知!!

[root@spider2 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Fri Nov  4 10:01:51 2016
*filter
:INPUT ACCEPT [4:192]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [49:7158]
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5902 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5901 -j ACCEPT
COMMIT
# Completed on Fri Nov  4 10:01:51 2016
# Generated by iptables-save v1.4.21 on Fri Nov  4 10:01:51 2016
*nat
:PREROUTING ACCEPT [50:2445]
:INPUT ACCEPT [16:835]
:OUTPUT ACCEPT [24:1821]
:POSTROUTING ACCEPT [24:1821]
-A POSTROUTING -s 192.168.0.0/24 -o eno16777728 -j MASQUERADE
COMMIT
# Completed on Fri Nov  4 10:01:51 2016

参考:http://www.cnblogs.com/apexchu/p/4274416.html

七.多网关和多IP配置

目前有两个IP网段,分别对应两个网关,且只有一块网卡,网卡名为eno16777984

比如

146.148.149.202-254 146.148.149.193
146.148.150.194-254 146.148.150.193

先将网段和网关绑定

一次性路由

1、使用route命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:

添加到主机的路由(就是一个IP一个IP添加)

 route add –host 146.148.149.202 dev eno16777984
 route add –host 146.148.149.202 gw 146.148.149.193

添加到网络的路由(批量)

route add –net 146.148.149.0 netmask 255.255.255.0 dev eno16777984
route add –net 146.148.149.0 netmask 255.255.255.0 gw 146.148.149.193

简洁写法

route add –net 146.148.150.0/24 dev eno16777984
route add –net 146.148.150.0/24 gw 146.148.150.193

添加默认网关

route add default gw 146.148.149.193

删除主机路由:

route del –host 146.148.149.202 dev eno16777984

删除网络路由:

 route del -net 146.148.149.0 netmask 255.255.255.0
 route del -net 146.148.150.0/24

删除默认路由

route del default gw 146.148.149.193

2.永久路由(推荐)

在/etc/rc.local里添加

route add default gw 146.148.149.193
route add –net 146.148.149.0 netmask 255.255.255.0 dev eno16777984
route add –net 146.148.149.0 netmask 255.255.255.0 gw 146.148.149.193
route add –net 146.148.150.0/24 dev eno16777984
route add –net 146.148.150.0/24 gw 146.148.150.193

或者在/etc/sysconfig/network里添加到末尾

GATEWAY=gw_ip  未尝试过

或者在/etc/sysconfig/static-routes:写入路由信息,如果没有这个文件自己创建 (尝试过十分好用)!(推荐!!!!!!)

any net 146.148.149.0/24 gw 146.148.149.193
any net 146.148.150.0/24 gw 146.148.150.193
any net 0.0.0.0/0 gw 146.148.149.193       # 默认路由写法,这里的子网掩码为0

创建完请这样,ifcfg-Wired_connection_1这个要自己把握

ifdown ifcfg-Wired_connection_1
ifup ifcfg-Wired_connection_1
service network restart   # 失败不要紧
[root@centos7 network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         146.148.149.193 0.0.0.0         UG    0      0        0 eno16777984
0.0.0.0         146.148.149.193 0.0.0.0         UG    100    0        0 eno16777984
146.148.149.0   0.0.0.0         255.255.255.0   U     100    0        0 eno16777984
146.148.150.0   146.148.150.193 255.255.255.0   UG    0      0        0 eno16777984
146.148.150.0   0.0.0.0         255.255.255.0   U     100    0        0 eno16777984

如果在rc.local中添加路由会造成NFS无法自动挂载问题,所以使用static-routes的方法是最好的。无论重启系统和service network restart 都会生效。

解决NFS问题的描述:
 按照linux启动的顺序,rc.local里面的内容是在linux所有服务都启动完毕,最后才被执行的,也就是说,这里面的内容是在NFS之后才被执行的,那也就是说在NFS启动的时候,服务器上的静态路由是没有被添加的,所以NFS挂载不能成功。

3.其他方法

在/etc/sysconfig/network-script/route-interface下添加路由(每个接口一个文件,如果没有就创建一个,只能添加针对该接口的路由)

格式如下:

network/prefix via gateway dev intf

例如给eno16777984添加一个默认网关:

vim /etc/sysconfig/network-scripts/route-eno16777984

#添加如下内容(可以省略dev eno16777984)   
0.0.0.0/0 via 146.148.149.193 dev eno16777984

ps:注意这里的掩码是0而不是32,因为这里是网段而不是路由。保存退出后,service network restart。

查看路由

route -n

修改好后保存退出,然后重启网络:

service network restart

或者

ifdown ifcfg-Wired_connection_2 && ifup ifcfg-Wired_connection_2 && service network restart

4.多IP

如在/etc/sysconfig/network-scripts/ifcfg-eno16777984手填,网关不设置

HWADDR=00:50:56:83:7B:B0
TYPE=Ethernet
BOOTPROTO=none
DNS1=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME="Wired connection 2"
UUID=94ce4100-6639-4950-8d94-6cd594a2759c
ONBOOT=yes
IPADDR1=146.148.149.203
PREFIX1=24
IPADDR2=146.148.149.204
PREFIX2=24
IPADDR3=146.148.149.205
PREFIX3=24
IPADDR4=146.148.150.194
PREFIX4=24
IPADDR=146.148.149.202
PREFIX=24


#GATEWAY=

写了个程序批量生成:

# !/usr/bin/python3.4
# -*-coding:utf-8-*-
# on 2016/11/5.
# 功能:
#   生成IP给Linux配置文件用

# IPADDR=146.148.149.202
# PREFIX=24
# IPADDR1=146.148.149.203
# PREFIX1=24
# IPADDR2=146.148.149.204
# PREFIX2=24
# IPADDR3=146.148.149.205
# PREFIX3=24
# IPADDR4=146.148.150.194
# PREFIX4=24
if __name__ == "__main__":
    ii = 0
    dudu = []
    try:
        while True:
            ips = input("如:146.148.149.202-254:")
            temp = ips.split("-")
            ipend = int(temp[1])

            temptemp = temp[0].split(".")

            ipprefix = ".".join(temptemp[0:3])
            ipbegin = int(temptemp[3])
            for i in range(ipbegin, ipend + 1):
                if ii == 0:
                    a = "IPADDR="
                    b = "PREFIX="
                else:
                    a = "IPADDR" + str(ii) + "="
                    b = "PREFIX" + str(ii) + "="

                dudu.append(a + ipprefix + "." + str(i))
                dudu.append(b + "24")
                ii = ii + 1
    except Exception as e:
        print(e)
        pass
    for i in dudu:
print(i)

然后

ifdown ifcfg-Wired_connection_1
ifup ifcfg-Wired_connection_1

八.HTTP代理服务器

1.使用squid

#安装
yum -y install squid

# 版本
squid -v

Squid Cache: Version 3.3.8


#开机使用
chkconfig --level 35 squid on

#安装密码
yum install httpd-tools
htpasswd  -c /etc/squid/passwd username

# 一定要设置!!
chmod 777 /etc/squid/passwd 


#编辑,看后面完整
vim /etc/squid/squid.conf 

# 加上
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated
#开端口
iptables -A INPUT -p tcp --dport 808 -j ACCEPT

ls /var/log/squid/

squid -z  #初始化缓存

# 启动
systemctl start squid.service

查看状态
squid -Nd1

# 停止
systemctl stop squid.service
# 重启
systemctl restart squid.service

出错!!

#按提示操作
journalctl -xe

如果只是用squid做代理,不想缓存所有网站文件的话,可以修改squid配置,no-store选项指示Squid继续从cache_dir读取文件,但不往里面写新目标。它在squid.conf文件里看起来如下:

将squid.conf里的cache_dir ufs /home/cache 1024 16 256改为

cache_dir ufs /home/cache 1024 16 256 no-store
cache deny all

保存 然后,检查一下,有没有语法错误:

#squid -k parse
#squid -k reconfigure

访问显示


    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Upgrade-Insecure-Requests: 1
    Host: ip.42.pl
    Via: 1.1 spider2 (squid/3.3.8)
    X-Forwarded-For: 116.21.24.161
    Cache-Control: max-age=259200
    Connection: keep-alive

去掉真实IP

via off
forwarded_for off
follow_x_forwarded_for deny all
request_header_access X-Forwarded-For deny all
header_access X_Forwarded_For deny all

爆 Could not determine this machines public hostname. Please configure one or set ‘visible_hostname’

配置加上
visible_hostname xxxx

完整配置:

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12	# RFC1918 possible internal network
acl localnet src 192.168.0.0/16	# RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80		# http
acl Safe_ports port 21		# ftp
acl Safe_ports port 443		# https
acl Safe_ports port 70		# gopher
acl Safe_ports port 210		# wais
acl Safe_ports port 1025-65535	# unregistered ports
acl Safe_ports port 280		# http-mgmt
acl Safe_ports port 488		# gss-http
acl Safe_ports port 591		# filemaker
acl Safe_ports port 777		# multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access deny CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
#http_access allow localnet
#http_access allow localhost


# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /var/spool/squid 100 16 256 no-store
cache deny all
# Leave coredumps in the first cache dir
coredump_dir /var/spool/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

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

# Squid normally listens to port 3128
http_port 808

# And finally deny all other access to this proxy
http_access deny all
via off
forwarded_for off
follow_x_forwarded_for deny all
request_header_access X-Forwarded-For deny all
#header_access X_Forwarded_For deny all
visible_hostname xxxx

http://blog.csdn.net/mingzznet/article/details/52921218

2.多IP squid设置!

http://zyan.cc/book/squid/

参考谷歌!!!

https://itechnology.wordpress.com/2008/05/26/setup-squid-proxy-server-to-use-multiple-outgoing-ip-addresses/

A friend of mine needed my help to configure squid server to use multiple IP addresses based on the squid’s IP being used as proxy server. I told him that he can configure the squid server using following squid acls and tcp_outgoing_address directives.

acl ip1 myip 192.168.1.2
acl ip2 myip 192.168.1.3
acl ip3 myip 192.168.1.4
tcp_outgoing_address 192.168.1.2 ip1
tcp_outgoing_address 192.168.1.3 ip2
tcp_outgoing_address 192.168.1.4 ip3

The acl lines tell squid to match myip which means if someone uses the IP 192.168.1.2 as their proxy server they will match the acl ip1 and so on..

Update: But his problem was not solved by this, because he had to configure squid for 500+ IPs and doing this by hand is not worth it when you have so many languages out there. So I wrote a small perl script for him to generate squid acl and tcp_outgoing_address directives for all IPs.

我编了一个程序来实现….hahha,注意myip已经改为localip

所以

# !/usr/bin/python3.4
# -*-coding:utf-8-*-
# on 2016/11/5.
# 功能:
#   生成IP给Squid配置文件用

# acl ip1 localip 192.168.1.2
# acl ip2 localip 192.168.1.3
# acl ip3 localip 192.168.1.4
# tcp_outgoing_address 192.168.1.2 ip1
# tcp_outgoing_address 192.168.1.3 ip2
# tcp_outgoing_address 192.168.1.4 ip3

if __name__ == "__main__":
    ii = 0
    dudu = []
    try:
        while True:
            ips = input("如:146.148.149.202-254:")
            temp = ips.split("-")
            ipend = int(temp[1])

            temptemp = temp[0].split(".")

            ipprefix = ".".join(temptemp[0:3])
            ipbegin = int(temptemp[3])
            for i in range(ipbegin, ipend + 1):
                dudu.append(ipprefix + "." + str(i))
                ii = ii + 1
    except Exception as e:
        print(e)
        pass
    for i in dudu:
        # acl ip3 localip 192.168.1.4
        # tcp_outgoing_address 192.168.1.2 ip1
        print("acl ip" + i + " localip " + i)
print("tcp_outgoing_address " + i + " " + "ip" + i)

九.附送

1.清除历史

清除登陆系统成功的记录
[root@localhost root]# echo > /var/log/wtmp //此文件默认打开时乱码,可查到ip等信息
[root@localhost root]# last //此时即查不到用户登录信息

清除登陆系统失败的记录
[root@localhost root]# echo > /var/log/btmp //此文件默认打开时乱码,可查到登陆失败信息
[root@localhost root]# lastb //查不到登陆失败信息
 
清除历史执行命令
[root@localhost root]# history -c //清空历史执行命令
[root@localhost root]# echo > ./.bash_history //或清空用户目录下的这个文件即可
 
导入空历史记录
[root@localhost root]# vi /root/history //新建记录文件
[root@localhost root]# history -c //清除记录 
[root@localhost root]# history -r /root/history.txt //导入记录 
[root@localhost root]# history //查询导入结果

example 
[root@localhost root]# vi /root/history
[root@localhost root]# history -c 
[root@localhost root]# history -r /root/history.txt 
[root@localhost root]# history 
[root@localhost root]# echo > /var/log/wtmp  
[root@localhost root]# last
[root@localhost root]# echo > /var/log/btmp
[root@localhost root]# lastb 
[root@localhost root]# history -c 
[root@localhost root]# echo > ./.bash_history
[root@localhost root]# history
echo > /var/log/wtmp	#清除用户登录记录和命令记录
echo > /var/log/btmp	
echo > .bash_history
history -c

2.扩大linux的文件描述符

一,通过ulimit命令修改
 
//显示当前文件描述符
ulimit -n
 
//修改当前用户环境下的文件描述符为65536
ulimit -HSn 65536
 
echo "ulimit -HSn 65536" >> /etcrc.local
 
使用ulimit命令的缺点:
 
1,只能修改当前登录用户环境下的文件描述符,如果此用户来另外打开一个连接,此链接环境的文件描述符依然是没改前的
2,如果系统重启,以前修改都不再生效
 
二,通过修改limits.conf文件
 
编辑/etc/security/limits.conf 文件,在最后加入如下两行
 
*  soft    nofile  65536
*  hard    nofile  65536
 
保存退出,都不需要重启服务器,直接重新登陆用ulimit -n就能看到效果
 
这样无论使用哪个用户,无论是否重启都不会失效了。
 
扩大linux的文件描述符后,再重新编译安装squid,安装完成后,重新启用此代理,发现连接数马上就上来了

3.替换文本字符串

http://www.cnblogs.com/emanlee/archive/2013/09/07/3307642.html

sed -i 's/myip/localip/g' /etc/squid/squid.conf     将source替换成OKSTR

多IP另外做法(参考其他人,解释不了,先留着)

[root@centos7 network-scripts]# cat ifcfg-eno16777984
TYPE=Ethernet
HWADDR=00:50:56:83:30:6a
BOOTPROTO=static
IPADDR=104.143.159.232
NETMASK=255.255.255.0
NETWORK=104.143.159.193
DNS1=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eno
#UUID=46bc88d2-5be6-44ea-bb70-865b8fd9d5cf
#DEVICE=eno
DEVICE=eno16777984
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=y

[root@centos7 network-scripts]# cat ifcfg-eno16777984-range0
TYPE=Ethernet
IPV4_FAILURE_FATAL=no
IPV6INIT=no
ONBOOT=yes
IPADDR_START=104.216.7.234
IPADDR_END=104.216.7.254
GATEWAY=104.216.7.193
NO_ALIASROUTING=yes
CLONENUM_START=0

[root@centos7 network-scripts]# cat ifcfg-eno16777984-range1
TYPE=Ethernet
IPV4_FAILURE_FATAL=no
IPV6INIT=no
ONBOOT=yes
IPADDR_START=104.216.18.233
IPADDR_END=104.216.18.254
GATEWAY=104.216.18.193
NO_ALIASROUTING=yes
CLONENUM_START=21

[root@centos7 network-scripts]# cat ifcfg-eno16777984-range2
TYPE=Ethernet
IPV4_FAILURE_FATAL=no
IPV6INIT=no
ONBOOT=yes
IPADDR_START=104.216.76.169
IPADDR_END=104.216.76.190
GATEWAY=104.216.76.129
NO_ALIASROUTING=yes
CLONENUM_START=43

[root@centos7 network-scripts]# cat ifcfg-eno16777984-range3
TYPE=Ethernet
IPV4_FAILURE_FATAL=no
IPV6INIT=no
ONBOOT=yes
IPADDR_START=104.216.77.41
IPADDR_END=104.216.77.62
GATEWAY=104.216.77.1
NO_ALIASROUTING=yes
CLONENUM_START=65
[root@centos7 squid]# cat squid.conf
#
# Recommended minimum configuration:
#

visible_hostname 104.216.7.234
via off
forwarded_for delete
cache deny all

#
# Multiprocessors
#

workers 4
cpu_affinity_map process_numbers=1,2,3,4 cores=1,2,3,4


#
# Multiple IPs
#

acl ip_104_216_7_234 localip 104.216.7.234
tcp_outgoing_address 104.216.7.234 ip_104_216_7_234

#
# Auth
#

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
auth_param basic children 5
auth_param basic realm Squid proxy-caching web server
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive off

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
acl localnet src 10.0.0.0/8	# RFC1918 possible internal network
acl localnet src 172.16.0.0/12	# RFC1918 possible internal network
acl localnet src 192.168.0.0/16	# RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines

acl SSL_ports port 443
acl Safe_ports port 80		# http
acl Safe_ports port 21		# ftp
acl Safe_ports port 443		# https
acl Safe_ports port 70		# gopher
acl Safe_ports port 210		# wais
acl Safe_ports port 1025-65535	# unregistered ports
acl Safe_ports port 280		# http-mgmt
acl Safe_ports port 488		# gss-http
acl Safe_ports port 591		# filemaker
acl Safe_ports port 777		# multiling http
acl CONNECT method CONNECT

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
#http_access deny CONNECT !SSL_ports
#http_access allow CONNECT SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

acl auth_user proxy_auth REQUIRED
http_access allow auth_user

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
# http_access deny all
http_access allow all

# Squid normally listens to port 3128
#http_port 3128
http_port 12306
#https_port 443 cert=/etc/squid/squid.crt key=/etc/squid/squid.key

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/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