#!/bin/bash
#this script is install nis server
#this nis server
yum -y install xinetd
yum -y install yp-tools ypbind ypserv portmap
read -p "please input a domain name for host(like:abc.com):" domain
read -p "please input a hostname for host:" hname
read -p "add a user for nis client:" user
echo "==========set password for $user:=========="
passwd $user
CONF=/etc/ypserv.conf
IP=`ifconfig | grep "inet addr" | grep -v "127.0.0.1" | cut -d: -f2 | cut -d' ' -f1`
MASK=`ifconfig | grep Mask | grep -v "127.0.0.1" | cut -d: -f4`
NIP=`echo $IP | awk -F . -v OFS="." '{print $1,$2,$3}'` #print ip the three before(取ip的前三位,也就是取网段)
sed -i "s/`hostname`/$hname.$domain/g" /etc/sysconfig/network
echo "NISDOMAIN=$domain" >>/etc/sysconfig/network
echo "/bin/nisdomainname $domain" >> /etc/rc.d/rc.local
sed -i "/127.0.0.1/s/localhost/$hname/" /etc/hosts
sed -i "/127.0.0.1/s/localhost.localdomain/$hname.$domain/" /etc/hosts
echo "$IP $hname $hname.$domain " >> /etc/hosts
nisdomainname $domian
sed -i '/port/s/^/# /g' $CONF #把匹配有port的行,前面加上#号
cat >> $CONF << EOF
xfr_check_port: yes
127.0.0.0/255.255.255.0 :* :* :none
$NIP.0/$MASK :* :* :port
:* :* :* :deny
EOF
touch /etc/netgroup
cat >> /var/yp/securenets << EOF
host 127.0.0.1
$MASK $NTP.0
EOF
/usr/lib/yp/ypinit -m
#start Related services
service xinetd restart
service rpcbind restart #IN centos 6.x Portmap has been replaced by rpcbind
service ypserv start
service yppasswd start
chkconfig ypserv on
chkconfig yppasswdd on
chkconfig rpcbind on
#!/bin/bash
#this nis client,this script can not run
yum -y install ypbind yp-tools
#添加服务器的ip与主机名对应的关系
echo " 192.168.1.1 ypserver" >> /etc/hosts
echo "NISDOMAIN=abc.com" >>/etc/sysconfig/network
echo "/bin/nisdomainname abc.com" >> /etc/rc.d/rc.local
sed -i '$a domain abc.com server ypserver.abc.com' /etc/yp.conf
cat >> /etc/nsswitch.conf << EOF
passwd: files nis
shadow: files nis
group: files nis
hosts: files nis dns
EOF
service rpcbind restart #IN centos 6.x Portmap has been replaced by rpcbind
service ypbind start
###测试用的命令
ypwhich -x
ypcat passwd
yptest