Ubuntu 20.04 LTS : Configure NIS Server : Server World (server-world.info)

以下为 Ubuntu 20.04 及以下版本的安装方法,在 Ubuntu 22.04 中,NIS服务被拆分。

0. 安装依赖

1
sudo apt update && apt install rpcbind nis -y

安装 nis 后会提示输入域名。如果要修改域名,则输入以下命令:

1
sudo dpkg-reconfigure nis

:star: Note: 在服务器端与客户端都需要安装相关依赖。

1. 配置服务器端

依赖安装成功后,需要修改三个配置文件:

  • /etc/default/nis :配置是否为master还是client
  • /etc/ypserv.securenets :配置网络号以指定是否对NIS Server有访问权限(该配置信息只需在Server端配置)
  • /etc/hosts :绑定IP地址与域名;将NIS域名与本机iP地址绑定
  • /etc/yp.conf :设置NIS域名与服务器IP地址

配置文件修改后,重启相关服务:sudo systemctl restart rpcbind nis

后使用 /usr/lib/yp/ypinit -m 命令初始化数据库。

示例命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
root@dlp:~# vi /etc/default/nis
# line 6: change (set NIS primary server)
NISSERVER=master
root@dlp:~# vi /etc/ypserv.securenets
# This line gives access to everybody. PLEASE ADJUST!
# comment out
# 0.0.0.0 0.0.0.0
# add to the end: IP range you allow to access
255.255.255.0 10.0.0.0

root@dlp:~# vi /etc/hosts
127.0.0.1 localhost
# add own IP address for NIS
10.0.0.30 dlp.srv.world dlp

root@dlp:~# systemctl restart rpcbind nis
# update NIS database
root@dlp:~# /usr/lib/yp/ypinit -m
At this point, we have to construct a list of the hosts which will run NIS
servers. dlp.srv.world 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: dlp.srv.world
next host to add: # Ctrl + D key
The current list of NIS servers looks like this:

dlp.srv.world

Is this correct? [y/n: y] y
We need a few minutes to build the databases...
Building /var/yp/srv.world/ypservers...
Running /var/yp/Makefile...
make[1]: Entering directory '/var/yp/srv.world'
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 netgroup...
Updating netgroup.byhost...
Updating netgroup.byuser...
Updating shadow.byname... Ignored -> merged with passwd
make[1]: Leaving directory '/var/yp/srv.world'

dlp.srv.world has been set up as a NIS master server.

Now you can run ypinit -s dlp.srv.world on all slave server.

此时的数据库已经初始化成功,如果后续在服务器端创建了新用户,则需要更新数据库:

1
sudo make -C /var/yp/

该命令也等同于

1
cd /var/yp && sudo make

2. 配置客户端

安装依赖与服务器端相同,客户端需修改的配置文件如下:

  • /etc/yp.conf :ypbind 的配置文件,理解为 Client 要连接的 NIS Server,只有客户端需要配置该文件
  • /etc/nsswitch.conf :指明NIS要共享的配置文件,如 passwd, group, hosts, etc.
  • /etc/pam.d/common-session :Linux-PAM 用来配置系统的认证任务,如 su;该配置文件用以配置pam-session开始和结束时的执行任务

在common-session中添加如下配置

1
session optional        pam_mkhomedir.so skel=/etc/skel umask=077

表示在登录时,若用户home目录为空则自动创建,umask 表示所创建的文件的访问权限(用反码表示),077 实际的读写权限为 700,即仅所属用户可 rwx

示例命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
root@node01:~# vi /etc/yp.conf
#
# yp.conf Configuration file for the ypbind process. You can define
# NIS servers manually here if they can't be found by
# broadcasting on the local net (which is the default).
#
# See the manual page of ypbind for the syntax of this file.
#
# IMPORTANT: For the "ypserver", use IP addresses, or make sure that
# the host is in /etc/hosts. This file is only interpreted
# once, and if DNS isn't reachable yet the ypserver cannot
# be resolved and ypbind won't ever bind to the server.

# ypserver ypserver.network.com
# add to the end: [domain name] [server] [NIS server's hostname]
domain srv.world server dlp.srv.world
root@node01:~# vi /etc/nsswitch.conf
# line 7: add like follows
passwd: files systemd nis
group: files systemd nis
shadow: files nis
gshadow: files

hosts: files dns nis

# set follows if needed (create home directory automatically if none)
root@node01:~# vi /etc/pam.d/common-session
# add to the end
session optional pam_mkhomedir.so skel=/etc/skel umask=077

root@node01:~# systemctl restart rpcbind nis
root@node01:~# exit
node01 login: focal # NIS user
Password:
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage

System information as of Wed 16 Sep 2020 04:22:09 PM JST

System load: 0.28 Processes: 134
Usage of /: 12.4% of 24.54GB Users logged in: 0
Memory usage: 5% IPv4 address for enp1s0: 10.0.0.51
Swap usage: 0%

* "If you've been waiting for the perfect Kubernetes dev solution for
macOS, the wait is over. Learn how to install Microk8s on macOS."

https://www.techrepublic.com/article/how-to-install-microk8s-on-macos/

11 updates can be installed immediately.
8 of these updates are security updates.
To see these additional updates run: apt list --upgradable

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

Creating directory '/home/focal'.
focal@node01:~$ # just logined

# changing NIS password is like follows
focal@node01:~$ yppasswd
hanging NIS account information for focal on dlp.srv.world.
Please enter old password:
Changing NIS password for focal on dlp.srv.world.
Please enter new password:
Please retype new password:

The NIS password has been changed on dlp.srv.world.

focal@node01:~$

3. 更新数据库数据

可以使用 yppasswd 命令快速修改当前账户的密码(会在所有NIS Client上立即生效,否则需要重新make),密码至少要6位。如果要设置小于6位长度的密码,则需要使用 root 权限来修改密码。


Ubuntu 22.04 中的安装方法

Ubuntu 22.04 中安装 NIS 服务器后,并不自动创建服务,NIS服务由 ypbind 管理。其他配置项与上相同。

可能的异常

nisypbind 服务启动,可能是由于客户端无法找到主节点,原因是 /etc/yp.conf 中使用域名表示master,但在hosts中未添加。