前言

在linux环境下,若要实现一个用户在多台主机上登录,需要怎么操作呢。

使用ldap?不,这过于复杂了,要实现轻量的用户验证环境,只需要配置nis+nfs即可。

nis服务可以集中式管理账户(用户、组、hosts、等等。。),配置过程相对简单。

环境

  • Server

    • ip: 172.16.70.129
    • 安装软件: ypserv(nis服务), nfs-utils(nfs服务)
  • Client

    • ip: 172.16.70.130
    • 安装软件: ypbind(nis客户端), nfs-utils-lib(nfs挂载支持), setuptool(自动配置工具)

nis服务端配置

配置允许访问的网段。

[root@localhost ~]# vi /etc/ypserv.conf
     41 # If you comment out the next rule, ypserv and rpc.ypxfrd will
     42 # look for YP_SECURE and YP_AUTHDES in the maps. This will make
     43 # the security check a little bit slower, but you only have to
     44 # change the keys on the master server, not the configuration files
     45 # on each NIS server.
     46 # If you have maps with YP_SECURE or YP_AUTHDES, you should create
     47 # a rule for them above, that's much faster.
     48 # *                        : *       : *                : none
     49
     50 172.16.70.0/255.255.255.0 : * : * : none

配置nis域的域名。

[root@localhost ~]# vi /etc/sysconfig/network
      1 NETWORKING=yes
      2 HOSTNAME=localhost.localdomain
      3 NISDOMAIN=test.com

建立用户,初始化数据库,环境配置。

[root@localhost ~]# useradd user1
[root@localhost ~]# useradd user2
[root@localhost ~]# echo 123456|passwd --stdin user1
更改用户 user1 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]# echo 123456|passwd --stdin user2
更改用户 user2 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]# service rpcbind restart
停止 rpcbind:                                             [失败]
正在启动 rpcbind:                                         [确定]
[root@localhost ~]# service ypserv restart
停止 YP 服务器的服务:                                     [失败]
设置 NIS 域名 test.com:                                   [确定]
启动 YP 服务器的服务:                                     [确定]
[root@localhost ~]# /usr/lib64/yp/ypinit -m
At this point, we have to construct a list of the hosts which will run NIS
servers.  localhost 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:  localhost
    next host to add:  # 此处按下ctrl+d即可。
The current list of NIS servers looks like this:

localhost

Is this correct?  [y/n: y]
We need a few minutes to build the databases...
Building /var/yp/test.com/ypservers...

...

localhost has been set up as a NIS master server.

Now you can run ypinit -s localhost on all slave server.

到这里,nis服务就配置完成了。

nis客户端配置

客户端的配置就相当简单了,使用setup工具即可。

[root@localhost ~]# setup
# 第一页确认即可。
# 第二页选中nis项(User Infomation,左侧),next.
# 第三页,填写域名及nis服务器ip,确认。
# 配置完成后退出即可。(正常情况1-3秒即可配置完成)
正在启动 rpcbind:                                         [确定]
启动 NIS 服务:                                            [确定]
绑定 NIS 服务:.                                           [确定]
[root@localhost ~]#

到此,nis客户端配置完成。

nfs服务配置

由于使用远程账户验证,那么客户端登录远程用户时,在本地没有对应的home目录。

以下为使用user1从Client登录的情况。

Ojo-Laptop:Blog hsojo$ ssh user1@172.16.70.130
user1@172.16.70.130's password:
Could not chdir to home directory /home/user1: No such file or directory
-bash-4.1$

可见user1已经可以在Client正常登录了,但是没有家目录,所以没法正常工作。

那么为了实现家目录能在Server与Client之间实时同步操作,可以使用nfs服务实现。

由于普通用户的home目录处于/home位置下,所以只需要对/home配置nfs即可。

[root@localhost ~]# vi /etc/exports
      1 /home 172.16.70.0/255.255.255.0(rw)
[root@localhost ~]# service nfs restart
关闭 NFS 守护进程:                                        [确定]
关闭 NFS mountd:                                          [确定]
关闭 NFS 服务:                                            [确定]
Shutting down RPC idmapd:                                  [确定]
启动 NFS 服务:                                            [确定]
启动 NFS mountd:                                          [确定]
启动 NFS 守护进程:                                        [确定]
正在启动 RPC idmapd:                                      [确定]
[root@localhost ~]#

nfs客户端配置

方法1: 直接把Server的/home,挂载到Client的/home。(不推荐,不过简单粗暴)

方法2: 参考关于AutoFS的文档,实现自动按需挂载。

以下为配置完成,正常登录的结果(使用方法1,最快的方法)。

Ojo-Laptop:Blog hsojo$ ssh user1@172.16.70.130
user1@172.16.70.130's password:
Last login: Thu Jul 12 21:49:14 2018 from 172.16.70.1
[user1@localhost ~]$ mount|grep home
172.16.70.129:/home on /home type nfs (rw,vers=4,addr=172.16.70.129,clientaddr=172.16.70.130)
[user1@localhost ~]$