Website-Suche

So richten Sie einen NFS-Server in CentOS 6.5 ein


In diesem Tutorial wollen wir sehen, wie man einen NFS-Server installiert und konfiguriert und wie man Dateien zwischen NFS-Server und Clients teilt. Obwohl ich diese Schritte in CentOS 6.5 32-Bit-Edition getestet habe, werden sie auch auf RHEL/Scientific Linux 6.x funktionieren.

Szenario

NFS-Server-Betriebssystem: CentOS 6.5 32-Bit (Minimale Serverinstallation)

NFS-Client-Betriebssystem: CentOS 6.5 32 Bit (Minimale Desktop-Installation)

NFS-Server-IP-Adresse : 192.168.1.200/24

NFS-Client-IP-Adresse : 192.168.1.29/24

1. Installiere NFS auf dem Server

[root@server ~]# yum install nfs* -y

2. NFS-Dienst starten

[root@server ~]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Stopping RPC idmapd:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
[root@server ~]# chkconfig nfs on

3. NFS auf dem Client installieren

[root@vpn client]# yum install nfs* -y

4. NFS-Dienst beim Client starten

[root@vpn client]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Stopping RPC idmapd:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
[root@vpn client]# chkconfig nfs on

5. Erstellen Sie freigegebene Verzeichnisse auf dem Server

Lassen Sie uns ein gemeinsames Verzeichnis namens ‘/home/ostechnix’ auf dem Server erstellen und den Client-Benutzern erlauben, Dateien im Verzeichnis ‘home/ostechnix’ zu lesen und zu schreiben.

[root@server ~]# mkdir /home/ostechnix
[root@server ~]# chmod 755 /home/ostechnix/

6. Gemeinsames Verzeichnis auf dem Server exportieren

Öffnen Sie die Datei /etc/exports und fügen Sie den Eintrag wie unten gezeigt hinzu

[root@server ~]# vi /etc/exports
/home/ostechnix 192.168.1.0/24(rw,sync,no_root_squash,no_all_squash)

wo,

/home/ostechnix - freigegebenes Verzeichnis

192.168.1.0/24 - IP-Adressbereich der Clients, die auf den freigegebenen Ordner zugreifen

rw - Machen Sie den freigegebenen Ordner beschreibbar

sync - Gemeinsames Verzeichnis synchronisieren, wann immer neue Dateien/Ordner erstellt werden

no_root_squash - Root-Berechtigung aktivieren (Benutzer können die Dateien im freigegebenen Verzeichnis lesen, schreiben und löschen)

no_all_squash - Benutzerrechte aktivieren

Starten Sie jetzt den NFS-Dienst neu.

[root@server ~]# /etc/init.d/nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Stopping RPC idmapd:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]       -

7. Gemeinsame Verzeichnisse beim Client einbinden

Erstellen Sie einen Einhängepunkt, um die freigegebenen Verzeichnisse des Servers einzuhängen.

Um das zu tun, erstellen Sie ein Verzeichnis namens ‘/nfs/shared’ (Sie können Ihren eigenen Einhängepunkt erstellen)

[root@vpn client]# mkdir -p /nfs/shared

Jetzt die freigegebenen Verzeichnisse vom Server wie unten gezeigt einbinden

[root@vpn client]# mount -t nfs 192.168.1.200:/home/ostechnix/ /nfs/shared/

Das wird eine Weile dauern und zeigt mir einen Verbindungszeitüberschreitungsfehler. Nun, keine Panik, die Firewall könnte möglicherweise die Clients daran hindern, Freigaben vom Server zu mounten. Stoppen Sie einfach die iptables, um das Problem zu beheben, oder Sie können die NFS-Dienstports durch iptables erlauben.

Um das zu tun, öffnen Sie die Datei /etc/sysconfig/nfs und heben Sie die Auskommentierung der Zeilen auf, die fett markiert sind.

[root@server ~]# vi /etc/sysconfig/nfs
#
# Define which protocol versions mountd 
# will advertise. The values are "no" or "yes"
# with yes being the default
#MOUNTD_NFS_V2="no"
#MOUNTD_NFS_V3="no"
#
#
# Path to remote quota server. See rquotad(8)
#RQUOTAD="/usr/sbin/rpc.rquotad"
# Port rquotad should listen on.
RQUOTAD_PORT=875
# Optinal options passed to rquotad
#RPCRQUOTADOPTS=""
#
#
# Optional arguments passed to in-kernel lockd
#LOCKDARG=
# TCP port rpc.lockd should listen on.
LOCKD_TCPPORT=32803
# UDP port rpc.lockd should listen on.
LOCKD_UDPPORT=32769
#
#
# Optional arguments passed to rpc.nfsd. See rpc.nfsd(8)
# Turn off v2 and v3 protocol support
#RPCNFSDARGS="-N 2 -N 3"
# Turn off v4 protocol support
#RPCNFSDARGS="-N 4"
# Number of nfs server processes to be started.
# The default is 8. 
#RPCNFSDCOUNT=8
# Stop the nfsd module from being pre-loaded
#NFSD_MODULE="noload"
# Set V4 grace period in seconds
#NFSD_V4_GRACE=90
#
#
#
# Optional arguments passed to rpc.mountd. See rpc.mountd(8)
#RPCMOUNTDOPTS=""
# Port rpc.mountd should listen on.
MOUNTD_PORT=892
#
#
# Optional arguments passed to rpc.statd. See rpc.statd(8)
#STATDARG=""
# Port rpc.statd should listen on.
STATD_PORT=662
# Outgoing port statd should used. The default is port
# is random
STATD_OUTGOING_PORT=2020
# Specify callout program 
#STATD_HA_CALLOUT="/usr/local/bin/foo"
#
#
# Optional arguments passed to rpc.idmapd. See rpc.idmapd(8)
#RPCIDMAPDARGS=""
#
# Set to turn on Secure NFS mounts. 
#SECURE_NFS="yes"
# Optional arguments passed to rpc.gssd. See rpc.gssd(8)
#RPCGSSDARGS=""
# Optional arguments passed to rpc.svcgssd. See rpc.svcgssd(8)
#RPCSVCGSSDARGS=""
#
# To enable RDMA support on the server by setting this to
# the port the server should listen on
#RDMA_PORT=20049

Starten Sie nun den NFS-Dienst neu

[root@server ~]# /etc/init.d/nfs restart
Shutting down NFS daemon:                                  [  OK  ]
Shutting down NFS mountd:                                  [  OK  ]
Shutting down NFS services:                                [  OK  ]
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Stopping RPC idmapd:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]

Fügen Sie die in Fettdruck angezeigten Zeilen in die Datei ‘/etc/sysconfig/iptables’ ein.

[root@server ~]# vi /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
-A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 32769 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 32803 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

Starten Sie nun den iptables-Dienst neu

[root@server ~]# service iptables restart
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

Binden Sie die Freigabe erneut vom Client ein

[root@vpn client]# mount -t nfs 192.168.1.200:/home/ostechnix/ /nfs/shared/

Schließlich ist das NFS-Share gemountet, ohne dass ein Verbindungszeitüberschreitungsfehler auftritt.

Um zu überprüfen, ob das freigegebene Verzeichnis eingebunden ist, geben Sie den Befehl mount im Client-System ein.

[root@vpn client]# mount
/dev/mapper/vg_vpn-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.1.200:/home/ostechnix/ on /nfs/shared type nfs (rw,vers=4,addr=192.168.1.200,clientaddr=192.168.1.29)

8. Testen von NFS

Erstellen Sie nun einige Dateien oder Ordner im Verzeichnis '/nfs/shared', das wir im vorherigen Schritt eingebunden haben.

[root@vpn shared]# mkdir test
[root@vpn shared]# touch file1 file2 file3

Gehe nun zum Server und wechsle in das Verzeichnis ‘/home/ostechnix’.

[root@server ~]# cd /home/ostechnix/
[root@server ostechnix]# ls
file1  file2  file3  test
[root@server ostechnix]#

Jetzt werden die Dateien und Verzeichnisse aufgelistet, die vom Client erstellt wurden. Außerdem können Sie die Dateien vom Server an den Client und umgekehrt freigeben.

9. Die Freigaben automatisch einbinden

Wenn Sie die Freigaben automatisch einbinden möchten, anstatt sie bei jedem Neustart manuell einzubinden, fügen Sie die folgenden in Fettschrift dargestellten Zeilen in die '/etc/fstab'-Datei des Client-Systems ein.

[root@vpn client]# vi /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Wed Feb 27 15:35:14 2013
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_vpn-lv_root /                       ext4    defaults        1 1
UUID=59411b1a-d116-4e52-9382-51ff6e252cfb /boot                   ext4    defaults        1 2
/dev/mapper/vg_vpn-lv_swap swap                    swap    defaults        0 0
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
192.168.1.200:/home/ostechnix/nfs/sharednfsrw,sync,hard,intr0 0

10. Überprüfen Sie die Aktien

Starten Sie Ihr Clientsystem neu und überprüfen Sie, ob das Laufwerk automatisch eingebunden wird oder nicht.

[root@vpn client]# mount
/dev/mapper/vg_vpn-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
192.168.1.200:/home/ostechnix on /nfs/shared type nfs (rw,sync,hard,intr,vers=4,addr=192.168.1.200,clientaddr=192.168.1.29)
nfsd on /proc/fs/nfsd type nfsd (rw)

Das ist es. Einen schönen Tag noch!!