CentOS7搭建nfs共享服务

需求背景

相信做Java开发的小伙伴可能都会遇到这样的需求,在生产环境多台机器需要访问一台机器上数据,这样的数据一般都是为了统一管理和存储,并且呢,这样的数据一般比较大,不容易进行迁移,因此我需要多台机器访问一台机器的上的数据的功能。

技术路线1

About Samba

Samba is the standard Windows interoperability suite of programs for Linux and Unix.

Samba is Free Software licensed under the GNU General Public License, the Samba project is a member of the Software Freedom Conservancy.

Since 1992, Samba has provided secure, stable and fast file and print services for all clients using the SMB/CIFS protocol, such as all versions of DOS and Windows, OS/2, Linux and many others.

Samba is an important component to seamlessly integrate Linux/Unix Servers and Desktops into Active Directory environments. It can function both as a domain controller or as a regular domain member.

安装Samba并配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ini复制代码 # 在线安装samba和samba-client
 yum install samba samba-client
 # 使用vim编辑smb.conf
 vi /etc/samba/smb.conf
 ​
 [global]
 workgroup = WORKGROUP
 server string = Samba Server %v
 netbios name = centos
 security = user
 map to guest = bad user
 dns proxy = no
 ​
 # Share Definition
 [Anonymous]
 path = /samba/anonymous
 browsable =yes
 writable = yes
 guest ok = yes
 read only = no
 ​

添加用户

1
bash复制代码 useradd -s /sbin/nologin/sambauser

设置密码

1
css复制代码 smbpasswd -a sambauser

检测用户是否添加成功

1
复制代码 pdbedit -L

设置samba服务开机自启

1
2
3
4
bash复制代码 systemctl enable smb.service
 systemctl enable nmb.service
 systemctl restart smb.service
 systemctl restart nmb.service

设置samba服务可以通过防火墙防御

1
2
css复制代码 firewall-cmd --permanent --zone=public --add-service=samba
 firewall-cmd --reload

测试语法是否正确

1
复制代码 testparam

开始挂载

1
bash复制代码 Mount –t cift //192.168.10.18:/share /share –o username=sambatest,password=xxxy

修改服务端口

samba服务默认使用的是445端口,按照如下方式可以进行修改:

1
2
3
4
5
ini复制代码 vim /etc/samba/smb.conf
 #在[global]内加入一行
 smb ports = 4455
 #重启samba服务
 systemctl restart smbd

查看服务状态

1
lua复制代码 systemctl status smbd
1
bash复制代码 tail /var/log/samba/log.smbd

查看服务是否运行在4455端口

1
css复制代码 lsof -i:4455

对不同版本的Windows做了抓包测试,发现WinXP只访问139端口,Win7同时访问139与445端口,Win10只访问445端口

技术路线2

About nfs

NFS 就是 Network FileSystem 的缩写,最早之前是由 Sun这家公司所发展出来的 。 它最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享文件。

服务端在线安装nfs

1
2
3
4
5
6
7
8
9
bash复制代码 # 使用yum安装nfs-utils时会把rpcbind一起安装上
 yum -y install nfs-utils
 # 编辑exports文件,添加客户端机器
 vim /etc/exports  
 #配置说明
 #/opt/nfs ,这个是本地要共享出去的目录
 # 192.168.0.0/24 ,允许访问的主机,可以是一个IP:192.168.0.4,也可以是一个IP段:192.168.227.0/24
 /opt/nfs 192.168.0.150(rw,sync,fsid=0) 192.168.0.151(rw,sync,fsid=0)  
 ​

设置nfs服务端开机自启

1
2
3
4
5
bash复制代码 systemctl enable rpcbind.service    
 systemctl enable nfs-server.service
 ​
 systemctl start rpcbind.service    
 systemctl start nfs.service

确认NFS服务器启动成功

1
css复制代码 rpcinfo -p

通过查看service列中是否有nfs服务来确认NFS是否启动

1
arduino复制代码 showmount -e 192.168.0.3 // 主机IP

在客户端安装nfs服务

1
2
3
4
5
bash复制代码 yum install -y nfs-utils
 ​
 systemctl enable rpcbind.service
 ​
 systemctl start rpcbind.service

注意:客户端不需要启动nfs服务,只需要启动rpcbind服务.

检查nfs服务器端是否有目录共享

1
复制代码 showmount -e 192.168.0.3

使用 mount 挂载A服务器端的目录/opt/nfs到客户端B的目录/opt/nfs下

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
ruby复制代码 [root@localhost ~]# mkdir /opt/nfs   
 ​
 [root@localhost ~]# mount -t nfs 192.168.0.3:/opt/nfs/ /opt/nfs/  
 ​
 mount 102.64.102.134:/mnt/data/ Z: (102.64.102.134存储共享)  
 [root@localhost ~]# df -h  
 ​
 文件系统                   容量 已用 可用 已用% 挂载点    
 ​
 /dev/mapper/centos-root     11G 1.3G 9.1G   13% /    
 ​
 devtmpfs                   911M     0 911M   0% /dev    
 ​
 tmpfs                     921M     0 921M   0% /dev/shm    
 ​
 tmpfs                     921M 8.5M 912M   1% /run    
 ​
 tmpfs                     921M     0 921M   0% /sys/fs/cgroup    
 ​
 /dev/sda1                 497M 170M 328M   35% /boot    
 ​
 tmpfs                     185M     0 185M   0% /run/user/0    
 ​
 192.168.227.3:/opt/nfs   11G 1.3G 9.1G   13% /opt/nfs
 ​
 # 挂载完成,可以正常访问本机下的/opt/nfs,如果在服务端A在共享目录/opte/nfs中
 写入文件,B、C机上可以看到,但是不能在这个目录中写入文件.

如果需要A、B、C三台机器都能够向共享目录写入文件,可按照如下操作进行:

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
bash复制代码 [root@localhost home]# id root   
 ​
 uid=0(root) gid=0(root) 组=0(root)
 ​
 2、在A服务器上再建立一个共享目录
 ​
 mkdir /opt/nfs1
 ​
 # vim /etc/exports  
 ​
 /opt/nfs 192.168.0.4(rw,sync,fsid=0) 192.168.0.5(rw,sync,fsid=0)    
 ​
 /opt/nfs1 192.168.0.0/24(rw,sync,all_squash,anonuid=0,anongid=0)
 ​
 加入第二行,anonuid=0,anongid=0即为root用户id。
 ​
 3、让修改过的配置文件生效
 ​
 exportfs –arv
 ​
 使用exportfs命令,当改变/etc/exports配置文件后,不用重启nfs服务直接用这个exportfs即可,它的常用选项为[-aruv].    
 ​
 -a :全部挂载或者卸载;      
 ​
 -r :重新挂载;      
 ​
 -u :卸载某一个目录;      
 ​
 -v :显示共享的目录;
 ​
 4、 查看新的可挂载目录及可连接的IP
 ​
 showmount -e 192.168.0.3    
 ​
 5、在B、C clinet端新挂载一个目录
 ​
 showmount -e 192.168.0.3 #查看新的挂载共享目录是否有了。
 ​
 mkdir nfs1  
 ​
 mount -t nfs 192.168.0.3:/opt/nfs1/ /opt/nfs1/    
 ​
 ll / >/opt/nfs1/ll.txt   #测试向新的共享目录中可以写入文件了。    
 ​
 (卸载挂载:umount /home/nfs1/)
 ​
 6、想在客户机B、C上实现开机挂载,则需要编辑/etc/fstab:
 ​
 vim /etc/fstab
 ​
 加入以下内容:
 ​
 192.168.0.3:/opt/nfs                 /opt/nfs   nfs   nolock   0 0  
 ​
 192.168.0.3:/opt/nfs1               /opt/nfs1 nfs   nolock   0 0
 ​
 保存后,重新挂载
 ​
 mount

\

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%