︿
Top

1、前言

NIS(Network Information Service) 最早稱為 Sun Yellow Pages (簡稱 YP)但是 Yellow Pages(tm) 是英國 British Telecom 公共有限公司的註冊商標,而且在未經許可下不得使用所以就改名為 NIS,NIS 主要用途為將所有使用者名稱、密碼、主機名稱及群組等與系統管理相關訊息統一集中管理 (若您同時有好幾台 Unix 的機器要管您就了解它的好處在哪裡了),目前主要的 Unix-Like Base 如 (Solaris 、HP-UX 、AIX 、Linux 、NetBSD 、OpenBSD、FreeBSD...等) 都支援 NIS。


NIS 運作方式為 Domain (跟 DNS Domain 一點關系都沒有),簡單說 NIS Serve (Master、Slave) 提供 NIS 服務 (也就是提供 passwd、shadow、hosts、groups、service…等資訊) 而 NIS Client 則透過 RPC (Remote Processure Call) 來存取 NIS Server 中的資訊。

在開始玩 NIS 以前要了解的一些名稱


初始化 NIS Server 指令語法及參數

  • ypinit -m [domainname]: 用於 NIS Master。
  • ypinit -s master_server [domainname]: 用於 NIS Slave。
  • ypinit -u [domainname]: 用於更新 ypservers map。





文章目錄

1、前言
2、實作環境
3、安裝及設定
4、NIS Server 設定 (Master)
          步驟1.修改 /etc/rc.conf
          步驟2.複製身份驗證檔案
          步驟3.初始化 NIS / YP 及相關檔案
          步驟4.啟動 NIS / YP 相關服務
5、NIS Server 設定 (Slave)
          步驟1.修改 /etc/rc.conf
          步驟2.初始化 NIS / YP 及相關檔案
          步驟3.手動啟動 NIS/YP 相關服務
          步驟4.排程定時向 NIS Master 更新 Map
6、NIS Client 設定
          步驟1.複製 master.passwd 及 group
          步驟2.修改 /etc/rc.conf
7、NFS / amd 設定
          步驟1.設定開機自動啟動 (/etc/rc.conf)
          步驟2.開啟 amd 服務
8、SSH-Agent
          步驟1.產生 SSH v2 DSA 公(私)密鑰
          步驟2.登入時自動啟動 ssh-agent
9、參考





2、實作環境

  • FreeBSD 5.2-RELEASE





3、安裝及設定

本次的實作為使用 NIS + NFS + amd + ssh-agent,而 NIS 又分為 NIS Server (Master、Slave) 及 NIS Client 二部份說明。





4、NIS Server 設定 (Master)

步驟 1. 修改 /etc/rc.conf

修改 /etc/rc.conf 以便系統重開時能自動啟動 NIS / YP Server 服務。
vi /etc/rc.conf      //修改 rc.conf 內容如下
 nisdomainname="weithenn"     //設定 NIS Domain 名稱為 weithenn
 nis_server_enable="YES"      //啟動NIS Server
 nis_client_enable="YES"      //否則本機使用者將無法登入
 nis_yppasswdd_enable="YES"   //負責 NIS 密碼部份
 nis_yppasswdd_flags="-t /var/yp/master.passwd"   //指定 NIS 密碼檔路徑
 rpcbind_enable="YES"         //4.x 為啟動 portmap




步驟 2. 複製身份驗證檔案

複製身份驗證檔案 Makefile.dist、master.passwd、group 至 NIS 資料夾下。
cd /var/yp;cp Makefile.dist Makefile //確定 NOPUSH = "True" 沒註解掉
cp /etc/master.passwd /var/yp         //複製本機使用者密碼檔案至 NIS 資料夾下
cp /etc/group /var/yp                  //複製本機群組檔案至 NIS 資料夾下


  • 複製檔案完成後利用 vipw 指令來修改 /var/yp/master.passwd 內容,請移除所有系統帳號,只留下一般使用者帳號 (共享 master.passwd 資訊)
  • 利用 vipw 指令來修改 /etc/master.passwd 內容,請移除所有一般使用者帳號,只留下系統帳號並且在檔案最後一行加上 +::::::::: 來允許所有使用者登錄 (只要在 NIS Server 內有帳號即可登入)

修改 /etc/master.passwd 檔案內容,以設定系統使用 NIS 來接受使用者資訊,並且允許所有的使用者登錄 (只要在 NIS Server 有帳號即可登入)。
vipw    //修改內容
 +:::::::::    //預設允許所有的使用者登錄

若你要禁止某些使用者登入可以這樣寫
 -weithenn:::::::::    //NIS Server 內帳號 weithenn 無法登入這台機器
 +:::::::::            //禁止使用者必須寫在此行之前

利用 vi 指令來修改 /var/yp/group 內容,請移除所有系統群組,只留下一般使用者群組並且在檔案最後一行加上 +:*:0: (共享 group 資訊)。
vi /etc/group
 +:*:0:           //共享 group 資訊




步驟 3. 初始化 NIS / YP 及相關檔案

  • ypinit:初始化 NIS 指令
  • -m:初始化 NIS 指令參數 (NIS Master)
  • NIS Domain:weithenn

利用 ypinit 指令加上參數 -m (Master) 及您所設定的 NIS Domain (此例為 weithenn) 來初始化 NIS。
ypinit -m weithenn       //weithenn 為 nisdomainname
 Server Type: MASTER Domain: weithenn
 Creating an YP server will require that you answer a few questions.
 Questions will all be asked at the beginning of the procedure.
 Do you want this procedure to quit on non-fatal errors? y/n: n      //按 Enter 繼續
 Ok, please remember to go back and redo manually whatever fails.
 If you don't, something might not work.
 Can we destroy the existing /var/yp/weithenn and its contents? y/n: n //按 Enter 繼續
 At this point, we have to construct a list of this domains YP servers.
 gateway.weithenn.idv.tw is already known as master server.
 Please continue to add any slave servers, one per line. When you are
 done with the list, type a <control D>.
 master server : nis1.weithenn.idv.tw
 next host to add:  nis2.weithenn.org     //輸入 NIS Slaver Server Hostname
 next host to add: ^D      //若有多台 NIS Slave 可繼續輸入否則可按 Ctrl+D 離開
 The current list of NIS servers looks like this:
 nis1.weithenn.org
 nis2.weithenn.org
 Is this correct? y/n: y   //鍵入 y 後按 Enter 開始初始化
 ...略...

完成上述步驟後我們必須在 NIS Server 上使用下列指令來讓帳號在 Client 端生效
cd /var/yp
make
 NIS Map update started on Sat Sep 4 10:48:05 CST 2004 for domain weithenn
 NIS Map update completed.




步驟 4. 啟動 NIS / YP 相關服務

使用 domainname 指令來確定 NIS Domain 是否設定成功,並使用如下指令來啟動 NIS 相關服務。
domainname weithenn
/usr/sbin/ypserv
/usr/sbin/ypbind
/usr/sbin/rpc.yppasswdd

以下四個指令 chfn、chsh、chpass、passwd 因為其自動支援 NIS,所以命令前不需要加上 yp 也可。
  • chfn 指令等同於 ypchfn 指令。
  • chsh 指令等同於 ypchsh 指令。
  • chpass 指令等同於 ypchpass 指令。
  • passwd 指令等同於 yppasswd 指令。





5、NIS Server 設定 (Slave)

步驟 1. 修改 /etc/rc.conf

修改 /etc/rc.conf 以便系統重開時能自動啟動 NIS / YP Server 服務。
vi /etc/rc.conf     //修改 rc.conf 內容如下
 nisdomainname="weithenn"   //設定 NIS Domain 為 weithenn
 nis_server_enable="YES"    //NIS Server 啟動
 nis_ypxfrd_enable="YES"    //負責透過網路傳輸 NIS 對應表
 rpcbind_enable="YES"       //4.x 為啟動 portmap




步驟 2. 初始化 NIS / YP 及相關檔案

  • ypinit:初始化 NIS 指令
  • -s:初始化 NIS 指令參數 (NIS Slave)
  • NIS Master Host:nis1.weithenn.org
  • NIS Domain:weithenn

利用 ypinit 指令加上參數 -s (Slave) 及 NIS Master 主機名稱 (nis1) 及您所設定的 NIS Domain (此例為 weithenn) 來初始化 NIS。
ypinit -s nis1 weithenn     //weithenn 為 nisdomainname
 Can't enumerate maps from gateway. Please check that it is running.
 Note: using hardcoded maplist for map transfers.
 Server Type: SLAVE Domain: weithenn Master: nis1      //顯示此台為 NIS Slave
 Creating an YP server will require that you answer a few questions.
 Questions will all be asked at the beginning of the procedure.
 Do you want this procedure to quit on non-fatal errors? y/n: n    //按 Enter 繼續
 ...略...
 nis2.weithenn.org has been setup as an YP slave server without any errors.
 Don't forget to update map ypservers on nis1. 

初始化完成後您應該可在 /var/yp 下看到 nisdomainname (也就是此例中 weithenn) 的資料夾了



步驟 3. 手動啟動 NIS/YP 相關服務

/etc/rc.d/ypserv start     //NIS Server 啟動
/etc/rc.d/ypxfrd start     //負責透過網路傳輸 NIS 對應表




步驟 4. 排程定時向 NIS Master 更新 Map

以下排程內容為 NIS Slave 於每小時的 20、21 分時像 NIS Master 更新 Map。
crontab -e
 20 * * * * root /usr/libexec/ypxfr passwd.byname
 21 * * * * root /usr/libexec/ypxfr passwd.byuid






6、NIS Client 設定

步驟 1. 複製 master.passwd 及 group

複製身份驗證檔案 master.passwd、group 至 NIS 資料夾下。
cp /etc/master.passwd /var/yp    //複製本機使用者密碼檔案至 NIS 資料夾下
cp /etc/group /var/yp            //複製本機群組檔案至 NIS 資料夾下

  • 複製檔案完成後利用 vipw 指令來修改 /var/yp/master.passwd 內容,請移除所有系統帳號,只留下一般使用者帳號 (共享 master.passwd 資訊)
  • 利用 vipw 指令來修改 /etc/master.passwd 內容,請移除所有一般使用者帳號,只留下系統帳號並且在檔案最後一行加上 +::::::::: 來允許所有使用者登錄 (只要在 NIS Server 內有帳號即可登入)
vipw    //修改
 +:::::::::    //預設允許所有的使用者登錄

利用 vi 指令來修改 /var/yp/group 內容,請移除所有系統群組,只留下一般使用者群組並且在檔案最後一行加上 +:*:0: (共享 group 資訊)。
vi /etc/group
 +:*:0:        //共享 group 資訊

NIS Server 也可共享 hosts、resole.conf、nis 資訊給 NIS Client,設定後 NIS Client 查尋 DNS 步驟將為:
  1. 先查詢 /etc/hosts
  2. 再查詢 nameserver (就是 /etc/resolv.conf 設定)
  3. 最後向 nis server 查詢

若 NIS Client 需要使用 NIS Server 資源請在 /etc/host.conf 中加入下列三行設定
 hosts
 bind
 nis

若系統中沒有 host.conf 檔案,可由 /usr/src/release/picobsd/net/floppy.tree/etc/host.conf 複製到 /etc 下再行修改。



步驟 2. 修改 /etc/rc.conf

修改 /etc/rc.conf 以便系統重開時能自動啟動 NIS / YP Client 服務。
vi /etc/rc.conf            //修改 rc.conf 內容如下
 nisdomainname="weithenn"   //設定 NIS Domain 為 weithenn
 nis_client_enable="YES"
 nis_client_flags="-S weithenn,192.168.78.254"  //設定 NIS Server 資訊






7、NFS / amd 設定

為何設置 NFS + amd 呢? 不知您有沒有想過當您用 NIS Client 向 NIS Server 確定完帳號、密碼後登入這台機器時家目錄到底是用 NIS Client 還是在 NIS Server 上,若不管在哪台 NIS Client 登入時家目錄都是使用 NIS Server 這樣在各機器間便利性相信會更好更具有彈性,所以此設定的目的在於從 NIS Server 上開啟 NFS 服務並把本機使用者家目錄共享,以達成我們需要的功能。關於 NFS / amd 的詳細設定可參考本站另一篇文章 NFS-Unix 之間的網路分享功能



步驟 1. 設定開機自動啟動 (/etc/rc.conf)

修改 /etc/rc.conf 以便系統重開機時能自動啟動 nfsd 服務及 amd 服務。
vi /etc/rc.conf      //修改 rc.conf 內容如下
 nfs_reserved_port_only="YES"
 nfs_server_enable="YES"
 rpc_lockd_enable="YES"
 amd_enable="YES"

修改 NFS Server 分享設定檔 (/etc/exports) 設定分享家目錄,記得修改完成後必須重新啟動 mountd 服務其更改才會生效。
vi /etc/exports     //增加如下設定以便分享家目錄
 /home -maproot=0 -alldirs -network 192.168.78.0 -mask 255.255.255.0    




步驟 2. 開啟 amd 服務

AMD (AutoMounter Daemon) 顧名思義為自動掛載服務,使用 amd 的好處為當 NFS Client 掛載 NFS Server 所分享的目錄時會馬上安裝該檔案系統,但經過一段時間間隔內 NFS Client 沒有對 NFS Server 有任何存取動作時,會自動卸載 (umount) 這個檔案系統。

當 amd 服務啟動後可發現檔案系統多了 /host 及 /net,當有連結時則會出現 /.amd_mnt,我們將 /home 建一個 symbolic link 連結到使用者資料伺服器(NIS Server) 例如名為 gw (NIS Client 設定此行以便連結至 NIS Server)。
/etc/rc.d/amd
cd /;ln -sf /host/gw/home    //有把 home 切一個獨立分區






8、SSH-Agent

我們可使用 SSH v2 DSA 公(私)密鑰來達成 SSH 登入遠端主機而不用輸入密碼以便,在各伺服器及工作站間便可通行無阻並搭配之前的 NFS/amd 設定以及 NIS/YP 協同工作,得以保持相同的帳號資訊以及檔案資料。



步驟 1. 產生 SSH v2 DSA 公(私)密鑰

使用 ssh-keygen -d 指令(或 ssh-keygen -t dsa) 來建立 SSH v2 DSA 公(私)密鑰。公(私)密鑰預設皆存放於 ~/.ssh/內而請將 DSA 公共密鑰存放於所要連線遠端機器的 ~/.ssh/authorized_keys 中即可。
ssh-keygen -d          
 Generating public/private dsa key pair.
 Enter file in which to save the key (/home/weithenn/.ssh/id_dsa):  預設路徑
 Enter passphrase (empty for no passphrase):  輸入保護密碼
 Enter same passphrase again: 再次輸入保護密碼
 Your identification has been saved in /home/weithenn/.ssh/id_dsa.
 Your public key has been saved in /home/weithenn/.ssh/id_dsa.pub.
 The key fingerprint is:
 2a:98:c5:27:2b:f9:91:ee:84:bb:27:d2:2f:f0:b7:83 weithenn@weithenn.org
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys  //將公鑰 id_dsa.pub 內容存入 authorized_keys 中




步驟 2. 登入時自動啟動 ssh-agent

以下程式碼來源出自  開放源碼的 Single Sign-On 實作品技術調查與研究---林東毅 Clive Tong-I Lin 。程式碼特色:
  1. For csh/tcsh
  2. 放在 ~/.login 的最後面。
  3. 使用前先 touch ~/.agent
  4. login 後會判斷 ssh-agent 有沒有 run 起來
  5. 有的話,會列出裡面的 key
  6. 沒有的,會幫你 run
  7. Don't panic ! source ~/.login 就好了

我們開始編輯 .login 檔案內容吧。
vi ~/.login           //修改 .login 內容如下
 set agentf=~/.agent
 if ( -f $agentf ) then
  set ssh_agent_pid=`tail -2 $agentf | head -1 | awk '{ print $3}' | sed 's/;//'`
  if ( $?OS ) then # usually cygwin
    set ssh_agent_stat=`ps -f | & grep -v kvm | grep $ssh_agent_pid |grep -v  grep| tail -1 | awk '{print $1}'`
    set _ssh_agent_pid_prog=`ps | & grep -v kvm | grep $ssh_agent_pid | grep -v grep | tail -1 | awk '{print $8}'`
    set ssh_agent_pid_prog=`echo $_ssh_agent_pid_prog | sed -e 's,/usr/bin/,,g'`
  else
    set ssh_agent_stat=`ps -up $ssh_agent_pid | & grep -v kvm |tail -1 | awk '{print $1}'`
    set _ssh_agent_pid_prog=`ps -p $ssh_agent_pid | & grep -v kvm |tail -1 | awk '{print $5}'`
    set ssh_agent_pid_prog=`echo $_ssh_agent_pid_prog | sed -e 's,^-,,g'`
  endif
  echo -n "% SSH Agent status: "
  if ( $ssh_agent_stat == ${USER} && $ssh_agent_pid_prog == ssh-agent ) then
    echo Running
    source $agentf
    echo Stored identities:
    ssh-add -l
  else
    echo Dead
    echo Run ssh-agent, remember to ssh-add
    rm -f $agentf
    ssh-agent -c > $agentf
    source $agentf
  endif
  unset ssh_agent_pid
  unset ssh_agent_stat
  unset ssh_agent_pid_prog
  unset _ssh_agent_pid_prog
 endif
 unset agentf

當您更新 ~/.login 後再重新登入一次可看到類似如下訊息也就是說 ~/.login 已經幫您把 ssh-agent 啟動了您只要啟動 ssh-add 把私鑰 passphrase 輸入一次後便可在各伺服器或工作站間通行無阻了。
 Run ssh-agent, remember to ssh-add
ssh-add
 Enter passphrase for /home/weithenn/.ssh/id_dsa: 輸入保護金鑰的 passphrase
 Identity added: /home/weithenn/.ssh/id_dsa (/home/weithenn/.ssh/id_dsa)






9、參考

文章標籤: