SAMBA文件共享服务
先通过Yum软件仓库来安装Samba服务程序
yum install samba #安装samba服务程序
Samba服务程序中的参数以及作用
[global] | #全局参数。 | |
---|---|---|
workgroup = MYGROUP | #工作组名称 | |
server string = Samba Server Version %v | #服务器介绍信息,参数%v为显示SMB版本号 | |
log file = /var/log/samba/log.%m | #定义日志文件的存放位置与名称,参数%m为来访的主机名 | |
max log size = 50 | #定义日志文件的最大容量为50KB | |
security = user | #安全验证的方式,总共有4种 | |
#share:来访主机无需验证口令;比较方便,但安全性很差 | ||
#user:需验证来访主机提供的口令后才可以访问;提升了安全性 | ||
#server:使用独立的远程主机验证来访主机提供的口令(集中管理账户) | ||
#domain:使用域控制器进行身份验证 | ||
passdb backend = tdbsam | #定义用户后台的类型,共有3种 | |
#smbpasswd:使用smbpasswd命令为系统用户设置Samba服务程序的密码 | ||
#tdbsam:创建数据库文件并使用pdbedit命令建立Samba服务程序的用户 | ||
#ldapsam:基于LDAP服务进行账户验证 | ||
load printers = yes | #设置在Samba服务启动时是否共享打印机设备 | |
cups options = raw | #打印机的选项 | |
[homes] | #共享参数 | |
comment = Home Directories | #描述信息 | |
browseable = no | #指定共享信息是否在“网上邻居”中可见 | |
writable = yes | #定义是否可以执行写入操作,与“read only”相反 | |
[printers] | #打印机共享参数 | |
comment = All Printers | ||
path = /var/spool/samba | #共享文件的实际路径(重要)。 | |
browseable = no | ||
guest ok = no | #是否所有人可见,等同于"public"参数。 | |
writable = no | ||
printable = yes |
[root@linuxprobe ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@linuxprobe ~]# cat /etc/samba/smb.conf.bak | grep -v "#" | grep -v ";" | grep -v "^$" > /etc/samba/smb.conf
[root@linuxprobe ~]# cat /etc/samba/smb.conf
配置共享资源
用于设置Samba服务程序的参数以及作用
参数 | 作用 |
---|---|
[database] | 共享名称为database |
comment = Do not arbitrarily modify the database file | 警告用户不要随意修改数据库 |
path = /home/database | 共享目录为/home/database |
public = no | 关闭“所有人可见” |
writable = yes | 允许写入操作 |
[root@linuxprobe ~]# id linuxprobe
uid=1000(linuxprobe) gid=1000(linuxprobe) groups=1000(linuxprobe)
[root@linuxprobe ~]# pdbedit -a -u linuxprobe
new password:此处输入该账户在Samba服务数据库中的密码
retype new password:再次输入密码进行确认
Unix username: linuxprobe
NT username:
Account Flags: [U ]
User SID: S-1-5-21-507407404-3243012849-3065158664-1000
Primary Group SID: S-1-5-21-507407404-3243012849-3065158664-513
Full Name: linuxprobe
Home Directory: \\localhost\linuxprobe
HomeDir Drive:
Logon Script:
Profile Path: \\localhost\linuxprobe\profile
Domain: LOCALHOST
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 10:06:39 EST
Kickoff time: Wed, 06 Feb 2036 10:06:39 EST
Password last set: Mon, 13 Mar 2017 04:22:25 EDT
Password can change: Mon, 13 Mar 2017 04:22:25 EDT
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
创建共享目录并赋予权限
[root@linuxprobe ~]# mkdir /home/database
[root@linuxprobe ~]# chown -Rf linuxprobe:linuxprobe /home/database
重启服务并关闭防火墙
[root@linuxprobe ~]# systemctl restart smb
[root@linuxprobe ~]# systemctl enable smb
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@linuxprobe ~]# iptables -F
[root@linuxprobe ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
Windows挂载共享
无论Samba共享服务是部署Windows系统上还是部署在Linux系统上,通过Windows系统进行访问时,其步骤和方法都是一样的。下面假设Samba共享服务部署在Linux系统上,并通过Windows系统来访问Samba服务。Samba共享服务器和Windows客户端的IP地址可以根据表12-4来设置。
表12-4 Samba服务器和Windows客户端使用的操作系统以及IP地址
主机名称 | 操作系统 | IP地址 |
---|---|---|
Samba共享服务器 | RHEL 7 | 192.168.10.10 |
Linux客户端 | RHEL 7 | 192.168.10.20 |
Windows客户端 | Windows 7 | 192.168.10.30 |
要在Windows系统中访问共享资源,只需在Windows的“运行”命令框中输入两个反斜杠,然后再加服务器的IP地址即可,如图12-2所示。
在Windows系统中访问共享资源
如果已经清空了Linux系统上iptables防火墙的默认策略(即执行iptables -F命令),现在就应该能看到Samba共享服务的登录界面了。先使用linuxprobe账户的系统本地密码尝试登录,结果出现了如图所示的报错信息。由此可以验证,在RHEL 7系统中,Samba服务程序使用的果然是独立的账户信息数据库。所以,即便在Linux系统中有一个linuxprobe账户,Samba服务程序使用的账户信息数据库中也有一个同名的linuxprobe账户,大家也一定要弄清楚它们各自所对应的密码。
访问Samba共享服务时,提示出错
正确输入linuxprobe账户名以及使用pdbedit命令设置的密码后,就可以登录到共享界面中了,如图12-4所示。此时,我们可以尝试执行查看、写入、更名、删除文件等操作。
成功访问Samba共享服务
由于Windows系统的缓存原因,有可能您在第二次登录时提供了正确的账户和密码,依然会报错,这时只需要重新启动一下Windows客户端就没问题了
Linux挂载共享
[root@linuxprobe ~]# yum install cifs-utils
Loaded plugins: langpacks, product-id, subscription-manager
rhel | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package cifs-utils.x86_64 0:6.2-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
cifs-utils x86_64 6.2-6.el7 rhel 83 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 83 k
Installed size: 174 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : cifs-utils-6.2-6.el7.x86_64 1/1
Verifying : cifs-utils-6.2-6.el7.x86_64 1/1
Installed:
cifs-utils.x86_64 0:6.2-6.el7
Complete!
在Linux客户端,按照Samba服务的用户名、密码、共享域的顺序将相关信息写入到一个认证文件中。为了保证不被其他人随意看到,最后把这个认证文件的权限修改为仅root管理员才能够读写:
[root@linuxprobe ~]# vim auth.smb
username=linuxprobe
password=redhat
domain=MYGROUP
[root@linuxprobe ~]# chmod -Rf 600 auth.smb
现在,在Linux客户端上创建一个用于挂载Samba服务共享资源的目录,并把挂载信息写入到/etc/fstab文件中,以确保共享挂载信息在服务器重启后依然生效:
[root@linuxprobe ~]# mkdir /database
[root@linuxprobe ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# 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/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
//192.168.10.10/database /database cifs credentials=/root/auth.smb 0 0
[root@linuxprobe ~]# mount -a
Linux客户端成功地挂载了Samba服务的共享资源。进入到挂载目录/database后就可以看到Windows系统访问Samba服务程序时留下来的文件了(即文件Memo.txt)。当然,我们也可以对该文件进行读写操作并保存。
[root@linuxprobe ~]# cat /database/Memo.txt
i can edit it .
NFS网络文件系统(实现两台Linux系统之间共享)
iptables -F #清空防火墙
mkdir /nfsshare #创建目录
chmod -Rf 777 /nsshare #赋予权限
vim /etc/exports #NFS配置文件
加入以下内容:
/nsshare 192.168.10.*(rw,sync,root_squash)
systemctl restart rpcbind
systemctl enable rpcbind
#重启并加入启动项
systemctl start nfs-server
systemctl enable nfs-server
[root@linuxprobe ~]# showmount -e 192.168.10.10
Export list for 192.168.10.10:
/nfsfile 192.168.10.*
然后在NFS客户端创建一个挂载目录。使用mount命令并结合-t参数,指定要挂载的文件系统的类型,并在命令后面写上服务器的IP地址、服务器上的共享目录以及要挂载到本地系统(即客户端)的目录。
[root@linuxprobe ~]# mkdir /nfsfile
[root@linuxprobe ~]# mount -t nfs 192.168.10.10:/nfsshare /nfsfile
挂载成功后就应该能够顺利地看到在执行前面的操作时写入的文件内容了。如果希望NFS文件共享服务能一直有效,则需要将其写入到fstab文件中:
[root@linuxprobe ~]# cat /nfsfile/readme
welcome to linuxprobe.com
[root@linuxprobe ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# 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/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
192.168.10.10:/nfsshare /nfsfile nfs defaults 0 0
AutoFS自动挂载服务
autofs自动挂载服务可以帮我们解决这一问题。与mount命令不同,autofs服务程序是一种Linux系统守护进程,当检测到用户试图访问一个尚未挂载的文件系统时,将自动挂载该文件系统。换句话说,我们将挂载信息填入/etc/fstab文件后,系统在每次开机时都自动将其挂载,而autofs服务程序则是在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源。
[root@linuxprobe ~]# yum install autofs
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed
--> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-40.el7.x86_64
--> Running transaction check
---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
autofs x86_64 1:5.0.7-40.el7 rhel 550 k
Installing for dependencies:
hesiod x86_64 3.2.1-3.el7 rhel 30 k
Transaction Summary
================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 579 k
Installed size: 3.6 M
Is this ok [y/d/N]: y
Downloading packages:
--------------------------------------------------------------------------------
Total 9.4 MB/s | 579 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : hesiod-3.2.1-3.el7.x86_64 1/2
Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2
Verifying : hesiod-3.2.1-3.el7.x86_64 1/2
Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2
Installed:
autofs.x86_64 1:5.0.7-40.el7
Dependency Installed:
hesiod.x86_64 0:3.2.1-3.el7
Complete!
处于生产环境中的Linux服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到autofs服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,因此在autofs服务程序的主配置文件中需要按照“挂载目录 子配置文件”的格式进行填写。挂载目录是设备挂载位置的上一级目录。例如,光盘设备一般挂载到/media/cdrom目录中,那么挂载目录写成/media即可。对应的子配置文件则是对这个挂载目录内的挂载设备信息作进一步的说明。子配置文件需要用户自行定义,文件名字没有严格要求,但后缀建议以.misc结束。具体的配置参数如第7行的加粗字所示。
[root@linuxprobe ~]# vim /etc/auto.master
#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
/media /etc/iso.misc
/misc /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
# "nosuid" and "nodev" options unless the "suid" and "dev"
# options are explicitly given.
#
/net -hosts
#
# Include /etc/auto.master.d/*.autofs
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
在子配置文件中,应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式进行填写。例如,要把光盘设备挂载到/media/iso目录中,可将挂载目录写为iso,而-fstype为文件系统格式参数,iso9660为光盘设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,/dev/cdrom则是定义要挂载的设备名称。配置完成后再顺手将autofs服务程序启动并加入到系统启动项中:
[root@linuxprobe ~]# vim /etc/iso.misc
iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
[root@linuxprobe ~]# systemctl start autofs
[root@linuxprobe ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
接下来将发生一件非常有趣的事情。我们先查看当前的光盘设备挂载情况,确认光盘设备没有被挂载上,而且/media目录中根本就没有iso子目录。但是,我们却可以使用cd命令切换到这个iso子目录中,而且光盘设备会被立即自动挂载上。我们也就能顺利查看光盘内的内容了。
[root@linuxprobe ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
[root@linuxprobe ~]# cd /media
[root@linuxprobe media]# ls
[root@linuxprobe media]# cd iso
[root@linuxprobe iso]# ls -l
total 812
dr-xr-xr-x. 4 root root 2048 May 7 2017 addons
dr-xr-xr-x. 3 root root 2048 May 7 2017 EFI
-r--r--r--. 1 root root 8266 Apr 4 2017 EULA
-r--r--r--. 1 root root 18092 Mar 6 2012 GPL
dr-xr-xr-x. 3 root root 2048 May 7 2017 images
dr-xr-xr-x. 2 root root 2048 May 7 2017 isolinux
dr-xr-xr-x. 2 root root 2048 May 7 2017 LiveOS
-r--r--r--. 1 root root 108 May 7 2017 media.repo
dr-xr-xr-x. 2 root root 774144 May 7 2017 Packages
dr-xr-xr-x. 24 root root 6144 May 7 2017 release-notes
dr-xr-xr-x. 2 root root 4096 May 7 2017 repodata
-r--r--r--. 1 root root 3375 Apr 1 2017 RPM-GPG-KEY-redhat-beta
-r--r--r--. 1 root root 3211 Apr 1 2017 RPM-GPG-KEY-redhat-release
-r--r--r--. 1 root root 1568 May 7 2017 TRANS.TBL
[root@linuxprobe ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 905M 0 905M 0% /dev
tmpfs 914M 140K 914M 1% /dev/shm
tmpfs 914M 8.9M 905M 1% /run
tmpfs 914M 0 914M 0% /sys/fs/cgroup
/dev/cdrom 3.5G 3.5G 0 100% /media/iso
/dev/sda1 497M 119M 379M 24% /boot
1.要想实现Linux系统与Windows系统之间的文件共享,能否使用NFS服务?
**答:**不可以,应该使用Samba服务程序,NFS服务仅能实现Linux系统之间的文件共享。
2.用于管理Samba服务程序的独立账户信息数据库的命令是什么?
**答:**pdbedit命令用于管理Samba服务程序的账户信息数据库。
3.简述在Windows系统中使用Samba服务程序来共享资源的方法。
**答:**在开始菜单的输入框中按照\192.168.10.10的格式输入访问命令并回车执行即可。在Windows的“运行”命令框中按照“\192.168.10.10”的格式输入访问命令并按回车键即可。
4.简述在Linux系统中使用Samba服务程序来共享资源的步骤方法。
**答:**首先应创建密码认证文件以及挂载目录,然后把挂载信息写入到/etc/fstab文件中,最后执行mount -a命令挂载使用。
5.如果在Linux系统中默认没有安装NFS服务程序,则需要安装什么软件包呢?
**答:**NFS服务程序的软件包名字为nfs-utils,因此执行yum install nfs-utils命令即可。
6.在使用NFS服务共享资源时,若希望无论NFS客户端使用什么帐户来访问共享资源,都会被映射为本地匿名用户,则需要添加哪个参数。
**答:**需要添加all_squash参数,以便更好地保证服务器的安全。
7.客户端在查看到远程NFS服务器上的共享资源列表时,需要使用哪个命令?
**答:**使用showmount命令即可看到NFS服务器上的资源共享情况。
8.简述autofs服务程序的作用。
**答:**实现动态灵活的设备挂载操作,而且只有检测到用户试图访问一个尚未挂载的文件系统时,才自动挂载该文件系统。