DNS原理相关
DNS 为Domain Name System(域名系统)的缩写,它是一种将IP地址转换成对应的主机名或将主机名转换成与之相对应IP地址的一种服务机制。 其中通过域名解析出IP地址的叫做正向解析,通过IP地址解析出域名的叫做反向解析。 DNS使用TCP和UDP, 端口号都是53, 但它主要使用UDP,服务器之间备份使用TCP。
全世界只有13台“根”服务器,1个主根服务器放在美国,其他12台为辅根服务器,DNS服务器根据角色可以分为:主DNS, 从DNS, 缓存DNS服务器,DNS转发服务器。
软件下载地址:https://download.samba.org/pub/samba/stable/
测试环境:
[root@light src]# cat /etc/centos-release && uname -r CentOS release 6.6 (Final) 2.6.32-504.el6.x86_64
安装环境: 安装开发工具和库包
yum groupinstall "Development Tools" "Development Libraries" yum provides "*/filename" //如果提示依赖某filename,使用此命令查找依赖包
安装过程:
[root@light src]# tar -zxvf bind-9.10.2.tar.gz [root@light src]# cd bind-9.10.2 [root@light bind-9.10.2]# ./configure --prefix=/usr/local/named --enable-threads --with-openssl --with-libxml2 --sysconfdir=/etc [root@light bind-9.10.2]# make && make install [root@light bind-9.10.2]# echo $? 0 [root@light bind-9.10.2]# groupadd -g 53 -r named [root@light bind-9.10.2]# useradd -g named -r named [root@light bind-9.10.2]# mkdir /var/named
配置与测试:
[root@light bind-9.10.2]# vi /etc/named/named.conf options { directory "/var/named"; //表示默认的数据库文件在/var/named中 若没有需手动创建 pid-file "/var/run/named/named.pid"; //运行的PID文件路径,用于使用其他用户启动 }; zone "."{ //创建root域 type hint; file "named.ca"; }; zone "localhost" { //创建 localhost域 type master; file "named.local"; }; zone "0.0.127.in-addr.arpa" { //localhost的反解析 type master; file "127.0.0.zone"; }; [root@light named]# /usr/local/named/bin/dig -t NS . > /var/named/named.ca [root@light named]# /usr/local/named/sbin/rndc-confgen > /etc/named/rndc.conf [root@light sbin]# tail -12 /etc/named/rndc.conf >> /etc/named/named.conf # Use with the following in named.conf, adjusting the allow list as needed: # key "rndc-key" { # algorithm hmac-md5; # secret "4wq5U0hWysn2vygWCUloLw=="; # }; # # controls { # inet 127.0.0.1 port 953 # allow { 127.0.0.1; } keys { "rndc-key"; }; # }; # End of named.conf [root@light ~]# vi /var/named/named.local $TTL 1D @ IN SOA localhost. root ( 2015013001 1H 15M 1W 1D ) IN NS @ IN A 127.0.0.1 [root@light ~]# vi /var/named/127.0.0.zone [root@light ~]# cat /var/named/127.0.0.zone $TTl 1D @ IN SOA @ root.localhost. ( 2015013001 1H 15M 1W 1D ) IN NS localhost. 1 IN PTR localhost.
权限设置:
[root@light ~]# chown root:named /etc/named/* [root@light ~]# chown -R named:named /var/named [root@light ~]# chmod 640 /etc/named/* [root@light ~]# chmod 640 /var/named/*
检查文件正反解析:
[root@light sbin]# ./named-checkzone "localhost" /var/named/named.local zone localhost/IN: loaded serial 2015013001 OK [root@light sbin]# ./named-checkzone "0.0.127.in-addr.arpa" /var/named/named.local zone 0.0.127.in-addr.arpa/IN: loaded serial 2015013001 OK [root@light sbin]# ./named-checkzone "0.0.127.in-addr.arpa" /var/named/127.0.0.zone zone 0.0.127.in-addr.arpa/IN: loaded serial 2015013001 OK [root@light ~]# /usr/local/named/sbin/rndc-confgen -r /dev/urandom -a // 这一步是生成 rndc.key, 如果没有这个key, namd启动报错。 wrote key file "/etc/named/rndc.key" [root@light ~]# chown named:named /etc/named/rndc.key [root@light ~]# /usr/local/named/sbin/named #启动服务
增加一个域名:
[root@light ~]# vi /etc/named/named.conf //添加进脚本中 zone "bright.com" { type master; file "bright.com.zone"; }; zone "1.168.192.in-addr.arpa" { type master; file "192.168.1.zone"; }; [root@light ~]# vi /var/named/bright.com.zone //创建并编辑 $TTL 86400 @ IN SOA bright.com. root.bright.com. ( 2015013001 1H 15M 1W 1D ) IN NS bright.com. IN A 192.168.1.52 [root@light ~]# vi /var/named/192.168.1.zone //创建并编辑 $TTL 1D @ IN SOA bright.com. root.bright.com. ( 2015013001 1H 15M 1W 1D ) IN NS bright.com. 52 IN PTR bright.com.
同上测试是否正常解析
[root@light sbin]# pwd /usr/local/named/sbin [root@light sbin]# ./named-checkzone "bright.com" /var/named/bright.com.zone zone bright.com/IN: loaded serial 2015013001 OK [root@light sbin]# ./named-checkzone "1.168.192.in-addr.arpa" /var/named/192.168.1.zone zone 1.168.192.in-addr.arpa/IN: loaded serial 2015013001 OK
编写启动脚本 (named的配置文件路径按需修改!)
[root@light ~]# vi /etc/rc.d/init.d/named #!/bin/bash # # description: named daemon 描述信息 # chkconfig: - 25 88 启动和关闭级别 # # 启动named的pid文件、其实文件路径我们都可以自己指定的 pidFile=/var/run/named.pid # 启动服务时的锁文件、判断一个服务是不是启动是靠这个锁文件的 lockFile=/var/lock/subsys/named # named的配置文件路径 confFile=/etc/named.conf # 判断/etc/rc.d/init.d/functions路径下的functios是否存在这个文件、存在就sources进来 [ -r /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions # 定义启动方法 start() { # 判断锁文件是否存在、如果存在就说服务已经启动并退出 if [ -e $lockFile ]; then echo "named is already running..." exit 0 fi # 打印服务已经启动的信息 echo -n "Starting named:" # 调用functions中的daemon方法、并用--pidfile指定pid文件位置、指定用户和配置文件、我们本来是直接启用named的、现在利用daemon这个函数来启用这个服务、他会获得很多额外的属性、如果成功了会帮我们打印什么[ OK ]的、还可以做判断 daemon --pidfile "$pidFile" /usr/local/named/sbin/named -u named -c "$confFile" RETVAL=$? echo # 判断上面的命令是否执行成功、说明启动成功 if [ $RETVAL -eq 0 ]; then # 如果判断成功、就创建锁文件 touch $lockFile return $RETVAL else # 否则、就是失败了、那就删除锁文件和pid文件 rm -f $lockFile $pidFile return 1 fi } # 定义停止方法 stop() { # 如果锁文件不存在 if [ ! -e $lockFile ]; then # 说明服务还没有启动 echo "named is stopped." fi echo -n "Stopping named:" killproc named RETVAL=$? echo # 判断以上的命令是否执行停止成功、如果成功 if [ $RETVAL -eq 0 ];then # 删除锁文件和pid文件 rm -f $lockFile $pidFile return 0 else # 否则打印说没能正常停止 echo "Cannot stop named." # 调用functions中的函数、会打印失败 failure return 1 fi } # 定义重启服务 restart() { # 调用停止方法 stop # 睡眠2秒 sleep 2 # 调用启动方法 start } # 定义重新加载服务方法 reload() { echo -n "Reloading named: " # killprco HUP信号、named进程的信号 killproc named -HUP RETVAL=$? echo return $RETVAL } # 定义服务状态 status() { if pidof named &> /dev/null; then echo -n "named is running..." success echo else echo -n "named is stopped..." success echo fi } # 定义错误提示信息 usage() { echo "Usage: named {start|stop|restart|status|reload}" } case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; reload) reload ;; *) usage exit 4 ;; esac
加入服务
chmod 755 /etc/rc.d/init.d/named chkconfig --add named chkconfig --level 345 named on service named start(stop|status|reload|restart)