之前咱们在windows上共享文件的话,只需右击要共享的文件夹而后选择共享相关的选项设置便可。然而如何实现windows和linux的文件共享呢?这就涉及到了samba服务了,这个软件配置起来也不难,使用也很是简单。linux
安装系统的时候大多会默认安装samba,若是没有安装,在CentOS上只须要运行这个命令安装便可:ios
##同时安装了samba服务和客户端; [aming@Dasoncheng ~]$ yum install -y samba samba-client
Samba的配置文件为/etc/samba/smb.conf,经过修改这个配置文件来完成咱们的各类需求。打开这个配置文件,你会发现不少内容都用 # 或者 ; 注视掉了。先看一下未被注释掉的部分:数据库
[global] #全局参数 workgroup = SAMBA #工做组名称 security = user #安全验证的方式,总共有4种(详细下面介绍) passdb backend = tdbsam #定义用户后台的类型,共有3种 printing = cups printcap name = cups load printers = yes #设置在Samba服务启动时是否共享打印机设备 cups options = raw #打印机的选项 [homes] #共享参数 comment = Home Directories #描述信息 valid users = %S, %D%w%S browseable = No #指定共享信息是否在“网上邻居”中可见 read only = No #定义是否仅可读,与“writable”相反 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
主要有以上三个部分:[global], [homes], [printers]vim
[global] 定义全局的配置,workgroup用来定义工做组,相信若是您安装过windows的系统,你会对这个workgroup不陌生。通常状况下,须要咱们把这里的MYGROUP改为WORKGROUP(windows默认的工做组名字)。windows
除了这些参数外,还有几个参数须要你了解:centos
[homes] 该部份内容共享用户本身的家目录,也就是说,当用户登陆到samba服务器上时其实是进入到了该用户的家目录,用户登录后,共享名不是homes而是用户本身的标识符,对于单纯的文件共享的环境来讲,这部分能够注视掉。
[printers] 该部份内容设置打印机共享。浏览器
重要提醒:centos7的share模式和6.系列有区别;实战1演示安全
需求:共享一个目录,任何人均可以访问,即不用输入密码便可访问,要求只读!服务器
环境centos6.xdom
cat /etc/samba/smb.conf ##其余的如[home]、[print]等,不须要能够注释掉! [global] workgroup = MYGROUP server string = Samba Server Version %v security = user passdb backend = tdbsam load printers = yes cups options = raw [share] comment = share all path = /tmp/samba browseable = yes public = yes writable = no
环境centos7.x
[root@Dasoncheng2 ~]# cat /etc/samba/smb.conf [global] workgroup = WORKGROUP # security = share ##这里区别1 passdb backend = tdbsam share modes = yes ##这里区别2 printing = cups printcap name = cups load printers = yes cups options = raw [share] comment = Share all path = /tmp/samba browseable = yes public = yes writable = no security = share ##这里区别3
[root@Dasoncheng2 ~]# mkdir /tmp/samba [root@Dasoncheng2 ~]# chmod 777 /tmp/samba/ [root@Dasoncheng2 ~]# touch /tmp/samba/sharefiles [root@Dasoncheng2 ~]# echo "111111" > /tmp/samba/sharefiles
##测试你配置的smb.conf是否正确,用下面的命令: [root@Dasoncheng2 ~]# testparm
你应该会看到一个警告:WARNING: The security=share option is deprecated, 不过影响不大,无需管它。若是没有错误,则在你的windows机器上的浏览器中输入:
file://IP/share
file://192.168.60.12/share ##这个share是[share]的名字;
[root@Dasoncheng2 ~]# systemctl start smb
以centos7为例:
编辑配置文件:
[root@Dasoncheng2 ~]# cat /etc/samba/smb.conf [global] workgroup = WORKGROUP security = user passdb backend = tdbsam share modes = yes printing = cups printcap name = cups load printers = yes cups options = raw [share2] comment = Share all2 path = /tmp/samba2 browseable = yes public = no writable = yes
建立共享文件夹:
[root@Dasoncheng2 ~]# mkdir /tmp/samba2 [root@Dasoncheng2 ~]# chmod 777 /tmp/samba2 [root@Dasoncheng2 ~]# touch /tmp/samba2/sharefiel2 [root@Dasoncheng2 ~]# echo "2222222" > /tmp/samba2/sharefiel2
建立用户:
[root@Dasoncheng2 ~]# useradd user1 ##建立系统用户; [root@Dasoncheng2 ~]# pdbedit -a user1 ##添加user1为samba帐号 new password: retype new password: Unix username: user1 NT username: Account Flags: [U ] User SID: S-1-5-21-3131785581-1912635650-3533182432-1000 Primary Group SID: S-1-5-21-3131785581-1912635650-3533182432-513 Full Name: Home Directory: \\localhost\user1 HomeDir Drive: Logon Script: Profile Path: \\localhost\user1\profile Domain: LOCALHOST Account desc: Workstations: Munged dial: Logon time: 0 Logoff time: Wed, 06 Feb 2036 23:06:39 CST Kickoff time: Wed, 06 Feb 2036 23:06:39 CST Password last set: Sun, 11 Feb 2018 16:42:09 CST Password can change: Sun, 11 Feb 2018 16:42:09 CST Password must change: never Last bad password : 0 Bad password count : 0 Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
而后输入用户名和密码便可访问:
需求:使用linux访问samba服务器。
Samba服务在linux下一样能够访问。前提是您的linux安装了samba-client软件包。安装完后就可使用smbclient命令了。具体语法为:
smbclient //IP/共享名 -U 用户名
[aming@Dasoncheng ~]$ smbclient //192.168.60.12/share -U user2 ##这里报错,我也搞不懂 服务端的globe的security没有share,我下面就把本地的share修改成user了就行了; WARNING: Ignoring invalid value 'share' for parameter 'security' smbclient: Can't load /etc/samba/smb.conf - run testparm to debug it [aming@Dasoncheng ~]$ testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) WARNING: Ignoring invalid value 'share' for parameter 'security' Error loading services. [aming@Dasoncheng ~]$ vim /etc/samba/smb.conf [aming@Dasoncheng ~]$ sudo vim /etc/samba/smb.conf [aming@Dasoncheng ~]$ smbclient //192.168.60.12/share -U user2 Enter WORKGROUP\user2's password: Domain=[LOCALHOST] OS=[Windows 6.1] Server=[Samba 4.6.2] smb: \> ? ##登陆了以后,使用?列出全部可用命令 ? allinfo altname archive backup blocksize cancel case_sensitive cd chmod chown close del dir du echo exit get getfacl geteas hardlink help history iosize lcd link lock lowercase ls l mask md mget mkdir more mput newer notify open posix posix_encrypt posix_open posix_mkdir posix_rmdir posix_unlink posix_whoami print prompt put pwd q queue quit readlink rd recurse reget rename reput rm rmdir showacls setea setmode scopy stat symlink tar tarmode timeout translate unlock volume vuid wdel logon listconnect showconnect tcon tdis tid logoff .. ! smb: \> q
另外的方式就是经过mount挂载了,如:
[root@Dasoncheng2 ~]# mount -t cifs //10.0.4.67/myshare /mnt -o username=user1,password=123456
格式就是这样,要指定 -t cifs //IP/共享名 本地挂载点 -o后面跟username 和 password 挂载完后就能够像使用本地的目录同样使用共享的目录了,注意共享名后面不能有斜杠。