设为首页 收藏本站
查看: 192|回复: 0

[经验分享] samba服务

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-8-11 08:32:53 | 显示全部楼层 |阅读模式
、samba服务
注:*Samba服务实现了Linux和Windows之间的文件传输,但是samba不能跨网段,一般用于局域网中,相当于Windows的网上邻居,节约内存
*Windows有缓存功能,访问第一次的时候需要输入用户名和密码,但是以后再访问的时候就不用输入密码了,清理缓存的命令是:net use * /delete查看缓存命令是net use
*对于samba服务的一个公共的目录访问只有两种情况,即所有用户都可访问但samba用户不能访问其家目录;第二种就是只有samba用户可以访问公共目录和其家目录,普通用户不可访问公共目录;不存在普通用户访问公共目录而samba用户可访问公共目录和家目录的情况
(1)Linux访问Windows的共享文件夹:
Windows服务器端(192.168.200.126):设置一个共享目录即可
Linux客户端:rhel6.3(192.168.200.122)
[iyunv@localhost 桌面]# rpm -qa | grep samba
samba-common-3.5.10-125.el6.x86_64
samba-winbind-clients-3.5.10-125.el6.x86_64
samba-client-3.5.10-125.el6.x86_64
[iyunv@localhost 桌面]# rpm -qa | grep cifs
cifs-utils-4.8.1-10.el6.x86_64
[iyunv@localhost 桌面]# smbclient -L 192.168.200.126 -U administrator  
#用服务器端的administrator用户查看服务器端的共享目录
Enter administrator's password: 【输入服务器密码】
Sharename       Type      Comment
---------       ----      -------
ADMIN$          Disk      远程管理
C$              Disk      默认共享
cxm             Disk      
IPC$            IPC       远程 IPC
访问共享目录:
法一:使用挂载到本地的方式访问
[iyunv@localhost 桌面]# mount.cifs //192.168.200.126/cxm /mnt -o username=administrator
#将服务器共享的目录挂载到本地的/mnt下
Password: 【输入服务器密码】
[iyunv@localhost 桌面]# df -h /mnt   #挂载成功
文件系统       容量  已用  可用 已用%% 挂载点
//192.168.200.126/cxm
                       20G  8.1G   12G  41% /mnt
[iyunv@localhost 桌面]# ls /mnt  #查看共享目录中的内容
100.txt  111.txt  333  zyk
[iyunv@localhost 桌面]# cp /mnt/333 /opt  #将共享目录的内容复制到本地的/opt下
[iyunv@localhost 桌面]# touch /mnt/111  #此时服务器端多了个111文件
[iyunv@localhost 桌面]# rm -rf /mnt/111   #服务器端没有111文件了
[iyunv@localhost 桌面]# umount /mnt  #卸载共享目录
[iyunv@localhost 桌面]# df -h /mnt   #卸载成功
文件系统       容量  已用  可用 已用%% 挂载点
/dev/mapper/junxi-root
                       13G   10G  1.8G  85% /
法二:以远程登录的方式访问(个人感觉不如第一种方法)
[iyunv@localhost 桌面]# smbclient //192.168.200.126/cxm -U administrator  
Enter administrator's password: 【输入服务器密码】
smb: \> ls
  .                                   D        0  Tue Feb  3 10:22:11 2015
  ..                                  D        0  Tue Feb  3 10:22:11 2015
  100.txt                             A        0  Tue Feb  3 10:11:57 2015
  111.txt                             A        0  Tue Feb  3 10:07:37 2015
  333                                 A        0  Tue Feb  3 10:22:11 2015
  zyk                                 D        0  Tue Feb  3 10:21:01 2015
smb: \> help
?              allinfo        altname        archive        blocksize      
cancel         case_sensitive cd             chmod          chown         
close          del            dir            du             echo           
exit           get            getfacl        hardlink       help           
history        iosize         lcd            link           lock           
lowercase      ls             l              mask           md            
mget           mkdir          more           mput           newer         
open           posix          posix_encrypt  posix_open     posix_mkdir   
posix_rmdir    posix_unlink   print          prompt         put            
pwd            q              queue          quit           readlink      
rd             recurse        reget          rename         reput         
rm             rmdir          showacls       setmode        stat           
symlink        tar            tarmode        translate      unlock         
volume         vuid           wdel           logon          listconnect   
showconnect    ..             !              
smb: \> get 111.txt  #将共享文件的内容下载到当前目录
getting file \111.txt of size 0 as 111.txt (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec)
smb: \> put 333   #将当前目录的文件复制到共享目录
putting file 333 as \333 (0.0 kb/s) (average 0.0 kb/s)
smb: \> get 111.txt /opt/111.txt #将共享文件的内容下载到指定目录(后面必须写文件名)
getting file \111.txt of size 0 as /opt/111.txt (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec)
smb: \> put /opt/111.txt 100.txt    #将指定目录的内容复制到共享目录
putting file /opt/111.txt as \100.txt (0.0 kb/s) (average 0.0 kb/s)
smb: \> rename 111.txt 999.txt  #给文件改名
smb: \> rm 999.txt  #删除文件
(2)Windows访问Linux的共享目录
<1>windows访问Linux的samba用户的家目录:
[iyunv@localhost 桌面]# useradd test  #建要设为samba的用户
[iyunv@localhost 桌面]# passwd test
[iyunv@localhost 桌面]# rpm -qa | grep samba  
samba-common-3.5.10-125.el6.x86_64
samba-winbind-clients-3.5.10-125.el6.x86_64
samba-client-3.5.10-125.el6.x86_64
[iyunv@localhost 桌面]# yum list | grep samba
samba.x86_64                          3.5.10-125.el6                   bendiyuan
samba-winbind.x86_64                  3.5.10-125.el6                   bendiyuan
samba-winbind-clients.x86_64            3.5.10-125.el6                   bendiyuan
[iyunv@localhost 桌面]#yum install –y  samba-winbind.x86_64  samba-winbind-clients.x86_64
                    samba.x86_64  
#前两个可以不装,那是把smb加入域,最后一个要装,为samba服务器端包
[iyunv@localhost 桌面]# pdbedit -a test  #将test用户设为samba用户
new password:
retype new password:
[iyunv@localhost 桌面]# /etc/init.d/smb restart   #重启服务
关闭 SMB 服务:                                            [失败]
启动 SMB 服务:                                            [确定]
[iyunv@localhost 桌面]# pdbedit –L  #查看系统的samba用户
test:500:
[iyunv@localhost 桌面]# grep test /etc/passwd
test:x:500:500::/home/test:/bin/bash
[iyunv@localhost 桌面]# getenforce  #查看selinux是否开启
Enforcing  (此为开启状态)
[iyunv@localhost 桌面]# getsebool -a | grep samba   #查看samba相关的bool值
samba_enable_home_dirs --> off
[iyunv@localhost 桌面]# setsebool -P samba_enable_home_dirs on  #开启samba的/home
[iyunv@localhost 桌面]# getsebool -a | grep samba
samba_enable_home_dirs --> on
[iyunv@localhost 桌面]#
注:此时Windows客户端打开运行输入:[url=]\\192.168.200.122[/url]然后回车,输入smb用户名(test)和密码(123)即可看到test用户家目录了,在里面建东西什么的,在服务器的/home/test下可看到相应结果
<2>Windows使用samba用户访问Linux的公共目录和用户家目录(需输入samba用户密码)
[iyunv@localhost 桌面]#yum  install  –y  samba*
[iyunv@localhost 桌面]# /etc/init.d/smb restart
[iyunv@localhost 桌面]# useradd test  #建要设为samba的用户
[iyunv@localhost 桌面]# passwd test
[iyunv@localhost 桌面]# pdbedit -a test  #将test用户设为samba用户
new password:
retype new password:
[iyunv@localhost 桌面]# pdbedit –L  #查看系统的samba用户
test:500:
[iyunv@localhost 桌面]# getsebool -a | grep samba   #查看samba相关的bool值
samba_enable_home_dirs --> off  #samba用户家目录的bool值
[iyunv@localhost 桌面]# setsebool -P samba_enable_home_dirs on  #开启相应bool值
[iyunv@localhost 桌面]# mkdir /public   #建立要共享的公共目录
[iyunv@localhost 桌面]# id nobody  #nobody用户不用输入密码
uid=99(nobody) gid=99(nobody) 组=99(nobody)
[iyunv@localhost 桌面]# chown nobody:nobody /public/  
#将目录所有者,组改为nobody,这样客户端就可以在目录中随便建东西了
[iyunv@localhost 桌面]# ll -d /public/
drwxr-xr-x. 2 nobody nobody 4096 2月   3 11:30 /public/
[iyunv@localhost 桌面]# vim /etc/samba/smb.conf  
101:security=user   #以samba用户访问(需输用户名和密码)
最后面:(去掉分号)  #设置公共目录信息
[public]   #客户端看到的名字(随便)
comment = Public Stuff
path = /public  #路径(绝对路径)
public = yes  #是否公共
writable = yes   #是否可写
[iyunv@localhost 桌面]# ll -Z -d /public/  #查看上下文
drwxr-xr-x. nobody nobody unconfined_u:object_r:default_t:s0 /public/
[iyunv@localhost 桌面]#man samba_selinux | more  #查看samba的公共目录上下文
samba_share_t
       - Set files with the samba_share_t type, if you want to treat the files
       as samba share data.
[iyunv@localhost 桌面]# chcon -Rt samba_share_t /public/   #修改公共目录上下文
[iyunv@localhost 桌面]# ll -d /public/
drwxr-xr-x. 2 nobody nobody 4096 2月   3 11:30 /public/
[iyunv@localhost 桌面]# chmod o+w /public/  #因为公共目录的所有者为nobody,其他用户只有读和执行权限,所以samba用户没有写权限,应给其写权限
[iyunv@localhost 桌面]# /etc/init.d/smb restart
关闭 SMB 服务:                                            [确定]
启动 SMB 服务:                                            [确定]
注:此时Windows客户端打开运行输入:[url=]\\192.168.200.122[/url]然后回车,输入smb用户名(test)和密码(123)即可看到test用户家目录和公共目录了,并且可以在任何一个目录中建东西什么的,在服务器的/home/test下和/public下可看到相应结果
<3>Windows的普通用户访问Linux的公共目录(不需用户名和密码)
[iyunv@localhost 桌面]#yum  install  –y  samba*
[iyunv@localhost 桌面]# /etc/init.d/smb restart
[iyunv@localhost 桌面]# useradd test
[iyunv@localhost 桌面]# passwd test
[iyunv@localhost 桌面]# pdbedit -a test
new password:
retype new password:
[iyunv@localhost 桌面]# pdbedit –L
test:500:
[iyunv@localhost 桌面]# getsebool -a | grep samba
samba_enable_home_dirs --> off
[iyunv@localhost 桌面]# setsebool -P samba_enable_home_dirs on
[iyunv@localhost 桌面]# mkdir /public1
[iyunv@localhost 桌面]# chown nobody:nobody /public1/  
[iyunv@localhost 桌面]# vim /etc/samba/smb.conf  
101:security=share
最后面:(添加以下内容,或将相应分号去掉)  
[public1]
comment = Public Stuff
path = /public1
public = yes
writable = yes
[iyunv@localhost 桌面]# ll -Z -d /public1/  #查看上下文
drwxr-xr-x. nobody nobody unconfined_u:object_r:default_t:s0 /public1/
[iyunv@localhost 桌面]# chcon -Rt samba_share_t /public/   #修改公共目录上下文
[iyunv@localhost 桌面]# /etc/init.d/smb restart
关闭 SMB 服务:                                            [确定]
启动 SMB 服务:                                            [确定]
注:此时Windows客户端打开运行输入:[url=]\\192.168.200.122[/url]然后回车,即可看到服务器的公共目录了,并且可以在里面建东西什么的,在服务器的/public1下可看到相应结果
<4>Windows访问Linux隐藏的公共目录
[iyunv@localhost 桌面]#yum  install  –y  samba*
[iyunv@localhost 桌面]# /etc/init.d/smb restart
[iyunv@localhost 桌面]# useradd test
[iyunv@localhost 桌面]# passwd test
[iyunv@localhost 桌面]# pdbedit -a test
new password:
retype new password:
[iyunv@localhost 桌面]# pdbedit –L
test:500:
[iyunv@localhost 桌面]# getsebool -a | grep samba
samba_enable_home_dirs --> off
[iyunv@localhost 桌面]# setsebool -P samba_enable_home_dirs on
[iyunv@localhost 桌面]# mkdir /public2
[iyunv@localhost 桌面]# chown nobody:nobody /public2/  
[iyunv@localhost 桌面]# ll -d /public2/
drwxr-xr-x. 2 nobody nobody 4096 2月   3 11:30 /public2/
[iyunv@localhost 桌面]# vim /etc/samba/smb.conf  
101:security=share
最后面:(添加以下内容或去掉相应的分号)
[public2]
comment = Public Stuff
path = /public2
public = yes
writable = yes
   browseable=no
[iyunv@localhost 桌面]# ll -Z -d /public2/  #查看上下文
drwxr-xr-x. nobody nobody unconfined_u:object_r:default_t:s0 /public2/
[iyunv@localhost 桌面]# chcon -Rt samba_share_t /public2/   #修改公共目录上下文
[iyunv@localhost 桌面]# /etc/init.d/smb restart
关闭 SMB 服务:                                            [确定]
启动 SMB 服务:                                            [确定]
注:此时Windows客户端打开运行输入:[url=]\\192.168.200.122[/url]然后回车,看不到隐藏的目录,当在地址栏中输入[url=]\\192.168.200.122\public2[/url]时就可以看见了,并且可以在里面建东西什么的,在服务器的/public2下可看到相应结果
二、selinux服务
注:*selinux是强制安全访问策略,保护服务器安全的,一般Linux系统中都会开启selinux,要想访问一个目录,必须有相应的上下文和bool值,否则不能访问或不能进行操作
    *selinux的三种模式:enforcing(记录警告且阻止可疑行为,即开启selinux)
permissive(记录安全警告,但不阻止);disable(关闭selinux)
*selinux的配置文件:/etc/selinux/config
selinux的相关命令:
[iyunv@localhost 桌面]# sealert –a  
[iyunv@localhost 桌面]# sestatus –bv   #查看当前系统bool值开关和selinux状态
[iyunv@localhost 桌面]# getenforce  #查看selinux是否开启
Enforcing
[iyunv@localhost 桌面]# getfattr -m. -d /etc/passwd   #查看相应目录的上下文
security.selinux="system_u:object_r:etc_t:s0"
上下文的查看、修改与恢复:
[iyunv@localhost 桌面]# ll -Z -d /public/   #查看目录现在的上下文
drwxr-xr-x. nobody nobody unconfined_u:object_r:samba_share_t:s0 /public/
[iyunv@localhost 桌面]# chcon -Rt samba_share_t /public2/ #修改上下文
[iyunv@localhost 桌面]# matchpathcon /public/  #查看目录原来的上下文
/public system_u:object_r:default_t:s0
[iyunv@localhost 桌面]# restorecon /public/   #恢复目录原来的上下文
[iyunv@localhost 桌面]# ll -Z -d /public/
drwxr-xr-x. nobody nobody unconfined_u:object_r:default_t:s0 /public/
bool值的查看与修改:
[iyunv@localhost 桌面]# getsebool -a | grep samba  #查看相应bool值
[iyunv@localhost 桌面]# setsebool -P samba_enable_home_dirs on  #开启相应bool值
selinux的管理工具(semanage):
[iyunv@localhost 桌面]#yum install –y policycoreutils-python   #安装semanage
[iyunv@localhost 桌面]#semanage fcontext –a –t samba_share_t /public2/ #改上下文
[iyunv@localhost 桌面]#semanage port –l | grep http  #查属于http的端口
[iyunv@localhost 桌面]#semanage port –a –t http_port_t –p tcp 81  #给http添加端口81
(ftp改变端口时不用做修改,因为ftp传输端口是随机的)
扩展:
yum install –y policycoreutils-gui setroubleshoot-server selinux-policy-doc.noarch
                                   #rhel6下selinux的安装
注:policycoreutils-gui   #管理工具
setroubleshoot-server   #selinux故障浏览器
selinux-policy-doc.noarch  #selinux帮助手册
sealer –b  #打开selinux故障浏览器
system-config-selinux  #打开selinux管理工具
cat /etc/selinux/targeted/contexts/files/file_contexts  #默认上下文存储文件
cat /etc/selinux/targeted/contexts/files/file_contexts.local   #修改后的上下文存储文件


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-97199-1-1.html 上篇帖子: rsync和inotify-tools 下篇帖子: NFS网络文件系统+autofs自动挂载
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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