吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 377|回复: 3
收起左侧

[学习记录] 4月18日学习与练习关于文件共享的NFS和Samba

  [复制链接]
m1366684 发表于 2025-4-18 19:00
本帖最后由 m1366684 于 2025-4-18 19:06 编辑

4月18日学习与练习关于文件共享的NFS和Samba

NFS和samba在虚拟机上的安装、使用以及一些个人感悟。


一、NFS介绍与samba的介绍

NFS提供了一种再类UNIX系统上共享文件的方法。
为什么要安装NFS服务?当服务器的访问流量太大时,需要多台服务器分流,这时可以使用NFS服务进行共享。
NFS组件由NFS相关的内核模块、NFS用户空间工具和RPC相关服务组成。
samba的通信基于SMB协议,文件和打印机共享是samba服务的主要功能,smbd进程支持user mode和domain mode等身份验证和权限设置模式,可以通过加密的方式保护共享的文件和打印机。samba通过nmbd进程可以搭建NBNS(NetBIOS Name Service)服务器提供名称解析,将计算机的NetBIOS名解析为IP地址。

二、准备工作

首先需要准备两台虚拟机,在配置好yum源后安装NFS服务和samba,如果已经有了最新的NFS和samba也可以忽略,这里我使用的是两台centos7
我一般用镜像配置本地yum源,当然也可以用阿里源等,这里写一下我怎么用镜像配置本地源的,阿里源的话可以自己去找,这里不过多介绍。首先你得有一个镜像放桌面上,然后打开终端。如果自己会配置的可以忽略这一步。下面是在我在终端中需要输入的指令。

mkdir /opt/centos
mount /dev/cdrom /opt/centos
rm -rf *
vi /etc/yum.repo.d/local.repo

然后在local.repo文件中输入:

[centos7]
name=centos7
baseurl=file///ort/centos
gpgcheck=0
enabled=1

本地源就配置好了。
然后我将第一台虚拟机ip地址配置为192.168.254.128,第二台虚拟机的IP地址配置为192.168.254.129。主要两台虚拟机一定要在同一个网段。

三、NFS练习使用步骤

为了区分两台虚拟机我将第一台虚拟机的主机名改为nfs-server,第二台虚拟机的主机名改为nfs-cline。

1.实施

需要注意的点是两台虚拟机都要systemctl stop firewalld
具体步骤如下:

[root@nfs-server ~]# yum -y install nfs-utils rpcbind
[root@nfs-server ~]#mkdir /mnt/test
[root@nfs-server ~]# vi /etc/exports
[root@nfs-server ~]# cat /etc/exports
/mnt/test 192.168.254.0/24(rw,no_root_squash,no_all_squash,sync,anonuid=501,anongid=501) 
[root@nfs-server ~]# exportfs –r
[root@nfs-server ~]# systemctl start rpcbind
[root@nfs-server ~]# systemctl start nfs
[root@nfs-server ~]# showmount -e 192.168.254.128
Export list for 192.168.254.128:
/mnt/test 192.168.128.0/24
[root@nfs-server ~]# systemctl stop firewalld
[root@nfs-client ~]# setenforce 0
[root@nfs-client ~]# systemctl stop firewalld
[root@nfs-client ~]# mount -t nfs 192.168.254.128:/mnt/test /mnt/
[root@client ~]# df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root     17G  3.9G   14G   23% /
devtmpfs                   1.9G     0  1.9G    0% /dev
tmpfs                      1.9G     0  1.9G    0% /dev/shm
tmpfs                      1.9G  9.1M  1.9G    1% /run
tmpfs                      1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda1                 1014M  179M  836M   18% /boot
tmpfs                      378M  4.0K  378M    1% /run/user/42
tmpfs                      378M   28K  378M    1% /run/user/0
/dev/sr0                   4.3G  4.3G     0  100% /run/media/root/CentOS 7 x86_64
192.168.254.129:/mnt/test   17G  3.9G   14G   23% /mnt

2.验证

步骤如下:

[root@nfs-client ~]# cd /mnt/
[root@nfs-client mnt]# ll
total 0
 [root@client mnt]# touch abc.txt
[root@client mnt]# md5sum abc.txt
d41d8cd98f00b204e9800998ecf8427e  abc.txt [root@nfs-server ~]# cd /mnt/test/
[root@nfs-server test]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月  18 10:11 abc.txt
[root@nfs-server test]# md5sum abc.txt
d41d8cd98f00b204e9800998ecf8427e  abc.txt

不难发现在cline节点创建的文件和server节点的文件是一样的。


四、CIFS(samba)练习使用步骤

登录192.168.254.129虚拟机,我将主机名改为了samba,然后编辑文件/etc/samba/smb.conf

[root@samba ~] vi /etc/samba/smb.conf

文件内容如下:

# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.

[global]
        workgroup = SAMBA
        security = user

        passdb backend = tdbsam

        printing = cups
        printcap name = cups
        load printers = no
        cups options = raw;
        printcap name=/dev/null;
        printcap name=lpstat;
        printing=bsd
        disable spoolss=yes

[homes]
        comment = Home Directories
        valid users = %S, %D%w%S
        browseable = No
        read only = No
        inherit acls = Yes

[printers]
        comment = All Printers
        path = /var/tmp
        printable = Yes
        create mask = 0600
        browseable = No

[print$]
        comment = Printer Drivers
        path = /var/lib/samba/drivers
        write list = root
        create mask = 0664
        directory mask = 0775
[share]
        path=/opt/share
        browseable=yes
        public=yes
        writable=yes
[root@samba ~]mkdir /opt/share
[root@samba ~]chmod 777 /opt/share/
 [root@samba ~]mkdir /opt/cento
[root@samba ~]mount /dev/cdrom /opt/cento
mount: /dev/sr0 写保护,将以只读方式挂载
[root@samba ~]yum install -y samba
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 samba.x86_64.0.4.6.2-8.el7 将被 安装
--> 正在处理依赖关系 samba-common-tools = 4.6.2-8.el7,它被软件包 samba-4.6.2-8.el7.x86_64 需要
--> 正在检查事务
---> 软件包 samba-common-tools.x86_64.0.4.6.2-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=============================================================================================================================================================================================
 Package                                              架构                                     版本                                          源                                         大小
=============================================================================================================================================================================================
正在安装:
 samba                                                x86_64                                   4.6.2-8.el7                                   centos7                                   633 k
为依赖而安装:
 samba-common-tools                                   x86_64                                   4.6.2-8.el7                                   centos7                                   456 k

事务概要
=============================================================================================================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:1.1 M
安装大小:2.9 M
Downloading packages:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                          14 MB/s | 1.1 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : samba-common-tools-4.6.2-8.el7.x86_64                                                                                                                                    1/2 
  正在安装    : samba-4.6.2-8.el7.x86_64                                                                                                                                                 2/2 
  验证中      : samba-common-tools-4.6.2-8.el7.x86_64                                                                                                                                    1/2 
  验证中      : samba-4.6.2-8.el7.x86_64                                                                                                                                                 2/2 

已安装:
  samba.x86_64 0:4.6.2-8.el7                                                                                                                                                                 

作为依赖被安装:
  samba-common-tools.x86_64 0:4.6.2-8.el7                                                                                                                                                    

完毕!
[root@samba ~]# systemctl start smb
[root@samba ~]# systemctl start nmb
[root@samba ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      38532/smbd          
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1925/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1138/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1139/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1377/master         
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      38532/smbd          
tcp6       0      0 :::139                  :::*                    LISTEN      38532/smbd          
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1138/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1139/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1377/master         
tcp6       0      0 :::445                  :::*                    LISTEN      38532/smbd          
[root@samba ~]service smb restart
Redirecting to /bin/systemctl restart smb.service
[root@samba ~]smbpasswd -a root
New SMB password:
Retype new SMB password:
Added user root. 

这里我为了方便我使用的是root用户 密码可以自己设置。然后需要重启samba服务才能生效。

[root@samba ~] service smb restart
Redirecting to /bin/systemctl restart smb.service

然后使用PC电脑,按win+R键,输入samba服务的IP地址

在弹出的界面输入用户名和密码,然后单击左下角的确定。
登录后的界面可以看到一个root目录和一个share目录,samba会默认共享用户目录,share则是通过配置文件共享的目录。使用samba服务,可以简单理解为共享文件服务器,将需要被共享的文件放入share目录即可。
将之前移动到/media中的repo文件移动到share目录的命令如下:

[root@samba ~]# mv /media/ * /opt/share

这就是我今天练习的全部内容。如有错误,欢迎指正。

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
swz7852151 + 1 + 1 热心回复!
lcg2014 + 1 CIFS(samba)和windows网络邻居一样吗?

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xiatao 发表于 2025-4-19 07:28
请教下大佬NFS和Samba那个更优秀?
lcg2014 发表于 2025-4-19 09:05
CIFS(samba)和 windows网络共享一样吗?
linux怎么使用windows网络共享?
kjkuang 发表于 2025-4-19 09:18
android的 es文件管理器  rs文件管理器 都不支持smb2.0 速度太慢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-23 05:27

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表