修改主机名
[root@localhost ~]# hostnamectl set-hostname master.example.com
bash
[root@localhost ~]# bash
[root@master ~]#
安装bind和bind-utils服务
[root@master ~]# yum -y install bind bind-utils
[root@master ~]# systemctl enable --now named
Created symlink from /etc/systemd/system/multi-user.target.wants/named.service to /usr/lib/systemd/system/named.service.
修改主配置文件/etc/named.conf
[root@master ~]# vim /etc/named.conf
....
options {
listen-on port 53 { any; }; #将127.0.0.1改为any,表示监听所有的53端口
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; #将localhost改为any,表示允许所有网段使用
....
修改区域配置文件/etc/named.rfc1912.zones,添加正向区域配置
[root@master ~]# vim /etc/named.rfc1912.zones
#在文件最后添加
zone "example.com" IN {
type master;
file "example.com.zone";
allow-update { none; };
};
配置区域数据配置文件
#复制默认文件作为我们要使用的配置文件
[root@master ~]# cd /var/named/
[root@master named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@master named]# cp named.localhost example.com.zone
[root@master named]# ll
总用量 20
drwxrwx---. 2 named named 23 5月 24 16:16 data
drwxrwx---. 2 named named 60 5月 24 16:16 dynamic
-rw-r-----. 1 root root 152 5月 24 16:48 example.com.zone
-rw-r-----. 1 root named 2253 4月 5 2018 named.ca
-rw-r-----. 1 root named 152 12月 15 2009 named.empty
-rw-r-----. 1 root named 152 6月 21 2007 named.localhost
-rw-r-----. 1 root named 168 12月 15 2009 named.loopback
drwxrwx---. 2 named named 6 2月 24 01:17 slaves
#授权用户,否则可能解析异常
[root@master named]# chown root.named example.com.zone
[root@master named]# ll
总用量 20
drwxrwx---. 2 named named 23 5月 24 16:16 data
drwxrwx---. 2 named named 60 5月 24 16:16 dynamic
-rw-r-----. 1 root named 152 5月 24 16:48 example.com.zone
-rw-r-----. 1 root named 2253 4月 5 2018 named.ca
-rw-r-----. 1 root named 152 12月 15 2009 named.empty
-rw-r-----. 1 root named 152 6月 21 2007 named.localhost
-rw-r-----. 1 root named 168 12月 15 2009 named.loopback
drwxrwx---. 2 named named 6 2月 24 01:17 slaves
#配置区域数据配置文件
NS(nameserver)指的是域名服务器
MX为邮件交换记录
A(address)表示地址
CNAME表示别名
[root@master named]# vim example.com.zone
$TTL 1D
@ IN SOA example.com. admin.example.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS example.com.
A 192.168.40.133
master IN A 192.168.40.133
node1 IN A 192.168.40.153
node2 IN A 192.168.40.154
server IN CNAME master
#重启服务
[root@master ~]# systemctl restart named
配置本地解析
#配置dns指向
[root@master ~]# vim /etc/resolv.conf
[root@master ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain example.com
nameserver 192.168.40.133 #将dns服务器放在第一位
nameserver 192.168.40.2
#重启会失效,所以使配置永久生效(取消生效就将+变成—就行)
[root@master ~]# chattr +i /etc/resolv.conf
[root@master ~]# nslookup master.example.com
Server: 192.168.40.133
Address: 192.168.40.133#53
Name: master.example.com
Address: 192.168.40.133
[root@master ~]# nslookup node1.example.com
Server: 192.168.40.133
Address: 192.168.40.133#53
Name: node1.example.com
Address: 192.168.40.153
修改主机名
[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
安装bind-utils等常用工具
[root@node2 ~]# yum -y install bind-utils vim bash-com*
配置默认dns指向
[root@node2 ~]# vim /etc/resolv.conf
[root@node2 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain example.com
nameserver 192.168.40.133 #将dns服务器放在第一位
nameserver 192.168.40.2
#重启会失效,所以使配置永久生效(取消生效就将+变成—就行)
[root@node2 ~]# chattr +i /etc/resolv.conf
[root@kube ~]# cat /etc/sysconfig/named
OPTIONS="-4"
Linux防火墙设置-DNS服务器
【1】第一步:清除默认防火墙规则
iptables -F
iptables -X
iptables -Z
参数说明:
-F:清除所有的已制定的规则
-X:清除所有用户自定义的chain(应该说的是tables)
(扩展:table–Linux的iptables防火墙默认有三种表,Filter、NAT与Mangle,当然还有自定义的,其中Filter即是默认使用的表格,chain–条链,比如filter有INPUT、OUTPUT、FORWARD三条链)
-Z:将所有的chain的计数与流量统计清零
·设置原因:
filter的三条链中,默认策略都为ACCEPT,显然对于INPUT来说,这是很危险的,可以使用命令iptables -L -n来查看默认设置,或者使用iptables-save命令(会列出更详细的防火墙配置信息)。
【2】第二步:设置策略
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
设置原因:
DROP为丢弃,由1中可知,INPUT策略制定为DROP时才比较安全。
【3】第三步:根据所需服务制定各项规则
(1)将本机设置为信任设备
iptables -A INPUT -i lo -j ACCEPT
(2)制定ssh远程连接规则
iptables -A(添加) INPUT(链路) -p(指定协议) tcp(指定为TCP协议) --dport(指定目标端口号) 22(指定目标端口号为22) -j(指定操作) ACCEPT(指定操作为接受)
(3)制定dns服务规则
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
说明:
允许新的dns请求,同时允许以nslookup的方式来向服务器查询,即以源端口号53来查询dns信息。
(4)制定其它规则
iptables -A INPUT -p icmp -j ACCEPT说明:
可不用,但为了方便检测服务器的网络连通性,所以还是加上。
【4】写入防火墙配置文件
/etc/init.d/iptables save
说明:
要保存,否则重启服务器后上面所做的配置会失效。
完整的执行脚本如下:
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
/etc/init.d/iptables save
保存为.sh文件,以管理员权限执行即可。
其它常用命令:
查看防火墙简要配置
iptables -L -n
查看防火墙详细配置
iptables-save
重要说明:
进行防火墙的配置一定要格外小心,特别在远程做配置时,如果不小心清除了已定义的规则,又把默认的INPUT规则设置为DROP,这时就没有办法远程连接了,这点特别要注意。