#!/bin/bash
###
# @Author: ThreeFlower [email protected]
# @Date: 2024-05-28
# @LastEditors: ThreeFlower [email protected]
# @LastEditTime: 2024-07-19
# @FilePath: NIS-use.sh
# @Description: NIS(Network Information Services) source:https://www.thkukuk.de/nis/sources/
###
# env firewall down 防火墙已关
#设置NIS域名,服务端主机上设置:
name_nisdoamin='www.xxx.com'
export NISDOMAIN="${name_nisdoamin}"
#新建的用户设置的密码
password='Abc@123456'
#NIS服务器IP,后续自行修改
HOSTNAME_IP="$(hostname --all-ip-addresses | awk '{print $1}')"
##NIS服务器IP允许访问网段,后续自行修改
# local_net='192.168.200.0/24'
# server 服务端
## server rpm *
yum install -y ypserv rpcbind
### 设置NIS域名: *
nisdomainname "${name_nisdoamin}"
#NIS域名可以简单用主机名代替下
hostnamectl set-hostname --static "${name_nisdoamin}"
#将域名修改写入/etc/sysconfig/netwrok文件 *
grep NISDOMAIN="${name_nisdoamin}" /etc/sysconfig/network ||echo NISDOMAIN="${name_nisdoamin}" >> /etc/sysconfig/network
### 在/etc/hosts中进行域名解析 *
#判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断部分子集IP在,sed同行追加;&& 判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断空集都不在,最后不行再追加
grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || sed -i 's/^\([[:space:]]*\)'''"${HOSTNAME_IP}"'''[[:space:]]\+\(.*\)/\1'''"${HOSTNAME_IP}"''' \2 '''"${name_nisdoamin}"'''/g' /etc/hosts && grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || echo "${HOSTNAME_IP} ${name_nisdoamin}" >> /etc/hosts
### 设置NIS服务主要配置文件/etc/ypserv.conf *
### 其他条件默认即可需要修改可以参考https://www.thkukuk.de/nis/,追加些允许访问的地址
# 添加规则,格式为:机名/IP:NIS域名:可用数据库名称:安全限制
# 127.0.0.0/255.255.255.0 : * : * : none ##开放内部接口
# 192.168.1.0/255.255.255.0 : * : * : none ##开放局域网
# * : * : * : deny ##其他拒绝
# * : * : shadow.byname : port
# * : * : passwd.adjunct.byname : port
# echo "${local_net} :*:*:none" >> /etc/ypserv.conf
grep '* :*:*:none' /etc/ypserv.conf || echo "* :*:*:none" >> /etc/ypserv.conf
### 启动服务与设置为开机启动
systemctl start ypserv
systemctl start rpcbind
systemctl start yppasswdd.service
systemctl enable ypserv
systemctl enable rpcbind
systemctl enable yppasswdd.service
### 建立NIS帐户
for i in `seq 1 5`;do
echo "=====create nisuser$i=====";
useradd -u 100$i nisuser$i;
echo "${password}" | passwd --stdin nisuser$i;
done
### 建立NIS资料库
/usr/lib64/yp/ypinit -m ##交互式操作,取默认选项,却啥文件touch创建即可,如需在touch中添加配置自行添加,执行完后,数据会存放在/var/yp/"$nisdomainname"中
#### if 新增帐户与修改帐户信息时,需要重新更新数据库
make -C /var/yp
#### if 修改密码时,需对档案进行更新
make -C /var/yp passwd
## server deb
#####以下是客户端配置,去客户端机器上搞,需要就从选一整段取消下注释
# ------------------------------------------------------------------------
# # client 客户端
# ## rpm
# #NIS服务器域名,可以暂时用hostname代替
# name_nisdoamin="www.xxx.com"
# export NISDOMAIN="${name_nisdoamin}"
# #NIS服务器IP,自行填写
# HOSTNAME_IP=""
# ### 安装client包 *
# yum install -y ypbind rpcbind
# ### /etc/hosts 域名解析 *
# #判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断部分子集IP在,sed同行追加;&& 判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断空集都不在,最后不行再追加
# grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || sed -i 's/^\([[:space:]]*\)'''"${HOSTNAME_IP}"'''[[:space:]]\+\(.*\)/\1'''"${HOSTNAME_IP}"''' \2 '''"${name_nisdoamin}"'''/g' /etc/hosts && grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || echo "${HOSTNAME_IP} ${name_nisdoamin}" >> /etc/hosts
# ### /etc/yp.conf 指定NIS服务器 *
# # 强制格式: domain xxxx server xxxx
# sed -i 's/^[[:space:]]*domain[[:space:]]\+\(.*\)[[:space:]]\+server[[:space:]]\+\(.*\)/domain '''"${name_nisdoamin}"''' server '''"${HOSTNAME_IP}"'''/g' /etc/yp.conf && grep "^[[:space:]]*domain[[:space:]]\+${name_nisdoamin}[[:space:]]\+server[[:space:]]\+${HOSTNAME_IP}[[:space:]]*" /etc/yp.conf || echo "domain ${name_nisdoamin} server ${HOSTNAME_IP} " >> /etc/yp.conf
# ### /etc/nsswitch.conf 帐号密码查询顺序,添加查询之后可su切换yptest获取到的用户,例如: *
# # /etc/nsswitch.conf 帐号密码查询顺序
# # passwd: files nis sss
# # shadow: files nis sss
# # group: files nis sss
# # …
# # hosts: files nis dns
# grep "^passwd" /etc/nsswitch.conf | grep nis || sed -i '/^passwd/ s/$/ nis/g' /etc/nsswitch.conf
# grep "^shadow" /etc/nsswitch.conf | grep nis || sed -i '/^shadow/ s/$/ nis/g' /etc/nsswitch.conf
# grep "^group" /etc/nsswitch.conf | grep nis || sed -i '/^group/ s/$/ nis/g' /etc/nsswitch.conf
# ### 启动及开机启动 *
# systemctl start rpcbind
# systemctl start ypbind
# systemctl enalbe rpcbind
# systemctl enable ypbind
# ###验证 进行连接测试,可获取到NIS服务已添加到数据库中的用户
# yptest ##有错误会返回结果,某些错误可以忽略
# ###验证 切换NIS中用户(客户端主机不需要手动添加yptest用户)测试
# su nisuser1
##---------------------
## deb 客户端
#NIS服务器域名,可以暂时用hostname代替
name_nisdoamin="www.xxx.com"
export NISDOMAIN="${name_nisdoamin}"
#NIS服务器IP,自行填写
HOSTNAME_IP=""
### /etc/hosts 域名解析 *
#判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断部分子集IP在,sed同行追加;&& 判断全集,grep 与 判断IP和HOSTNAME在/etc/hosts里没->判断空集都不在,最后不行再追加
grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || sed -i 's/^\([[:space:]]*\)'''"${HOSTNAME_IP}"'''[[:space:]]\+\(.*\)/\1'''"${HOSTNAME_IP}"''' \2 '''"${name_nisdoamin}"'''/g' /etc/hosts && grep "${HOSTNAME_IP}" /etc/hosts | grep "${name_nisdoamin}" || echo "${HOSTNAME_IP} ${name_nisdoamin}" >> /etc/hosts
sudo apt install -y rpcbind nis #安装时会要求配置NIS服务器的域名,后续可在/etc/defaultdoam修改,nis安装肯定报错,需要修改 /etc/hosts,/etc/yp.conf,/etc/nsswitch.conf,再启ypbind,再重新apt install nis
### /etc/yp.conf 指定NIS服务器 *
# 强制格式: domain xxxx server xxxx
sed -i 's/^[[:space:]]*domain[[:space:]]\+\(.*\)[[:space:]]\+server[[:space:]]\+\(.*\)/domain '''"${name_nisdoamin}"''' server '''"${HOSTNAME_IP}"'''/g' /etc/yp.conf && grep "^[[:space:]]*domain[[:space:]]\+${name_nisdoamin}[[:space:]]\+server[[:space:]]\+${HOSTNAME_IP}[[:space:]]*" /etc/yp.conf || echo "domain ${name_nisdoamin} server ${HOSTNAME_IP} " >> /etc/yp.conf
### /etc/nsswitch.conf 帐号密码查询顺序,添加查询之后可su切换yptest获取到的用户,例如: *
# /etc/nsswitch.conf 帐号密码查询顺序
# passwd: files nis sss
# shadow: files nis sss
# group: files nis sss
# …
# hosts: files nis dns
grep "^passwd" /etc/nsswitch.conf | grep nis || sed -i '/^passwd/ s/$/ nis/g' /etc/nsswitch.conf
grep "^shadow" /etc/nsswitch.conf | grep nis || sed -i '/^shadow/ s/$/ nis/g' /etc/nsswitch.conf
grep "^group" /etc/nsswitch.conf | grep nis || sed -i '/^group/ s/$/ nis/g' /etc/nsswitch.conf
#/etc/defaultdomain
echo "${name_nisdoamin}" > /etc/defaultdomain
### 启动及开机启动 *
systemctl start rpcbind
systemctl start ypbind
systemctl enalbe rpcbind
systemctl enable ypbind
###验证 进行连接测试,可获取到NIS服务已添加到数据库中的用户
yptest ##有错误会返回结果,某些错误可以忽略
###验证 切换NIS中用户(客户端主机不需要手动添加yptest用户)测试
su nisuser1
#########清除/etc/nsswitch.conf中的nis ,可取消之前添加的帐号密码查询顺序,取消获取的用户信息
#########systemctl stop ypbind,注释/etc/yp.conf可停止客户端服务
标签:Information,grep,Network,IP,HOSTNAME,etc,conf,Services,name
From: https://www.cnblogs.com/users1367067573/p/18335277