码迷,mamicode.com
首页 > 系统相关 > 详细

Linux SMB和NFS文件共享

时间:2017-08-16 23:12:21      阅读:495      评论:0      收藏:0      [点我收藏+]

标签:linux 基础运维学习

一、smb文件共享

1.安装和为smb添加用户

1.1安装smb软件

yum install samba samba-common samba-client-y
systemctl start smb nmb
systemctl enable smb nmb

1.2为smb添加用户

smb用户必须时本地用户
smbpasswd -a student
New SMB password:      #输入smb当前用户密码
Retype new SMB password:  #确认密码
 
pdbedit -L         #查看smb用户信息
pdbedit -x smb用户     #删除smb用户


1.3查看smb的共享目录

查看自己的共享目录,添加目录后默认共享家目录

[root@server ~]# smbclient  -L //172.25.254.231 -U student
Enter student‘s password:
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
 
         Sharename       Type     Comment
         ---------       ----     -------
         IPC$            IPC  IPC Service (Samba Server Version 4.2.3)
         student         Disk   Home Directories
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
 
         Server              Comment
         ---------            -------
 
         Workgroup            Master
         ---------            -------

1.4登陆smb 的共享目录

[root@server ~]# smbclient //172.25.254.231/student  -U student
Enter student‘s password:
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
smb: \>
#此时你如果想看你的共享目录就会出现如下错误,因为的selinux 开着
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
#这时你需要将selinux的samba_enable_home_dirs打开。
#同时还需要在配置文件中设定smb用户可以访问自己的家目录
#就是/etc/samba/smb.conf中第30行 setsebool -P samba_enable_home_dirs on
[root@server ~]#setsebool  samba_enable_home_dirs on
[root@server ~]# smbclient //172.25.254.231/student  -U student
Enter student‘s password:
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
smb: \> ls
 .           D        0  Thu Jul 10 19:06:52 2014
 ..          D        0  Thu Jul 10 18:19:09 2014
 .bash_logout     H       18  Wed Jan 29 07:45:18 2014
 .bash_profile     H      193  Wed Jan 29 07:45:18 2014
 .bashrc        H      231  Wed Jan 29 07:45:18 2014
 .ssh        DH        0  Thu Jul 10 18:19:10 2014
 .config       DH        0 Thu Jul 10 19:06:53 2014
 
 10473900 blocks of size 1024. 7318236 blocksavailable

2. /etc/samba/smb.conf 的简单配置

 
[root@server ~]# vim  /etc/samba/smb.conf
 
30 setsebool   -P samba_enable_home_dirs on
#开启可以查看用户的家目录
49 setsebool  -P samba_export_all_roon      
#只读共享
51 setsebool  -P samba_export_all_rwon     
#读写共享
##在selinux中设定smb用户可以访问自己的家目录
89   workgroup = MYGROUP
#修改标签名称也就是下面的Domai的名称
#[root@server ~]# smbclient  -L //172.25.254.231 -U student
#Enter student‘s password:
#Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
96  hosts allow =172.25.254.131
#只给172.25.254.131这个主机,其他用户远程登陆时会出下如下错误:
[root@server ~]# smbclient  //172.25.254.231/student  -U student
Enter student‘s password:
protocol negotiation failed:NT_STATUS_INVALID_NETWORK_RESPONSE

 

3.共享目录的基本设定

3.1共享目录的简单设定步骤

vim /etc/samba/smb.conf
[haha]      共享名称
comment = 对共享目录的描述
path = 共享目录的绝对路径
workgroup = WESTOS
 
当共享目录为用户自建立目录时
semanage fcontext -a -t samba_share_t ‘目录名称(/.*)?‘
restorecon -RvvF 目录名称
当共享目录为系统建立目录
setsebool -P samba_export_all_ro on       #只读共享
setsebool -P samba_export_all_rw on      #读写共享

3.2共享目录的实例

[root@server ~]# vim  /etc/samba/smb.conf
#下面为添加的共享目录的代码
[feitian]
#共享目录的名称,就是你挂载时需要填写的:例如//172.25.254.231/feitian
comment = local directory  /westos
path = /westos
[root@server ~]# systemctl restart smb
#这时你在查看就会多了一个共享目录当你登陆时会发现登陆之后,出现如下错误,还是因为selinux
[root@server ~]# smbclient -L //172.25.254.231 -U student
feitian      Disk      local directory /westos
[root@server westos]# smbclient //172.25.254.231/feitian  -U student
Enter student‘s password:       #输入添加的用户student的密码
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
#修改smb共享目录的安全上下文
[root@server ~]# semanage  fcontext -a -t  samba_share_t  ‘/westos(/.*)?‘
[root@server ~]# restorecon  -RvvF /westos
restorecon reset /westos contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file1 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file2 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file3 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
#这时你登陆就会好了,别忘了/etc/samba/smb.conf中selinux需要打开。还要在主机上开启selinux对smb服务的只读或者读写权限。
[root@client ~]# mount//172.25.254.231/student   /mnt  -o username=student,password=student
[root@client ~]# df -h |grep//172.25.254.231
//172.25.254.231/student   10G 3.1G  7.0G  31% /mnt
#开机自动挂载
vim /etc/fstab
//172.25.254.231/feitian  /mnt  cifs defaults,username=studnet,password 0  0

4.samba的配置参数 

#匿名用户访问
guest ok = yes
map to guest = bad user
 
#访问控制
hosts allow =           #仅允许
hosts deny =           #仅拒绝
valid users =          #当前共享的有效用户
valid users = westos    #当前共享的有效用户为westos
valid users = @westos    #当前共享的有效用户为westos组
valid users = +westos     #当前共享的有效用户为westos组
 
#读写控制
所有用户均可写
chmod o+w /mnt
setsebool -P samba_export_all_rw on
vim /etc/samba/smb.conf
writable = yes
 
#设定指定用户可写
write list = student     #可写用户
write list = +student    #可写用户组
write list = @student      #也是可写用户组
admin users = westos     #共享的超级用户指定

5.smb多用户共享

在客户端做如下操作

 [root@client ~]# vim /root/haha
username=student
password=student
[root@client ~]# chmod 600 /root/haha
[root@client ~]# yum install cifs-utils -y
[root@client ~]# cifscreds clear  -u student 172.25.254.231
Password:        ##smb用户student的密码
mount -o credentials=/root/haha,multiuser,sec=ntlmssp //172.25.254.231/haha  /mnt/
#credentials=/root/haha    指定挂载时所用到的用户文件
#multiuser           支持多用户认证
#sec=ntlmssp          认证方式为标准smb认证方式  
[root@client ~]# df -h|grep 172.25.254.231
//172.25.254.231/feitian   10G 3.1G  7.0G  31% /mnt

#共享用户
cifscreds add -u westos  172.25.254.100
Password:         ##smb用户westos的密码
ls /mnt

二 、NFS 文件共享

1、NFS介绍

   NFS是Network File System 的缩写,主要功能是通过网络让不同的机器彼此之间共享文件或目录,可以通过挂载(mount)的方式将NFS服务器端共享的数据文件目录挂载到NFS客户端本地中。NFS协议有多个版本:Linux支持版本4、版本3和版本2,而大多数系统管理员熟悉的是NFSv3。默认情况下,该协议并不安全,但是更新的版本(如NFSv4)提供了更安全的身份验证支持,甚至可以通过kerberos进行加密

2、简单的NFS不同主机之间的共享

  在server上的简但配置

 [root@nfs-clinent ~]# vim /etc/exports
 /westso/lala  172.25.254.231(rw,sync)
 #共享serve /westos/lala这个目录,客户端有读和写还有在共享目录的建立删除会直接写入主机的内存中
 #exportfs -rv 直接刷新,不用重启NFS服务

  在client的简但配置

[root@nfs-clinent ~]# showmount -e 172.25.254.231
Export list for 172.25.254.231:
/westos/lala 172.25.254.131
[root@nfs-clinent ~]# mount -t nfs 172.25.254.231:/westos /mnt
[root@nfs-clinent ~]# df -h|gerp 172.25.254.231
172.25.254.231:/westos/lala     9.2G  2.5G  6.3G  29% /mnt

  设置开机自动挂载,这里就不在说了,已经说太多变了

3、默认目录自动挂载

3.1 安装软件

   客户端安装软件   

[root@nfs-clinent ~]# yum install autofs -y
[root@nfs-clinent  ~]# systemctl restart autifs.server
[root@nfs-clinent  ~]# cd /net
[root@nfs-clinent  ~]# cd 172.25.254.231
[root@nfs-clinent  ~]# ls
 westos
[root@nfs-clinent  ~]# cd /westos/lala 
#这样就可以看到他的共享目录下面的内容,而且是自动挂载,退出目录后默认5分钟自动卸载。他可以修改
#在/etc/autofs.conf中可以修改。

4、 指定目录的自动挂载

  在客户端修改配置文件

[root@nfs-clinent ~]# vim /etc/auto.master
#添加一行,/nfs是指共享目录的第一层目录,/etc/auto.nfs他是master的子文件
/westos  /etc/auto.nfs
[root@nfs-clinent ~]#vim /etc/auto.nfs
#共享目录的第二层目录
*   172.25.254.231:/&
#这个意思是前面的第二层目录和第一层目录是一致的,如果你挂载了lala就表示你 自动挂载的目录为/westos/lala


本文出自 “13122323” 博客,请务必保留此出处http://13132323.blog.51cto.com/13122323/1956882

Linux SMB和NFS文件共享

标签:linux 基础运维学习

原文地址:http://13132323.blog.51cto.com/13122323/1956882

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!