您的位置:首页 > 运维架构 > Linux

【linux学习笔记】samba在RHEL7上配置

2015-10-13 17:43 525 查看
samba全称为Server Message Block,信息服务块,主要是用来实现linux与windows文件共享的.
一、基本配置选项
主要的配置文件为/etc/samba/smb.conf,配置文件分为SELINUX,全局配置,日志配置,标准服务选项,域控选项,共享定义选项,主要修改的区域为全局选项和共享定义选项,全局配置将会针对所有的共享定义生效
workgroup = WORKGROUP   定义所在的工作组
server string = Samba Server Version %v  定义提示信息,这里%v表示samba版本号
日常管理中可以对server string选项内容进行修改,可以隐藏自己所在的版本
hosts allow 定义允许访问共享的目标主机,可以有如下几种格式定义
.example.com    //定义所有example.com结尾的主机均可访问
172.16.0.0/16
172.16.0.0/255.255.0.0
172.16.          //以上三种均定义为同样的意思,定义一个网段,多个网段可用空格隔开
hosts deny 定义拒绝访问共享的主机,如果只设定了deny,除deny后的网段外,其它网段均可以正常访问,比如:
hosts deny 192.168.1.0/24
hosts allow 192.168.1.1
#表示不允许192.168.1.0/24网段访问,但允许192.168.1.1这台主机和172.16.0.0访问,默认为允许,即其它网段默认为允许
日志信息配置
log file = /var/log/samba/log.%m  //默认是以log.机器名定义日志格式
max log size = 50   //日志轮巡,默认单位为KB
共享文件格式
[ public ]  //共享的名字
comment = Description      //描述这个共享
path = /path/to/share     //共享的文件夹
writable = yes|no  //默认不可写,要开启写入功能在这里选yes
valid users = user1 @group  //有效访问用户,可用@支持用户组访问,默认为支持所有用户访问
browseable = yes | no  //定义是否允许浏览,如果为yes则所有用户均可访问此目录
public = yes | no      //定义是否允许匿名访问,默认为no,此项比较危险,不建议开启
readonly = no // 相当于writable = yes
二、samba基本配置
主要使用到的包有samba,cifs-utils,先装需要的包,加入开机启动,开放防火墙,现有两台终端一台10.1.17.221(服务端),另外一台终端10.1.17.249(客户端)

#yum install -y samba cifs-utils
#systemctl enable smb
#systemctl enable nmb
#systemctl start smb
#systemctl start nmb
#firewall-cmd --permanent --add-service=samba
#firewall-cmd --reload
修改samba主配置文件vim /etc/samba/smb.conf,新建一个 rhce的共享,可以访问的用户为tom,bob,只允许tom写入,只允许10.1.0.0/16位网段访问

[rhce]
comment = share for it
path = /mnt/smb
valid users = tom bob
write list = tom
hosts allow = 10.1.0.0/16
使用testparm命令可以检查smb.conf配置文件语法
testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
params.c:Parameter() - Ignoring badly formed line in configuration file: setsebool -P samba_enable_home_dirs on
Processing section "[homes]"
Processing section "[printers]"
Processing section "[rhce]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions

[global]
netbios name = SERVER.REDHAT.SUN
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
load printers = No
idmap config * : backend = tdb
cups options = raw

[homes]
comment = Home Directories
read only = No
browseable = No

[printers]
comment = All Printers
path = /var/spool/samba
printable = Yes
print ok = Yes
browseable = No

[rhce]
comment = share for it
path = /mnt/smb
valid users = tom, bob
write list = tom
hosts allow = 10.1.0.0/16
如果语法检查没问题会提示OK,使用pdbedit -L显示 samba用户,-x删除用户

[root@server ~]# pdbedit -L
tom:1022:
bob:1023:
创建共享目录,修改SELINUXcontext值
#mkdir /mnt/smb
#cp /etc/fstab /mnt/smb
#ll -dZ /mnt/smb
drwxr-xr-x. root root unconfined_u:object_r:mnt_t:s0   /mnt/smb
#chcon -R -t samba_share_t /mnt/smb
#ll -dZ /mnt/smb
drwxr-xr-x. root root unconfined_u:object_r:samba_share_t:s0 /mnt/smb
#systemctl restart smb nmb
换一台终端检查rhce共享是否可以正常显示
#yum install -y samba-client
#smbclient -L 10.1.17.221
Enter root's password:
Anonymous login successful
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 4.1.12]

Sharename       Type      Comment
---------       ----      -------
rhce            Disk      share for it
IPC$            IPC       IPC Service (Samba Server Version 4.1.12)
Anonymous login successful
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 4.1.12]

Server               Comment
---------            -------
SERVER.REDHAT.S      Samba Server Version 4.1.12
WH-PRINT

Workgroup            Master
---------            -------
WORKGROUP
在服务器端添加用户,添加用户前,本地用户必须存在才能正常添加
smbpasswd -a 添加用户
-x 删除用户
-d 表示disable禁用用户
-e 表示enable启用用户
添加tom,bob用户,给tom,bob设计samba密码,添加it组,将tom,bob加入到it的附加组,修改/mnt/smb目录属组和权限

#useradd -s /sbin/nologin tom
#useradd -s /sbin/nologin bob
#smbpasswd -a tom
New SMB password:redhat
Retype new SMB password:redhat
Added user tom.
#smbpasswd -a bob
#groupadd it
#gpasswd -a tom it      //将tom加入到it附加组
#gpasswd -a bob it
#chown :it /mnt/smb
#chmod 2775 /mnt/smb
接下来查看/mnt/smb的权限属性和用户属性
#ll -d /mnt/smb
drwxrwsr-x. 2 root it 18 Oct 14 00:55 /mnt/smb
#id bob
uid=1023(bob) gid=1023(bob) groups=1023(bob),40002(it)
#id tom
uid=1022(tom) gid=1022(tom) groups=1022(tom),40002(it)
#grep -e "^it" /etc/group
it:x:40002:tom,bob
使用windows客户端访问\\10.1.17.221,测试bob,可以正常访问,在RHCE目录下无权建立文件夹,使用net usr /del *删除windows访问缓存,用tom用户登陆,可以正常RHCE上新建。
在客户端挂载测试
#mount -t cifs -o username=tom //10.1.17.221/rhce /mnt
Password for tom@//10.1.17.221/rhce:  ******
# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   11G  4.9G  5.4G  48% /
devtmpfs               740M     0  740M   0% /dev
tmpfs                  749M  140K  749M   1% /dev/shm
tmpfs                  749M   41M  708M   6% /run
tmpfs                  749M     0  749M   0% /sys/fs/cgroup
/dev/sda1              197M  130M   68M  66% /boot
/dev/mapper/rhel-home  4.9G  242M  4.7G   5% /home
//10.1.17.221/rhce     9.8G  631M  9.2G   7% /mnt
# cd /mnt
# ll
total 4
-rw-r--r--. 1 root root 693 Oct 14  2015 fstab
#echo "tom is here " >> tom.txt
# ll
total 8
-rw-r--r--. 1 root root  693 Oct 14  2015 fstab
-rw-r--r--. 1 1022 40002  13 Oct 14  2015 tom.txt
实现自动挂载功能,编辑/etc/fstab,加入一行
//10.1.17.221/rhce      /mnt        cifs    defaults,username=tom,passwd=redhat 0 0
三、使用用户名和密码访问共享
查看一下fstab权限为644,所有人均可读,所以这样没有安全性可言,可以新建一个密码文件,将用户名和密码放置在此密码文件中
#echo "username=tom" >/etc/tom.txt
# echo "passwd=redhat" >> /etc/tom.txt
# chmod 400 /etc/tom.txt
修改/etc/fstab
//10.1.17.221/rhce    /mnt      cifs    defaults,credentials=/etc/tom.txt 0 0
四、使用multiuser实现多用户加密访问
目前只有一个固定的用户,无法针对不同的用户实现不同的密码访问,而且密码已经固定,不方便进行管理,RHEL7新增了一个功能multiuser,可以多用户访问,编辑/etc/fstab,添加多用户选项,加密方式为ntlmssp,可以man mount.cifs,最后的_netdev选项是网络服务启动后再挂载,防止网络未启动时,一直卡顿,不能正常挂载的现象

//10.1.17.221/rhce      /mnt    cifs    defaults,credentials=/etc/tom.txt,multiuser,sec=ntlmssp,_netdev 0 0
将终端/mnt目录临时修改为777权限
#chmod 777 /mnt
#mount -a
# su - student    //切换为普通用户
#ll /mnt
ls: cannot access /mnt: Permission denied
cifscreds add -u bob 10.1.17.221
Password: redhat
#cifscreds -h           //cifscreds的用法
cifscreds: invalid option -- 'h'
Usage:
cifscreds add [-u username] [-d] <host|domain>
cifscreds clear [-u username] [-d] <host|domain>
cifscreds clearall
cifscreds update [-u username] [-d] <host|domain>
#cifscreds add -u bob 10.1.17.221   //添加bob的凭证
Password: redhat
#ll /mnt     //可以正常ls
total 8
-rw-r--r--. 1 root root  693 Oct 14  2015 fstab
-rw-r--r--. 1 1022 40002   0 Oct 13 18:30 newtom
-rw-r--r--. 1 1022 40002   0 Oct 13 18:33 newwww
-rw-r--r--. 1 1022 40002  13 Oct 14  2015 tom.txt
#touch bob
touch: cannot touch ?.ob?. Permission denied
//这里有一个命令为清除凭证,但已经添加过的用户,比如加过bob,使用cifscreds clearall,系统已经
//提示清掉凭证了,但添加tom后,依然还是使用的bob以前的凭证,通过卸载/mnt目录可以实现清掉凭证
//这里切换另外一个用户user演示tom用户
#su - user
Last login: Thu Aug 20 15:13:02 CST 2015 on pts/1
Last failed login: Tue Oct 13 18:40:42 CST 2015 on pts/1
There was 1 failed login attempt since the last successful login.
[user@sunny mnt]$ cifscreds add -u tom 10.1.17.221
Password:
[user@sunny mnt]$ cd /mnt
[user@sunny mnt]$ touch 111
说明user用户已经获取到了tom的凭证,可以正常写入,至此samba实验到此结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: