NIS服务器部署
NIS服务器介绍
- 核心功能
- 集中存储和分发网络配置信息(如用户账户 /etc/passwd、主机名映射 /etc/hosts、组信息 /etc/group),避免多台主机单独维护相同数据。
- 通过映射(Mappings)机制将文本文件转换为DBM数据库格式,提升查询效率。
- 设计目标
- 简化管理:管理员在单一服务器更新数据,自动同步至全网,减少配置错误。
- 高效查询:客户端通过RPC(远程过程调用)向服务器请求数据,支持广播或指定服务器绑定模式。
安装相关软件
yum install -y ypserv
设定域名及端口
NIS 的域名为 eda.com
整个内部的信任网域为 192.168.7.0/24
NIS master server 的 IP 为 192.168.7.51 ,主机名为 nis.eda.com
vim /etc/sysconfig/network
# 设定 NIS 领域名
NISDOMAIN=eda.com
# 设定 NIS 每次都启动在固定的埠口
YPSERV_ARGS="-p 1011"
配置nis的配置文件
vim /etc/ypserv.conf
# NIS 服务器大多使用于内部局域网络,只要有 /etc/hosts 即可
dns: no
# 预设会有 30 个数据库被读入内存当中,其实我们的账号档案并不多,30 个够用了。
files: 30
# 与 master/slave 有关,将同步更新的数据库比对所使用的端口,放置于 <1024 内。
xfr_check_port: yes
# 底下则是设定限制客户端或 slave server 查询的权限,利用冒号隔成四部分:
# [主机名/IP] : [NIS域名] : [可用数据库名称] : [安全限制]
# [主机名/IP] :可以使用 network/netmask 如 192.168.100.0/255.255.255.0
# [NIS域名] :例如本案例中的 eda.com
# [可用数据库名称]:就是由 NIS 制作出来的数据库名称;
# [安全限制] :包括没有限制 (none)、仅能使用 <1024 (port) 及拒绝 (deny)
# 一般来说,你可以依照我们的网域来设定成为底下的模样:
127.0.0.0/255.255.255.0 : * : * : none
192.168.100.0/255.255.255.0 : * : * : none
* : * : * : deny
# 星号 (*) 代表任何数据都接受的意思。上面三行的意思是,开放 lo 内部接口、
# 开放内部 LAN 网域,且杜绝所有其他来源的 NIS 要求的意思。
# 还有一个简单作法,你可以先将上面三行批注,然后加入底下这一行即可:
* : * : * : none
修改nsswitch文件
# vim /etc/nsswitch.conf
passwd: files nis
shadow: files nis
group: files nis
设定主机名与 IP 的对应 /etc/hosts
vim /etc/hosts
# 原本就有的 localhost 与 127.0.0.1 之类的设定都不要更动,只要新增数据:
192.168.7.50 yum.eda.com
192.168.7.51 nis.eda.com
启动相关服务并检查
# 为了也让 yppasswdd 启动在固定的端口,方便防火墙的管理
vim /etc/sysconfig/yppasswdd
YPPASSWDD_ARGS="--port 1012"
# 主要的 NIS 服务是 ypserv
# 提供 NIS 客户端的密码修改功能是yppasswdd
systemctl enable --now ypserv
systemctl enable --now yppasswdd
# 检查rpc服务
rpcinfo -p localhost
rpcinfo -u localhost ypserv
#案例
[root@nis ~]# rpcinfo -p localhost
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100004 2 udp 1011 ypserv
100004 1 udp 1011 ypserv
100004 2 tcp 1011 ypserv
100004 1 tcp 1011 ypserv
100009 1 udp 1012 yppasswdd
[root@nis ~]# rpcinfo -u localhost ypserv
program 100004 version 1 ready and waiting
program 100004 version 2 ready and waiting
设置账号登入认证机制
vim /etc/sysconfig/authconfig
USENIS=yes
设置PAM授权
vim /etc/pam.d/system-auth
#增加nis
password sufficient pam_unix.so sha512 shadow nis nullok try_first_pass use_authtok
新建账号并建立数据库
如果用户密码有变动,就要重新制作数据库并重新启动 ypserv 及 yppasswdd
账号设定,最后是安装了autofs设定了NFS共享之后再设定账号
# 新建账号edaadmin并设定密码123Qwe,.
useradd -u 1001 edaadmin
echo 123Qwe,. | passwd --stdin edaadmin
# 将建立的帐密数据转成数据库,通过 /usr/lib64/yp/ypinit 这个指令来处理
/usr/lib64/yp/ypinit -m
# 案例
[root@nis ~]# /usr/lib64/yp/ypinit -m
At this point, we have to construct a list of the hosts which will run NIS
servers. nis.eda.com is in the list of NIS server hosts. Please continue to add
the names for the other hosts, one per line. When you are done with the
list, type a <control D>.
next host to add: nis.eda.com
next host to add: <==这个地方按下 [crtl]-d
The current list of NIS servers looks like this:
nis.eda.com
Is this correct? [y/n: y] y
We need a few minutes to build the databases...
Building /var/yp/eda.com/ypservers...
Running /var/yp/Makefile...
gmake[1]: Entering directory `/var/yp/eda.com'
Updating passwd.byname...
Updating passwd.byuid...
Updating group.byname...
Updating group.bygid...
Updating hosts.byname...
Updating hosts.byaddr...
Updating rpc.byname...
Updating rpc.bynumber...
Updating services.byname...
Updating services.byservicename...
Updating netid.byname...
Updating protocols.bynumber...
Updating protocols.byname...
Updating mail.aliases...
gmake[1]: Leaving directory `/var/yp/eda.com'
nis.eda.com has been set up as a NIS master server.
Now you can run ypinit -s nis.eda.com on all slave server.
同步hosts
# nis服务器上修改完hosts后,同步数据到hosts.byname
cd /var/yp/
make
# nis客户端上可以通过ypcat查看
ypcat hosts.byname
NIS客户端
安装相关软件
yum install -y ypbind yp-tools
systemctl enable --now ypbind
加入nis域
authconfig --enablenis --nisdomain=eda.com --nisserver=192.168.7.51 --updateall
#查看hosts是否同步
ypcat hosts.byname
相关文件
/etc/sysconfig/network (加入 NISDOMAIN 项目)
/etc/nsswitch.conf (修改许多主机验证功能的顺序)
/etc/sysconfig/authconfig (CentOS 的认证机制)
/etc/pam.d/system-auth (许多登入所需要的 PAM 认证过程)
/etc/yp.conf (亦即是 ypbind 的配置文件)
NIS用户登录自动挂载
服务器端安装autofs
NFS服务器FQDN: nfs.eda.com
NFS服务器导出目录:
/rhome
/data
# 安装相关软件
yum install -y autofs nfs-utils
systemctl enable --now autofs
systemctl enable --now nfs
服务器端设定autofs
vim /etc/auto.master
##
## Sample auto.master file
## This is a 'master' automounter map and it has the following format:
## mount-point [map-type[,format]:]map [options]
## For details of the format look at auto.master(5).
##
#/misc /etc/auto.misc
##
## NOTE: mounts done from a hosts map will be mounted with the
## "nosuid" and "nodev" options unless the "suid" and "dev"
## options are explicitly given.
##
#/net -hosts
##
## Include /etc/auto.master.d/*.autofs
## The included files must conform to the format of this file.
##
#+dir:/etc/auto.master.d
##
## Include central master map if it can be found using
## nsswitch sources.
##
## Note that if there are entries for /net or /misc (as
## above) in the included master map any keys that are the
## same will not be seen as the first read key seen takes
## precedence.
##
#+auto.master
/home auto.home nfsvers=3,tcp,soft,intr,bg,rsize=32768,wsize=32768
/tools auto.app nfsvers=3,tcp,soft,intr,bg,rsize=32768,wsize=32768
/license auto.license nfsvers=3,tcp,soft,intr,bg,rsize=32768,wsize=32768
/software auto.software nfsvers=3,tcp,soft,intr,bg,rsize=32768,wsize=32768
vim /etc/auto.home
* -rw nfs.eda.com:/rhome/&
vim /etc/auto.app
synopsys -rw nfs.eda.com:/data/tools/synopsys
mentor -rw nfs.eda.com:/data/tools/mentor
cadence -rw nfs.eda.com:/data/tools/cadence
lsf -rw nfs.eda.com:/data/tools/lsf
svn -rw nfs.eda.com:/data/tools/svn
vim /etc/auto.license
license -rw nfs.eda.com:/data/license
vim /etc/auto.software
software -rw nfs.eda.com:/data/software
服务器端修改nsswitch.conf
vim /etc/nsswitch.conf
passwd: files nis
shadow: files nis
group: files nis
#修改automount
automount: files nis
服务器端修改Makefile
vim /var/yp/Makefile
#修改一
AUTO_MASTER = $(YPSRCDIR)/auto.master
AUTO_HOME = $(YPSRCDIR)/auto.home
AUTO_APP = $(YPSRCDIR)/auto.app
AUTO_LICENSE = $(YPSRCDIR)/auto.license
AUTO_SOFTWARE = $(YPSRCDIR)/auto.software
#修改二
all: passwd group hosts rpc services netid protocols mail \
auto.master auto.home auto.app auto.license auto.software
#修改三(这里要注意,一定不能有“空格”,必须是“table”)
auto.master: $(AUTO_MASTER) $(YPDIR)/Makefile
@echo "Updating $@..."
-@sed -e "/^#/d" -e s/#.*$$// $(AUTO_MASTER) | $(DBLOAD) \
-i $(AUTO_MASTER) -o $(YPMAPDIR)/$@ - $@
-@$(NOPUSH) || $(YPPUSH) -d $(DOMAIN) $@
auto.home: $(AUTO_HOME) $(YPDIR)/Makefile
@echo "Updating $@..."
-@sed -e "/^#/d" -e s/#.*$$// $(AUTO_HOME) | $(DBLOAD) \
-i $(AUTO_HOME) -o $(YPMAPDIR)/$@ - $@
-@$(NOPUSH) || $(YPPUSH) -d $(DOMAIN) $@
auto.app: $(AUTO_APP) $(YPDIR)/Makefile
@echo "Updating $@..."
-@sed -e "/^#/d" -e s/#.*$$// $(AUTO_APP) | $(DBLOAD) \
-i $(AUTO_APP) -o $(YPMAPDIR)/$@ - $@
-@$(NOPUSH) || $(YPPUSH) -d $(DOMAIN) $@
auto.license: $(AUTO_LICENSE) $(YPDIR)/Makefile
@echo "Updating $@..."
-@sed -e "/^#/d" -e s/#.*$$// $(AUTO_LICENSE) | $(DBLOAD) \
-i $(AUTO_LICENSE) -o $(YPMAPDIR)/$@ - $@
-@$(NOPUSH) || $(YPPUSH) -d $(DOMAIN) $@
auto.software: $(AUTO_SOFTWARE) $(YPDIR)/Makefile
@echo "Updating $@..."
-@sed -e "/^#/d" -e s/#.*$$// $(AUTO_SOFTWARE) | $(DBLOAD) \
-i $(AUTO_SOFTWARE) -o $(YPMAPDIR)/$@ - $@
-@$(NOPUSH) || $(YPPUSH) -d $(DOMAIN) $@
#服务器和客户端都重启autofs、nfs进程
systemctl restart autofs
systemctl restart nfs
#执行检查
/usr/lib64/yp/ypinit -m
登录账号后出现 -bash-4.2$处理方法
cp /etc/skel/.bashrc /home/edaadmin
cp /etc/skel/.bash_profile /home/edaadmin
创建共享NFS上的用户总结:
mkdir /rhome/edaadmin
useradd -m -u 2001 -G root edaadmin
#会有2个报警提示
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
#解决这个问题,在NIS服务器上copy2个文件的到/home/edaadmin(此处的home挂载了NFS的/rhome)
cp /etc/skel/.bashrc /home/edaadmin
cp /etc/skel/.bash_profile /home/edaadmin
/usr/lib64/yp/ypinit -m
automount -m