第十三周作业

简述DNS服务器原理

第十三周作业_第1张图片

主辅DNS服务器搭建

第十三周作业_第2张图片
master dns 服务器

写一个脚本实现 主dns的搭建
脚本名:/scripts/master-dns.sh
zone: xuepeng.com
master.xuepeng.com NS 192.168.43.9
slave.xuepeng.com NS 192.168.43.19

#!/bin/bash
. /etc/init.d/functions
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
ip=192.168.43.9
slave_ip=192.168.43.19
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
        yum -q install $package -y && action "package bind install seccessful" true
fi
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@' -e '/allow-query/a\\tallow-transfer { '"$slave_ip"'; };' $name_conf
sed -i.bak '$a\zone "'"$zone"'" IN {\n\ttype master;\n\tfile "'"$zone"'.zone";\n};' ${name_zone}
cat < ${zone_file}
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
     NS  master
     NS  slave
master A $ip
slave  A ${slave_ip}
EOF
chgrp named ${zone_file}
chmod  640 ${zone_file}
named-checkconf &> /dev/null  && named-checkzone $zone  ${zone_file} &> /dev/null \
&& systemctl enable --now named && action "service named started" true

slave dns 服务器

写一个脚本实现 从dns的搭建
脚本名:/scripts/slave-dns.sh
zone: xuepeng.com

#!/bin/bash
. /etc/init.d/functions
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
ip=192.168.43.9
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
        yum -q install $package -y && action "package bind install seccessful" true
fi
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@' -e '/allow-query/a\\tallow-transfer { none; };' $name_conf
sed -i.bak '$a\zone "'"$zone"'" IN {\n\ttype slave;\n\tmasters { '"$ip"'; };\n\tfile "slaves/'"$zone"'.zone";\n};' ${name_zone}
named-checkconf &> /dev/null  && systemctl enable --now named && action "service named started" true

在主dns服务器上添加 A 记录,修改区域数据库文件序列号

number=$[`egrep -m 1 -o -w '[[:digit:]]+' /var/named/xuepeng.com.zone`+1]
echo "www A 192.168.43.100" >> /var/named/xuepeng.com.zone
sed -r -i '/SOA/s@\<[[:digit:]]+\>@'"$number"'@' /var/named/xuepeng.com.zone

主从服务器重启named服务

systemctl restart named

web服务器的配置

#!/bin/bash
. /etc/init.d/functions
rpm -q httpd &> /dev/null
if [ $? -ne 0 ];then
        yum -q install httpd -y && action "package httpd is installed"
fi
cat <<-EOF > /etc/httpd/conf.d/test.conf

        documentroot /var/www/html
        servername www.xuepeng.com
        
                require all granted
        

EOF
echo "www.xuepeng.com" > /var/www/html/index.html
killall -0 httpd &> /dev/null || systemctl enable --now httpd && action "service httpd started"

测试

在客户端上测试
ip: 192.168.43.49/24
dns: 192.168.43.19
在这里插入图片描述
在这里插入图片描述

智能 DNS

第十三周作业_第3张图片

DNS服务器配置

ip规划
vmnet0 192.168.1.0/24 ens37: 192.168.1.53
vmnet8 192.168.43.0/24 ens33: 192.168.43.53
vment2 192.168.2.0/24 ens38: 192.168.2.53

写一个脚本,实现智能dns

#!/bin/bash
. /etc/init.d/functions

# definition var
package="bind"
name_conf="/etc/named.conf"
name_zone="/etc/named.rfc1912.zones"
zone="xuepeng.com"
zone_file="/var/named/${zone}.zone"
Area1="BeiJing"
Area2="ShangHai"
Area3="ShenZhen"
ip1=192.168.1.53
ip2=192.168.2.53
ip3=192.168.43.53

# install package bind
rpm -q $package &> /dev/null
if [ $? -ne 0 ];then
        yum -q install $package -y && action "package bind install seccessful" true
fi

# named.conf
sed -i.bak -e '/listen-on/s@^@#@' -e '/allow-query/s@^@#@'  $name_conf
sed -i '/zone/,$d' $name_conf
cat <<-EOF >> $name_conf
acl $Area1 {
        192.168.1.0/24;
};
acl $Area2 {
        192.168.2.0/24;
};
acl $Area3 {
        192.168.43.0/24;
};

view view_$Area1 {
        match-clients { $Area1; };
        include "/etc/named.rfc1912.zones.$Area1";
};

view view_$Area2 {
        match-clients { $Area2; };
        include "/etc/named.rfc1912.zones.$Area2";
};
view view_$Area3 {
        match-clients { $Area3; };
        include "/etc/named.rfc1912.zones.$Area3";
};
EOF

#  /etc/named.rfc1912.zones
cp -p $name_zone $name_zone.$Area1
cat <<-EOF >> ${name_zone}.$Area1
zone "." IN {
        type hint;
        file "named.ca";
};
zone "$zone" IN {
        type master;
        file "${zone}.zone.${Area1}";
};
EOF
cp -p ${name_zone}.${Area1} ${name_zone}.$Area2
sed -i 's@'"$Area1"'@'"$Area2"'@' ${name_zone}.$Area2
cp -p ${name_zone}.${Area1} ${name_zone}.$Area3
sed -i 's@'"$Area1"'@'"$Area3"'@' ${name_zone}.$Area3

# /var/named/xuepeng.com.zone

cat < ${zone_file}.$Area1
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
     NS  master
master A $ip1
www A  192.168.1.80
EOF


cat < ${zone_file}.$Area2
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
     NS  master
master A $ip2
www A  192.168.2.80
EOF

cat < ${zone_file}.$Area3
\$TTL 1D
@ IN SOA master admin ( `date +%F|tr -dc [:digit:]` 1D 1H 1W 3H )
     NS  master
master A $ip3
www A  192.168.43.80
EOF

# check
named-checkconf && \
for i in ${zone_file}.$Area1 ${zone_file}.$Area2 ${zone_file}.$Area3;do
        named-checkzone $zone $i
done

# start service
if [ $? -eq 0 ];then
        systemctl enable --now named
        action "service named started" true
else
        action "service named not started" false
fi

运行脚本

第十三周作业_第4张图片
测试

第十三周作业_第5张图片

编译安装Mariadb,并启动后可以正常登录

源码安装 mariadb-10.4.12脚本 /scripts/install_mariadb.sh

#!/bin/bash
#
. /etc/init.d/functions
id mysql &> /dev/null || useradd -r  -s /sbin/nologin -d /data/mysql mysql
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql

yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel -y
work_dir=`pwd`
packagename="mariadb-10.4.12.tar.gz"
cd $work_dir
if [ -f $work_dir/$packagename ];then
        action "package mariadb is ok"
else
        action "not install package" false
        exit
fi
tar xf $packagename && action "$packagename unzip"
builddir=${packagename/".tar.gz"}
cd $builddir
cmake . -DCMAKE_INSTALL_PREFIX=/app/mysql -DMYSQL_DATADIR=/data/mysql/ \
        -DSYSCONFDIR=/etc/mysql -DMYSQL_USER=mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
        -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
        -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 \
        -DENABLED_LOCAL_INFILE=1 -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci

make -j `lscpu|grep -w "^CPU(s):"|awk '{print $2}'` && make install
if [ $? -eq 0 ];then
        action "$builddir install successful"
fi

运行脚本之后截图

第十三周作业_第6张图片
配置 mariadb-10.4.12脚本 /scripts/cfg_mariadb.sh

#!/bin/bash
#
. /etc/init.d/functions
buildir="/usr/local/src/mariadb-10.4.12/"
basedir="/app/mysql"
datadir="/data/mysql"
user="mysql"
cfgdir="/etc/mysql"
chown -R root.root $buildir
if [ ! -d $cfgdir ];then
        mkdir $cfgdir
fi

cat <<-EOF > $cfgdir/my.cnf
[client]
port = 3306
socket = $datadir/mysql.sock

[mysqld]
port = 3306
socket = $datadir/mysql.sock
user = mysql
basedir = $basedir
datadir = $datadir
log_error= $datadir/mysql.err
pid-file = $datadir/mysql.pid
log-bin = $datadir/mysql
skip_name_resolve = 1
EOF


cd $buildir && ./scripts/mysql_install_db --basedir=$basedir --datadir=$datadir --user=$user --defaults-file=$cfgdir/my.cnf  && action "init mariadb successful" true

cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

chkconfig --add mysqld
chkconfig mysqld on
echo 'PATH=/app/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
service mysqld start

运行脚本后截图
第十三周作业_第7张图片
在这里插入图片描述
第十三周作业_第8张图片

你可能感兴趣的:(Linux作业)