centos 7 torefresh refresh刷新
Linux下针对路由功能配置iptables的方法详解
作为公司上网的路由器需要实现的功能有nat地址转换、dhcp、dns缓存、流量控制、应用程序控制,nat地址转换通过iptables可以直接实现,dhcp服务需要安装dhcpd,dns缓存功能需要使用bind,流量控制可以使用tc,应用程序控制:例如对qq的封锁可以使用 netfilter-layer7-v2.22+17-protocols-2009-05-28.tar.gz来实现
1、网络规划
操作系统是centos5.8
2、安装dhcpd
代码如下:
yum install dhcp-3.0.5-31.el5
vim/etc/dhcp/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 10.0.0.0 netmask 255.255.255.0{
option routers 10.0.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 10.0.0.1;
range dynamic-bootp 10.0.0.100 10.0.0.200;
default-lease-time 21600;
max-lease-time 43200;
}
3、安装bind,实现dns缓存
代码如下:
yum install bind97.i386 bind97-libs.i386 bind97-utils.i386
vim/etc/named.conf
options{
directory"/var/named";
allow-recursion{ 10.0.0.0/24;};
recursion yes;
forward first;#将所有请求都进行转发
forwarders{ 114.114.114.114;};#定义转发服务器地址
};
zone"." IN{
type hint;
file"named.ca";
};
zone"localhost" IN{
type master;
file"named.localhost";
allow-transfer{ none;};
};
zone"0.0.127.in-addr.arpa" IN{
type master;
file"named.loopback";
allow-transfer{ none;};
};
创建根域文件,默认有
代码如下:
dig-t NS./var/named/named.ca
chown:named/var/named/named.ca
创建本地正向解析文件,默认有
代码如下:
vim/var/named/named.localhost
$TTL 1D
@ IN SOA@ rname.invalid.(
0; serial
1D; refresh
1H; retry
1W; expire
3H); minimum
NS@
A 127.0.0.1
chown:named/var/named/named.localhost
创建本地反向解析文件,默认有
代码如下:
vim/var/named/named.loopback
$TTL 1D
@ IN SOA@ rname.invalid.(
0; serial
1D; refresh
1H; retry
1W; expire
3H); minimum
NS@
A 127.0.0.1
PTR localhost.
chown:named/var/named/named.loopback
检查主配置文件
代码如下:
named-checkconf
检查根区域配置文件
代码如下:
named-checkzone“.”/var/named/named.ca
检查区域文件
代码如下:
named-checkzone“localhost”/var/named/named.localhost
启动服务
代码如下:
service named start
4、重新编译编译内核和iptables以支持应用层过滤
由于实行防火墙功能的是netfilter内核模块,所以需要重新编译内核,需要下载新的内核源码,并使用netfilter-layer7-v2.22作为内核的补丁一起编译到内核中。而控制netfiler的是iptables工具,因此iptables也必须重新编译安装,最后再安装应用程序过滤特征码库17-protocols-2009-05028.tar.gz
1、给内核打补丁,并重新编译内核
2、给iptables源码打补丁,并重新编译iptables
3、安装17proto
备份iptables脚本和配置文件
代码如下:
cp/etc/rc.d/init.d/iptables/root/iptables.sysv
cp/etc/sysconfig/iptables-config/root/iptables-config
2.6内核下载地址
netfilter下载地址
iptables源码下载地址
应用程序特征码库下载地址
代码如下:
xz-d linux-2.6.28.10.tar.xz
tar-xvf linux-2.6.28.10.tar.gz-C/usr/src#新的内核源码,用于重新编译
tar-zxvf netfilter-layer7-v2.22.tar.gz-C/usr/src#内核补丁和iptables补丁,只支持到2.6.28
#进入解压目录并创建软连接
pcd/usr/src
ln-sv linux-2.6.28.10 linux
#进入内核目录
pcd/usr/src/linux
#为当前内核打补丁
ppatch-p1../netfilter-layer7-v2.22/kernel-2.6.25-2.6.28-layer7-2.22.path
#为了方便编译内核将系统上的内核配置文件复制过来
pcp/boot/config-2.6.18-164.el5/usr/src/linux/.config
编译内核
代码如下:
make menuconfig
Networking support- Networking Options- Network packet filtering framework- Core Netfilter Configuration
Netfilter connection tracking support
"lawyer7" match support
"string" match support
"time" match support
"iprange" match support
"connlimit" match support
"state" match support
"conntrack" connection match support
"mac" address match support
"multiport" Multiple port match support
Networking support- Networign options- Network packet filtering framework- IP:Netfiltr Configuration
IPv4 connection tracking support(required for NAT)
Full NAT
MASQUERADE target support
NETMAP target support
REDIRECT target support
在Networking support中选择 Networking options
查找Network packet filtering framework(Netfilter)–Core Netfiler Configrationg–Netfilter connection tracking support(NEW),”layer7″ match support(NEW),”time” match support(NEW),”iprange”
查找IP:Netfilter Configuration–IPv4 connection tracking support,Full NAT(NEW)
代码如下:
make
make modules_install
make install
重启操作系统选择新内核登录
卸载旧的iptables
代码如下:
rpm-e iptables-1.3.5-9.1.el5 iptables-ipv6-1.3.5-9.1.el5 iptstate-1.4-2.el5--nodeps
安装新的iptables,以支持新的netfiler模块
代码如下:
tar-jsvf iptables-1.4.6.tar.bz2-C/usr/src
cd/usr/src/netfilter-layer7-v2.23
cd iptables-1.4.3forward-for-kernel-2.6.20forward
cp*/usr/src/iptables-1.4.6/extensions/
cd/usr/src/iptables-1.4.6/
./configure--prefix=/usr--with-ksource=/usr/src/linux
make
make install
查看安装后的iptables的文件
代码如下:
ls/usr/sbin|grep iptables
ls/usr/libexec/xtables
复制之前备份的配置文件和脚本
代码如下:
cp/root/iptables-config/etc/sysconfig/
cp/root/iptables.sysv/etc/rc.d/init.d/iptables
修改脚本中iptables的路径
代码如下:
vim/etc/rc.d/init.d/iptables
:.,$s@/sbin/$IPTABLES@/usr/sbin/$IPTABLES@g
让iptables服务开机自动启动
代码如下:
chkconfig--add iptables
修改iptables配置文件
将/etc/sysconfig/iptables-config中的
IPTABLES_MODULES=”ip_conntrack_netbios_ns”注释掉
安装协议特征码
代码如下:
tar xvf 17-protocols-2009-05028.tar.gz
make install
完成后在/etc/l7-protocols会生成文件
支持的协议/etc/l7-protocols/protocols
添加iptables策略,运行内部网络上网,禁止qq和视频
代码如下:
iptables-t nat-A POSTROUTING-s 10.0.0.0/24-j SNAT--to-soure 192.168.6.67
iptables-A FORWARD-m layer7--l7proto qq-j DROP
iptables-A FORWARD-m layer7--l7proto httpvideo-j DROP
iptables-A FORWARD-m layer7--l7proto httpaudio-j DROP
指定8点到12点无法上网
复制代码
代码如下:
iptables-A FORWARD-m time--timestart 08:00--timestop 12:00-j DROP
5、使用tc控制带宽
例如公司出口带宽是10Mbps,个用户A分配500KB的最大下载带宽,给用户B分配分配的最大下载带宽是200KB
A用户ip:10.0.0.100
B用户ip:10.0.0.101
代码如下:
#在eth0网卡上创建一个根队列规则,队列规则的算法使用htb,default 2表示指定一个默认类别编号,默认的流量控制策略,如果ip没有在后面的filter中被匹配到就都是有这个策略
tc qdisc add dev eth0 root handle 1:0 htb default 2
#在eth0网卡上定义一个类,prant 1:0中的1对应根队列规则中的handle 1:0,classid 1:2表示当前这个类的标识,用于应用在后面的得到filter中,rate 200kbsp表示带宽为200KB/s,ceil 200kbps表示最大带宽也为200KB/s,prio 2是优先级
tc class add dev eth0 parent 1:0 classid 1:2 htb rate 200kbps ceil 200kbps prio 2
tc class add dev eth0 parent 1:0 classid 1:3 htb rate 500kbps ceil 500kbps prio 2
#将两个类的默认的fifq队列规则改为sfq
tc qdisc add dev eth0 parent 1:2 handle 20 sfq
tc qdisc add dev eth0 parent 1:3 handle 30 sfq
#在网卡eth0上的1:0节点(对应qdisc中的handle 1:0)添加一个u32过滤规则,优先级为1,凡是目标地址是10.0.0.100的数据包都使用1:2类(对应classid为1:2的类)
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.100 flowid 1:2
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.101 flowid 1:3
如果还有其他用户例如用户C和D的ip是102、103,要求的下载带宽也要求500那么在加入
代码如下:
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.102 flowid 1:3
tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip dst 10.0.0.103 flowid 1:3
清除eth0上的规则
代码如下:
tc qdisc del dev eth1 root/dev/null
CentOS系统怎样安装DNS服务器
CentOS系统安装DNS服务器方法
DNS安装配置
在 RHEL5、6中 DNS都是用的是 bind软件包,而在 RHEL/CentOS 7用的是 unbound安装包,配置文件也有了改变。我们来看一下:
2.1.安装:
代码如下:
[root@linuxprobe~]# yum-y install unbound
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
---> Running transaction check
---> Package unbound.x86_64 0:1.4.20-19.el7 will be installed
---> Finished Dependency Resolution
·····
启动服务
代码如下:
[root@linuxprobe~]# systemctl restart unbound//启动DNS服务
[root@linuxprobe~]# systemctl enable unbound
ln-s‘/usr/lib/systemd/system/unbound.service‘‘/etc/systemd/system/multi-user.target.wants/unbound.service‘
//下次系统重启自动启动DNS服务
2.2.修改配置文件
unbound安装好之后,缺省配置文件在/etc/unbound/unbound.conf。
2.2.1.修改端口监听地址
相当于 RHEL6配置文件中的:listen-on port 53{ any;};
查看默认监听地址
代码如下:
[root@linuxprobe~]# netstat-tunlp|grep unbound
tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 3333/unbound
tcp 0 0 127.0.0.1:8953 0.0.0.0:* LISTEN 3333/unbound
tcp6 0 0::1:53:::* LISTEN 3333/unbound
tcp6 0 0::1:8953:::* LISTEN 3333/unbound
udp 0 0 127.0.0.1:53 0.0.0.0:* 3333/unbound
udp6 0 0::1:53:::* 3333/unbound
//默认监听本地回环地址,也就是现在只有自己能访问DNS服务,其它主机不能访问本机的DNS服务
修改监听地址代码如下:
[root@linuxprobe~]# vim/etc/unbound/unbound.conf
……
38# interface: 0.0.0.0
39 interface: 0.0.0.0
……
//找到38行,复制去掉注释行,打开监听全网功能。
重启服务查看
代码如下:
[root@linuxprobe~]# systemctl restart unbound
[root@linuxprobe~]# netstat-tunlp|grep unbound
tcp 0 0 0.0.0.0:53 0.0.0.0:* LISTEN 3461/unbound
tcp 0 0 127.0.0.1:8953 0.0.0.0:* LISTEN 3461/unbound
tcp6 0 0::1:8953:::* LISTEN 3461/unbound
udp 0 0 0.0.0.0:53 0.0.0.0:* 3461/unbound
//现在53号端口监听的是0.0.0.0,即所有网段都监听。
2.2.2.修改允许查询的范围
在 RHEL6中,DNS配置文件中有这样一句:allow-query{ localhost;};。此句定义的是允许向本机查询(迭代&递归)的主机范围,localhost代表只有本机可以向本机查询。而在配置中,经常改 localhost为 any,让所有主机能够向本机查询 DNS。所以,在 RHEL7中,也要做这样的修改,只不过修改内容不同而已,如下:
代码如下:
[root@linuxprobe~]# vim/etc/unbound/unbound.conf
……
177# access-control: 0.0.0.0/0 refuse
178 access-control: 0.0.0.0/0 allow
179# access-control: 127.0.0.0/8 allow
……
找到配置文件/etc/unbound/unbound.conf的第177行,缺省为注释行,把内容改为允许访问,然后保存退出,重启服务即可。
2.2.3.创建解析文件
RHEL/CentOS 5、6系统中,DNS的解析文件分正向和反向两个解析文件,并且有解析文件的模板文件。但是在 RHEL7中,正反向解析文件合并为一个,并且无模板文件,需自己创建,路径可以在主配置文件中查看:
代码如下:
[root@linuxprobe~]# vim/etc/unbound/unbound.conf
……
453# You can add locally served data with
454# local-zone:"local." static
455# local-data:"mycomputer.local. IN A 192.0.2.51"
//正向解析可参考语法
456# local-data:‘mytext.local TXT"content of text record"‘
457#
458# You can override certain queries with
459# local-data:"adserver.example.com A 127.0.0.1"
460#
461# You can redirect a domain to a fixed address with
462#(this makes example.com, www.example.com, etc, all go to 192.0.2.3)
463# local-zone:"example.com" redirect
464# local-data:"example.com A 192.0.2.3"
465#
# Shorthand to make PTR records,"IPv4 name" or"IPv6 name".
467# You can also add PTR records using local-data directly, but then
468# you need to do the reverse notation yourself.
469# local-data-ptr:"192.0.2.3 www.example.com"
//反向解析参考语法
470
471 include:/etc/unbound/local.d/*.conf
472
473# service clients over SSL(on the TCP sockets), with plain DNS inside
……
查看本机FQDN
代码如下:
[root@linuxprobe~]# hostname
linuxprobe.example.com
//由此可知,域名为example.com
创建解析文件代码如下:
[root@linuxprobe~]# vim/etc/unbound/local.d/example.conf
local-zone:"example.com." static
local-data:"example.com. 86400 IN SOA ns.example.com. root 1 1D 1H 1W 1H"
local-data:"ns.example.com. IN A 192.168.10.10"
local-data:"linuxprobe.example.com. IN A 192.168.10.10"
local-data-ptr:"192.168.10.10 ns.example.com."
local-data-ptr:"192.168.10.10 linuxprobe.example.com."
查看RHEL6上解析文件以作对比
代码如下:
[root@linuxprobe~]# vim/var/named/named.localhost
$TTL 1D
@ IN SOA@ rname.invalid.(
0; serial
1D; refresh
1H; retry
1W; expire
3H); minimum
NS@
A 127.0.0.1
AAAA::1
2.3.禁用服务用户
每个服务都是有其专用的服务用户,DNS的服务用户为 unbound,实际情况下服务用户的启用有可能有安全隐患,这里要禁用服务用户。
代码如下:
[root@linuxprobe~]# vim/etc/unbound/unbound.conf
······
211# if given, user privileges are dropped(after binding port),
212# and the given username is assumed. Default is user"unbound".
213# If you give"" no privileges are dropped.
214#username:"unbound"
215 username:""
216
217# the working directory. The relative files in this config
······
如上,找到配置文件的第214行,删除unbound即可,删除后为:username”“。
2.4.验证
代码如下:
[root@linuxprobe~]# unbound-checkconf
unbound-checkconf: no errors in/etc/unbound/unbound.conf
验证无配置问题,即可重启服务
复制代码代码如下:
[root@linuxprobe~]# systemctl restart unbound
dns验证:
修改本机DNS
代码如下:
[root@linuxprobe~]# vim/etc/sysconfig/network-scripts/ifcfg-eth0
HWADDR=00:0C:29:70:····
TYPE=Ethernet
····
IPADDR="192.168.10.10"
PREFIX="24"
···
DNS1=192.168.10.10
NAME=eth0
ONBOOT=no
[root@linuxprobe~]# systemctl restart network
nslookup验证
代码如下:
[root@linuxprobe~]# nslookup
linuxprobe.example.com.
192.168.10.10
ok dns设置成功
PS:关闭防火墙
在本次实验中我们关闭了 linux的3大防火墙。当没有关闭防火墙时,远程主机验证可能出现故障,这时需要在 DNS服务器防火墙上开放 DNS服务。我们以 firewall防火墙为例,修改一下:
代码如下:
[root@linuxprobe~]# systemctl stop iptables
[root@linuxprobe~]# systemctl stop ebtables
[root@linuxprobe~]# systemctl disable iptables
[root@linuxprobe~]# systemctl disable ebtables
[root@linuxprobe~]# firewall-cmd--add-service=dns--permanent
success
[root@linuxprobe~]# firewall-cmd--reload
success
[root@linuxprobe~]# firewall-cmd--list-all
public(default, active)
interfaces: eth0
sources:
services: dhcpv6-client dns ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
//DNS服务器上Firewall开放DNS访问ok
怎样在CentOS 7.0上安装和配置VNC服务器
1.安装 X-Window
首先我们需要安装 X-Window,在终端中运行下面的命令,安装会花费一点时间。
# yum check-update# yum groupinstall"X Window System"
installing x windows
#yum install gnome-classic-session gnome-terminal nautilus-open-terminal control-center liberation-mono-fonts
install gnome classic session
###设置默认启动图形界面# unlink/etc/systemd/system/default.target# ln-sf/lib/systemd/system/graphical.target/etc/systemd/system/default.target
configuring graphics
# reboot
在服务器重启之后,我们就有了一个工作着的 CentOS 7桌面环境了。
现在,我们要在服务器上安装 VNC服务器了。
2.安装 VNC服务器
现在要在我们的 CentOS 7上安装 VNC服务器了。我们需要执行下面的命令。
# yum install tigervnc-server-y
vnc server
3.配置 VNC
然后,我们需要在/etc/systemd/system/目录里创建一个配置文件。我们可以将/lib/systemd/sytem/vncserver@.service拷贝一份配置文件范例过来。
# cp/lib/systemd/system/vncserver@.service/etc/systemd/system/vncserver@:1.service
copying vnc server configuration
接着我们用自己最喜欢的编辑器(这儿我们用的 nano)打开/etc/systemd/system/vncserver@:1.service,找到下面这几行,用自己的用户名替换掉。举例来说,我的用户名是 linoxide所以我用 linoxide来替换掉:
ExecStart=/sbin/runuser-l<USER>-c"/usr/bin/vncserver%i"PIDFile=/home/<USER>/.vnc/%H%i.pid
替换成
ExecStart=/sbin/runuser-l linoxide-c"/usr/bin/vncserver%i"PIDFile=/home/linoxide/.vnc/%H%i.pid
如果是 root用户则
ExecStart=/sbin/runuser-l root-c"/usr/bin/vncserver%i"PIDFile=/root/.vnc/%H%i.pid
configuring user
好了,下面重启 systemd。
# systemctl daemon-reload
最后还要设置一下用户的 VNC密码。要设置某个用户的密码,必须要有能通过 sudo切换到用户的权限,这里我用 linoxide的权限,执行“su linoxide”就可以了。
# su linoxide$ sudo vncpasswd
setting vnc password
确保你输入的密码多于6个字符
4.开启服务
用下面的命令(永久地)开启服务:
$ sudo systemctl enable vncserver@:1.service
启动服务。
$ sudo systemctl start vncserver@:1.service
5.防火墙设置
我们需要配置防火墙来让 VNC服务正常工作。
$ sudo firewall-cmd--permanent--add-service vnc-server$ sudo systemctl restart firewalld.service
allowing firewalld
现在就可以用 IP和端口号(LCTT译注:例如 192.168.1.1:1,这里的端口不是服务器的端口,而是视 VNC连接数的多少从1开始排序)来连接 VNC服务器了。
6.用 VNC客户端连接服务器
好了,现在已经完成了 VNC服务器的安装了。要使用 VNC连接服务器,我们还需要一个在本地计算机上安装的仅供连接远程计算机使用的 VNC客户端。