目录
NIS
一、简介
1.1NIS的产生
正所谓有需求才会有产品,有问题才会有方法去解决,那么NIS是怎么产生的呢?
在一个大型的网域中,如果有多部Linux主机,而且需要每台主机都设定相同的帐号与密码时,是十分麻烦的。此时,如果能够有一台NIS主控制服务器(master server)来管理该网域中所有主机的帐号密码,当其他的主机有用户登录的需求时,才到这台服务器上请求相关的帐号密码等使用者资料,这样一来,如果想要增加、修改、删除用户的资料,只需要到这台服务器上面处理即可,这样就能够大大降低重复设定用户帐号密码的步骤,便于管理。NIS(Network Information Services) Server就可以实现这样的功能。
1.2什么是NIS?
网络信息服务(Network Information Service)是集中控制几个系统管理数据库的网络用品。NIS简化了UNIX和LINUX桌面客户的管理工作,客户端利用它可以使用中心服务器的管理文件。桌面系统的用户无需建立他们自己的/etc/passwd,他们只简单的使用维护在NIS服务器的文件即可。用户只需要在nis服务器上面创建,客户端加入nis域,直接可以使用Nis域上的用户来登录客户端。
二、NIS的相关组件
服务器端文件名 |
档案内容 |
/etc/passwd |
提供用户账号、UID、GID、家目录所在、Shell 等等 |
/etc/group |
提供群组数据以及 GID 的对应,还有该群组的加入人员 |
/etc/hosts |
主机名与 IP 的对应,常用于 private IP 的主机名对应 |
/etc/services |
每一种服务 (daemons) 所对应的端口号 (port number) |
/etc/protocols |
基础的 TCP/IP 封包协定,如 TCP, UDP, ICMP 等 |
/etc/rpc |
每种 RPC 服务器所对应的程序号码 |
/var/yp/ypservers |
NIS 服务器所提供的数据库 |
/etc/netgroup |
网络群组的定义与使用 |
2.1服务端
2.1.1配置文件
配置文件名 |
文件内容 |
/etc/ypserv.conf |
这是最主要的 ypserv 软件所提供的配置文件,可以规范 NIS 客户端是否可登入的权限。 |
/etc/hosts |
由于 NIS server/client 会用到网络主机名与IP的对应,因此这个主机名对应档就显的相当重要!每一部主机名与 IP 都需要记录才行! |
/etc/netgroup |
提供组数据及GID的对应。 |
/etc/sysconfig/network |
可以在这个档案内指定 NIS 的网域 (nisdomainname)。 |
/var/yp/Makefile |
这就是与建立数据库有关的动作配置文件; |
2.1.2主要服务
主要服务名 |
功能 |
/usr/sbin/ypserv |
就是 NIS 服务器的主要提供服务 |
/usr/sbin/rpc.ypxfrd |
用于Master/Slave主机之间的传输数据库的服务。 |
/usr/sbin/rpc.yppasswd |
提供额外的 NIS 客户端之用户密码修改服务, 通过这个服务, NIS 客户端可以直接修改在 NIS 服务器上的密码。相关的使用程序则是 yppasswd 指令;(修改密码时,NIS客户端与NIS服务器端之前通过这个服务来通信) |
2.1.3数据库相关指令
相关指令位置 |
功能 |
/usr/lib64/yp/ypinit |
建立数据库的指令,非常常用 (在 32 位的系统下,文件名则是 /usr/lib/yp/ypinit ); |
/usr/sbin/yppush |
master 主机将数据库直接送至 slave 的指令; |
/usr/lib/yp/ypxfr |
传送数据库的指令,在该目录下尚有 ypxfr_1perday, ypxfr_1perhour, ypxfr_2perday 等等。 |
2.2客户端
2.2.1配置文件
配置文件名 |
文件内容 |
/etc/hosts |
主机名与IP地址对应关系 |
/etc/yp.conf |
ypbind的主要配置文件,设定NIS Server |
/etc/nsswitch.conf |
重要的配置文件,设定帐号密码等信息 |
/var/yp/Makefile |
与建立资料库有关的配置文件的查询顺序 |
2.2.2主要指令
相关指令名 |
功能 |
/usr/bin/yppasswd |
更改客户端登录用户在NIS服务器上的密码 |
/usr/bin/ypchsh |
更改客户端登录用户默认登录shell |
/usr/bin/ypchfn |
更改客户端登录用户的finger信息 |
三、NIS环境
3.1 NIS所需的软件包
- yp-tools :提供 NIS 相关的查寻指令功能
- ypbind :提供 NIS Client 端的设定软件
- ypserv :提供 NIS Server 端的设定软件
- portmap :这是 RPC必须的软件!
什么是RPC?请点这里
相关端口:portmap :111,因为NIS、NFS 都是依赖于portmap服务 6.0/7.0 版本用rpcbind取代portmap服务
相关包:ypserv(NIS服务器必需的),ypbind(NIS客户端必需的,默认已安装),yp-tools(NIS的常用工具,默认已安装)
3.2 NIS Server (Master/Slave)
NIS服务器包括主从服务器两种,从的作用就是备份,即当主服务器出现故障后,从能直接使用
主(Master):将文件建成数据库,并提供给Slave来更新;
从(Slave):以Master的数据库作为本身的数据库来源;
详细:
1. Master先将帐号密码相关文件制作成数据库文件;
2. Master可以主动告诉Slave来更新;
3. Slave亦可以主动前往Master取得更新;
4.若有帐号密码变动时,需要重新制作数据库文件并重新同步Master/Slave。
3.3 NIS Client
1.NIS client 若有登入需求时,会先查询其本的 /etc/passwd, /etc/shadow 等档案;
2.若在 NIS Client 本机找不到相关的账号数据,才开始向整个 NIS 网域的主机广播查询.
3.每个 NIS server (不论 master/slave) 都可以响应,基本上是『先响应者优先』。
四、搭建NIS
4.1服务器端
1.安装ypserv服务相关的包
2、建立一个Nis专用用户目录文件夹
3、建立多个NIS用户,并为其配置密码
4、标明NIS服务器,并启动ypserv服务
a.编辑/etc/sysconfig/network为NIS服务器指定NIS网域名,,可以不用写www这个主机名,直接声明成二级域名也行。
b. 使当前主机识别NIS域名 当前生效,重启后不生效; 若要开机自动生效(永久生效),进入这个配置文件(开机就会运行这个配置文件),当然还是需要输入nisdomainname www.example.com来使当前主机识别NIS域名
5.将服务器上的用户文件加载成数据库文件(该服务器上的所有用户名都将用作客户端身份认证),先关闭防火墙,开启ypserv服务后加载数据库
注意,每次添加用户之后,都需要重新生成nis数据库
备注:提示输入NIS服务器主机名,有多个可以输入多个,按Y确认。
配置完以上命令后在/var/yp下检查有没有生成一个NIS域名命名的目录,这是NIS用户数据库,如果没有则删除该目录下的’”(none)”目录,再重新生成数据库。
[root@NIS-Server ~]# yum install ypserv -y
[root@NIS-Server ~]# mkdir /nishome
[root@NIS-Server ~]# useradd -d /nishome/user1 user1
[root@NIS-Server /]# useradd -d /nishome/user2 user2
[root@NIS-Server /]# useradd -d /nishome/user3 user3
[root@NIS-Server /]# useradd -d /nishome/user4 user4
[root@NIS-Server user1]# echo 123123 | passwd --stdin user1
Changing password for user user1.
passwd: all authentication tokens updated successfully.
[root@NIS-Server /]# echo 123123 | passwd --stdin user2
Changing password for user user2.
passwd: all authentication tokens updated successfully.
[root@NIS-Server /]# echo 123123 | passwd --stdin user3
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@NIS-Server /]# echo 123123 | passwd --stdin user4
Changing password for user user4.
passwd: all authentication tokens updated successfully.
[root@NIS-Server /]# vim /etc/sysconfig/network
[root@NIS-Server /]# cat /etc/sysconfig/network
# Created by anaconda
nisdomain=www.example.com
[root@NIS-Server /]# nisdomainname www.example.com
[root@NIS-Server /]# nisdomainname
www.example.com
[root@NIS-Server /]# systemctl stop firewalld
[root@NIS-Server /]# setenforce 0
[root@NIS-Server /]# systemctl start ypserv.service
[root@NIS-Server /]# systemctl status ypserv.service
● ypserv.service - NIS/YP (Network Information Service) Server
Loaded: loaded (/usr/lib/systemd/system/ypserv.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2020-05-06 12:17:08 ULAST; 5s ago
Main PID: 6862 (ypserv)
Status: "Processing requests..."
CGroup: /system.slice/ypserv.service
└─6862 /usr/sbin/ypserv -f
May 06 12:17:08 NIS-Server systemd[1]: Starting NIS/YP (Network Information Service) Server...
May 06 12:17:08 NIS-Server ypserv[6862]: WARNING: no securenets file found!
May 06 12:17:08 NIS-Server systemd[1]: Started NIS/YP (Network Information Service) Server.
[root@NIS-Server /]# /usr/lib64/yp/ypinit -m
At this point, we have to construct a list of the hosts which will run NIS
servers. NIS-Server 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-Server
next host to add:
The current list of NIS servers looks like this:
NIS-Server
Is this correct? [y/n: y] y
We need a few minutes to build the databases...
Building /var/yp/www.example.com/ypservers...
gethostbyname(): Success
Running /var/yp/Makefile...
gmake[1]: Entering directory `/var/yp/www.example.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/www.example.com'
NIS-Server has been set up as a NIS master server.
Now you can run ypinit -s NIS-Server on all slave server.
4.2客户端
1.装包,ypbind(NIS客户端必需的,默认已安装),yp-tools(NIS的常用工具,默认已安装),安装ypbind时,也把yp tools安装了
2.利用setup进入图形化界面
a)验证配置
b)使用nis
c)标明nis域
[root@NIF-Client ~]# yum install ypbind -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package ypbind.x86_64 3:1.37.1-7.el7 will be installed
--> Processing Dependency: yp-tools for package: 3:ypbind-1.37.1-7.el7.x86_64
--> Running transaction check
---> Package yp-tools.x86_64 0:2.14-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================================
Package Arch Version Repository Size
===================================================================================================================
Installing:
ypbind x86_64 3:1.37.1-7.el7 base 62 k
Installing for dependencies:
yp-tools x86_64 2.14-3.el7 base 79 k
Transaction Summary
===================================================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 141 k
Installed size: 299 k
Downloading packages:
-------------------------------------------------------------------------------------------------------------------
Total 1.5 MB/s | 141 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 3:ypbind-1.37.1-7.el7.x86_64 1/2
Installing : yp-tools-2.14-3.el7.x86_64 2/2
base/productid | 1.6 kB 00:00:00
Verifying : yp-tools-2.14-3.el7.x86_64 1/2
Verifying : 3:ypbind-1.37.1-7.el7.x86_64 2/2
Installed:
ypbind.x86_64 3:1.37.1-7.el7
Dependency Installed:
yp-tools.x86_64 0:2.14-3.el7
Complete!
[root@NIF-Client ~]#
[root@NIF-Client ~]# setup
4.3验证测试:
客户端本地并没有user1这个用户,但是id一下却能查询出来uid、gid等信息,这是因为NFS环境下的客户端查询用户的方式是
1.NIS client 若有登入需求时,会先查询其本的 /etc/passwd, /etc/shadow 等档案;
2.若在 NIS Client 本机找不到相关的账号数据,才开始向整个 NIS 网域的主机广播查询.
3.每个 NIS server (不论 master/slave) 都可以响应,基本上是『先响应者优先』。
客户端切换进NIS域用户,不能正常显示是因为没有服务器的这三个隐藏文件
[root@NIF-Client ~]# su - user1
su: warning: cannot change directory to /nishome/user1: No such file or directory
-bash-4.2$ whoami
user1
-bash-4.2$
解决:
使用NFS将服务端的nishome目录共享出去,在客户端将NFS的服务端共享的目录挂载到本地即可,这里我采用自动挂载
- 服务端开启NFS,并共享/nishome目录
- 客户端安装autofs包,使用通配符的方式,将服务端的nishome下的用户都挂载到客户端
服务端的NFS服务的配置
[root@NIS-Server ~]# systemctl stop firewalld
[root@NIS-Server ~]# setenforce 0
[root@NIS-Server ~]# systemctl start nfs
[root@NIS-Server ~]# systemctl status nfs
● nfs-server.service - NFS server and services
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
Active: active (exited) since Wed 2020-05-06 12:46:26 ULAST; 7s ago
Process: 7803 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
Process: 7802 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
Main PID: 7803 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/nfs-server.service
May 06 12:46:25 NIS-Server systemd[1]: Starting NFS server and services...
May 06 12:46:26 NIS-Server systemd[1]: Started NFS server and services.
[root@NIS-Server ~]# vim /etc/exports
[root@NIS-Server ~]# cat /etc/exports
/nishome *(ro)
[root@NIS-Server ~]# exportfs -rv
exporting *:/nishome
[root@NIS-Server ~]# showmount -e
clnt_create: RPC: Unknown host
[root@NIS-Server ~]# showmount -e 192.168.211.133
Export list for 192.168.211.133:
/nishome *
[root@NIS-Server ~]#
客户端的NFS服务的挂载配置
第一步 装包
第二步 在autofs的主配置文件中定义挂载点根目录的子配置文件
第三步 配置子配置文件(挂载点目录,触发目录,即需要的时候自动挂载产生),这里利用通配符的方式,将服务器端对应的用户挂载到客户端的通配符(用户名都是相同的)用户上;重启服务
[root@NIF-Client ~]# systemctl stop firewalld
[root@NIF-Client ~]# setenforce 0
[root@NIF-Client ~]# systemctl start autofs
[root@NIF-Client ~]# vim /etc/auto.master
/home /etc/auto.nis
[root@NIF-Client ~]# vim /etc/auto.nis
[root@NIF-Client ~]# cat /etc/auto.nis
* -fstype=nfs 192.168.211.133:/nishome/&
[root@NIF-Client ~]# systemctl restart autofs
验证查看:
由于是自动挂载,即需要触发挂载点目录才会生成文件目录,所以没有触发时是没有文件目录的,如下
现在开始触发服务端共享的/nishome目录下的user1目录,权限被拒绝是因为服务端的这个文件对于其他用户是没有执行操作的,注意,这里是目录权限的拒绝,并不是文件系统层的拒绝
解决:
在服务端更改/nishome目录的权限
现在去客户端触发挂载点目录,可见是可以成功的,隐藏文件也被共享了过来,现在客户端就可以正常的在本地去切换NIS域中的用户了