Thứ Năm, 13 tháng 2, 2014

// // Leave a Comment

Cài đặt và cấu hình NFS trên CentOS 6.4 (server) - 5.4 (client)


NFS là một giao thức khá cũ nhưng do nó đơn giản và được hỗ trợ tốt của Linux nên vẫn được sử dụng. Nó thường được dùng làm ổ đĩa mạng và để cài hệ điều hành Linux qua mạng với thêm sự hỗ trợ từ PXE.

NFS là giao thức chia sẻ file phổ biến trên UNIX. Nó cho phép mount một thư mục trên một máy tính từ xa vào một thư mục cục bộ. Ví dụ trong các hệ thống cluster, thư mục /home thường được đặt trên một NFS server và tất cả các node mount thư mục này về /home cục bộ. Điều đó cho phép chúng ta có một cái nhìn nhất quán về thư mục chủ, mọi người dùng khi log-in vào bất cứ node nào cũng truy cập được vào thư mục chình của mình.

Cấu hình trên NFS Server:

-  Ngoài ra NFS đòi hỏi phải cài đặt thêm 1 Deamon quan trọng dùng để quản lý các kết nối đó là portmap. Mặc định portmap đã đc cài đặt sẵn giống như NFS, portmap hoạt động trên TCP port 111 - gói rpcbind.

yum install -y nfs-utils rpcbind

Kiểm tra portmap:
[root@linux ~]# rpm -qa|grep rpc
libtirpc-0.2.1-6.el6_4.i686
rpcbind-0.2.0-11.el6.i686
 
Khởi động NFS và cho phép khởi động kèm server:
[root@linux ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@linux ~]# chkconfig nfs on

Khởi động portmap và cho phép khởi động kèm server:
[root@linux ~]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@linux ~]# chkconfig rpcbind on

- Kiểm tra portmap deamon:

[root@linux ~]# netstat -an|grep 111
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 :::111                      :::*                        LISTEN
udp        0      0 0.0.0.0:41114               0.0.0.0:*                      
udp        0      0 0.0.0.0:111                 0.0.0.0:*                      
udp        0      0 :::111                      :::*    

Cấu hình NFS: file dùng để cấu hình NFS là /etc/exports

[root@linux ~]# mkdir /chiase #Tạo thư mục
[root@linux ~]# touch /chiase/thehai.txt #Tạo 1 file bất kỳ trong này
[root@linux ~]# chmod 766 -R /chiase/ #Phân quyền cho thư mục và các file bên trong


- Chỉnh sửa file cấu hình với nội dung:
[root@linux ~]# cat /etc/exports
/chiase 192.168.1.0/24(rw,sync,no_root_squash)
/thư mục chia sẻ Dãy IP có thể truy cập Quyền truy cập

Một số quyền truy cập thông dụng như:
secure : port từ client requests phải nhỏ hơn 1024
rw : Read - write
noaccess : Denied access
root_squash : Ngăn remote root user
no_root_squash : cho quép.....

Khởi động lại NFS và portmap(rpcbind):
[root@linux ~]# /etc/init.d/nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down RPC idmapd:                                  [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

Lưu ý: Sau khi thay đổi file /etc/exports phải khởi động lại deamon NFS hoặc dùng các lệnh sau:

exportfs -a : Reload lại cấu hình NFS
exportfs -r : Reload lại các thay đổi trong cấu hình của NFS
exportfs -ra : cũng đc :D

Kiêm tra lại:
[root@linux ~]# exportfs
/chiase         192.168.1.0/24

Do NFS cần 1 số port nên ta cần mở các port này trên firewall hoặc tắt firewall đi
[root@linux ~]# /etc/init.d/iptables stop
Chú ý quan trọng:
Nếu ko dùng rpcbind đc thì ta cài portmap 4:
[root@linux ~]# rpm -ivh portmap-4.0-65.2.2.1.i386.rpm
warning: portmap-4.0-65.2.2.1.i386.rpm: Header V3 DSA/SHA1 Signature, key ID 652e84dc: NOKEY
Preparing...                ########################################### [100%]
   1:portmap                ########################################### [100%]
[root@linux ~]# /etc/init.d/portmap start
Starting portmap:                                          [  OK  ]

Cấu hình trên NFS client - 5.4:
1. Cấu hình truy cập bằng tay:
Tạo thư mục:
[root@cent5 ~]# mkdir /nhanfile
Tiến hành mount:
[root@cent5 ~]# mount -t nfs 192.168.1.104:/chiase /nhanfile
[root@cent5 ~]# ll /nhanfile/
total 0
-rwxrw-rw- 1 root root 0 Feb 13  2014 thehai.txt

2. Cấu hình truy cập trong file /etc/fstab:
[root@cent5 ~]# cat /etc/fstab
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
LABEL=SWAP-sda3         swap                    swap    defaults        0 0
LABEL=SWAP-sda2         swap                    swap    defaults        0 0
192.168.1.104:/chiase   /nhanfile/              nfs     soft,nfsvers=2  0 0


Allow/Deny một kết nối đến NFS server (Chưa test đc):
Sử dụng file /etc/hosts.allow/etc/hosts.deny để permit kết nối đến NFS server
- Cấm tất cả các clients truy cập vào NFS server:
[root@CentOS1 ~]# vim /etc/hosts.deny
Thêm vào dòng: portmap : ALL
- Cho phép server có ip 192.168.100.52 truy cập đến NFS server:
[root@CentOS1 ~]# vim /etc/hosts.allow
Thêm vào dòng:  portmap : 192.168.100.52
Ta tiến hành Test thử bằng kết nối chia sẻ File từ địa chỉ IP 192.168.100.52 tới NFS Server

Kiểm tra hoạt động của NFS Server:
[root@linux ~]# nfsstat
Server rpc stats:
calls      badcalls   badclnt    badauth    xdrcall
212        0          0          0          0

Server nfs v2:
null         getattr      setattr      root         lookup       readlink
2        33% 2        33% 0         0% 0         0% 0         0% 0         0%
read         wrcache      write        create       remove       rename
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
link         symlink      mkdir        rmdir        readdir      fsstat
0         0% 0         0% 0         0% 0         0% 0         0% 2        33%

Server nfs v3:
null         getattr      setattr      lookup       access       readlink
16       10% 33       21% 22       14% 12        7% 15        9% 0         0%
read         write        create       mkdir        symlink      mknod
0         0% 7         4% 10        6% 0         0% 0         0% 0         0%
remove       rmdir        rename       link         readdir      readdirplus
10        6% 0         0% 0         0% 0         0% 0         0% 9         5%
fsstat       fsinfo       pathconf     commit
0         0% 16       10% 0         0% 7         4%

Server nfs v4:
null         compound
1         5% 19       95%

Server nfs v4 operations:
op0-unused   op1-unused   op2-future   access       close        commit
0         0% 0         0% 0         0% 2         4% 0         0% 0         0%
create       delegpurge   delegreturn  getattr      getfh        link
0         0% 0         0% 0         0% 18       40% 3         6% 0         0%
lock         lockt        locku        lookup       lookup_root  nverify
0         0% 0         0% 0         0% 2         4% 0         0% 0         0%
open         openattr     open_conf    open_dgrd    putfh        putpubfh
0         0% 0         0% 0         0% 0         0% 18       40% 0         0%
putrootfh    read         readdir      readlink     remove       rename
1         2% 0         0% 1         2% 0         0% 0         0% 0         0%
renew        restorefh    savefh       secinfo      setattr      setcltid
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
setcltidconf verify       write        rellockowner bc_ctl       bind_conn
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
exchange_id  create_ses   destroy_ses  free_stateid getdirdeleg  getdevinfo
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
getdevlist   layoutcommit layoutget    layoutreturn secinfononam sequence
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
set_ssv      test_stateid want_deleg   destroy_clid reclaim_comp
0         0% 0         0% 0         0% 0         0% 0         0%

Client rpc stats:
calls      retrans    authrefrsh
20         0          20

Client nfs v4:
null         read         write        commit       open         open_conf
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
open_noat    open_dgrd    close        setattr      fsinfo       renew
0         0% 0         0% 0         0% 0         0% 3        15% 0         0%
setclntid    confirm      lock         lockt        locku        access
0         0% 0         0% 0         0% 0         0% 0         0% 2        10%
getattr      lookup       lookup_root  remove       rename       link
3        15% 2        10% 1         5% 0         0% 0         0% 0         0%
symlink      create       pathconf     statfs       readlink     readdir
0         0% 0         0% 2        10% 0         0% 0         0% 1         5%
server_caps  delegreturn  getacl       setacl       fs_locations rel_lkowner
5        26% 0         0% 0         0% 0         0% 0         0% 0         0%
secinfo      exchange_id  create_ses   destroy_ses  sequence     get_lease_t
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
reclaim_comp layoutget    getdevinfo   layoutcommit layoutreturn getdevlist
0         0% 0         0% 0         0% 0         0% 0         0% 0         0%
(null)
0         0%

[root@linux ~]#

Liệt kê danh sách các portmapper đã đăng ký trên host:
[root@linux ~]# 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
    100005    1   udp  39935  mountd
    100005    1   tcp  54515  mountd
    100005    2   udp  33862  mountd
    100005    2   tcp  50801  mountd
    100005    3   udp  53343  mountd
    100005    3   tcp  35082  mountd
    100003    2   tcp   2049  nfs
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    2   tcp   2049  nfs_acl
    100227    3   tcp   2049  nfs_acl
    100003    2   udp   2049  nfs
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    2   udp   2049  nfs_acl
    100227    3   udp   2049  nfs_acl
    100021    1   udp  51214  nlockmgr
    100021    3   udp  51214  nlockmgr
    100021    4   udp  51214  nlockmgr
    100021    1   tcp  56827  nlockmgr
    100021    3   tcp  56827  nlockmgr
    100021    4   tcp  56827  nlockmgr

0 nhận xét:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.