samba进阶

1.samba基础

1.1 查看内核是否支持

modinfo cifs

# 如果没有安装cifs模块
apt install -y cifs-utils

1.2 安装samba客户端软件

apt install -y freeipa-client-samba
  • 在Windows中创建一个用户账号
# 管理员运行dos
net user test 123 /add
  • linux连接Windows的samba
# 查看有哪些共享目录
smbclient -L 10.0.0.1 -U test%123

# 连接共享目录
smbclient //10.0.0.1/论文share -U test%123

1.3 samba全局配置

vim /etc/samba/smb.conf
[global]
   workgroup = WORKGROUP
   netbios name = mika        # 需要重启nmb
   hosts allow = 127. 10.0.0.10  # 白名单
   security = user
   passdb backend = tdbsam
   printing = cups
   printcap name = cups
   load printers = yes
   cups options = raw
   log file = /var/log/samba/log.%I
   max log size = 1000

[homes]
   comment = Home Directories
   valid users = %S, %D%w%S
   browseable = no
   read only = no
   inherit acls = yes

[printers]
   comment = All Printers
   browseable = no
   path = /var/spool/samba
   printable = yes
   guest ok = no
   read only = yes
   create mask = 0700

[print$]
   comment = Printer Drivers
   path = /var/lib/samba/printers
   browseable = yes
   read only = yes
   guest ok = no
  • samba中的宏定义:

    • %m 客户端的主机的NetBIOS名
    • %M 客户端主机的FQDN
    • %H 当前用户家目录路径
    • %U 当前用户的用户名
    • %g 当前用户所属组
    • %h samba服务器的主机名
    • %L samba服务器的NetBIOS名
    • %l 客户端主机的IP
    • %s 可登录的用户名
  • 使用别名(netbios name段定义)访问

# 直接访问是访问不了,服务端重启nmb服务
systemctl restart nmbd

# 客户端访问
smbclient -L mika

1.4 用户管理

  • samba用户必须是Linux用户,建议使用/sbin/nologin
  • 用户数据库:/var/lib/samba/private/passdb.tdb
# 创建用户
useradd -s /sbin/nologin smb1
useradd -s /sbin/nologin smb2
useradd -s /sbin/nologin smb3

# 查看samba用户
pdbedit -L

# 添加samba账号并设置密码
smbpasswd -a smb1

# 修改samba密码
smbpasswd smb1

# 删除samba用户
smbpasswd -x storage

# 查看samba用户的详细信息
pdbedit -Lv smb1

# 查看samba连接状态
smbstatus
  • 客户端连接
# 会发现连接失败,查看日志得知因为是僵尸用户,没有家目录,手动创建即可
smbclient //mika/smb2 -U smb2%123

mkdir /home/smb2

# 成功
smbclient //mika/smb2 -U smb2%123

1.5 samba指定共享配置

  • [共享名称] 远程网络看到的共享名称
  • comment 注释信息
  • path 所共享的路径
  • public 能否被guest访问共享,默认no
  • browsable 是否允许所有用户浏览此共享,默认为yes,no隐藏
  • writable = yes 可以被所有用户读写,默认no
  • read only = no 和writable=yes等价,如与以上设置冲突,放在后面设置生效,默认只读
  • write list 用户,@组名,+组名,+组名,用 , 分割,如writeable=no,列表中用户或组可读可写,不在列表中用户只读
  • valid users 特定用户才能访问改共享,如为空,将允许所有用户,用户名之间用空格分割
mkdir /data/share1 -pv

chown 777 -R /data/share1/

vim /etc/samba/smb.conf
[share]
    path = /data/share1

systemctl restart smbd

# 客户端访问测试,可以观看到不支持匿名用户登录
smbclient //10.0.0.11/share

smbclient //10.0.0.11/share -U smb1%123
smbclient //10.0.0.11/share -U smb2%123

# 上传文件,发现权限不够
put passwd

# 下载文件测试
   # 先在/data/share1目录下创建文件
   touch /data/share1/test.txt
get test.txt 

# 修改配置文件
vim /etc/samba/smb.conf
[share]
    path = /data/share1
public = yes  # 一般不加,不安全

systemctl restart smbd

# 修改后不需要指定用户直接访问
smbclient //10.0.0.11/share

# 修改配置文件可写
vim /etc/samba/smb.conf
[share]
    path = /data/share1
    writable = yes

systemctl restart smbd

smbclient //10.0.0.11/share -U smb1%123

# 指定哪些用户可以访问
vim /etc/samba/smb.conf
[share]
    path = /data/share1
    writable = yes
    valid users = smb3 smb1
    
systemctl restart smbd

# 可以测试smb2无法访问
smbclient //10.0.0.11/share -U smb2%123

# 指定哪些组可以访问
vim /etc/samba/smb.conf
[share]
    path = /data/share1
    writable = yes
    valid users = @g1
    
systemctl restart smbd

# 创建g1组
groupadd g1

# 将smb1加入到smb1组
usermod -G g1 smb1

groupmems -l -g g1

# 可以测试仅smb1可访问
smbclient //10.0.0.11/share -U smb1%123


# 指定特定哪些组可以写,其余只读
vim /etc/samba/smb.conf
[share]
    path = /data/share1
    writable = yes
    write list = @g1
    
systemctl restart smbd

1.6 挂载访问

mount -o username=smb1,password=123 //10.0.0.11/share /mnt/

apt install -y cifs-utils

# 永久挂载配置
vim /etc/fstab
//10.0.0.11/share /mnt cifs cred=/etc/user.txt 0 0

vim /etc/user.txt
username=smb1
password=123

chmod 600 /etc/user.txt

# 挂载
mount -a
  • windows取消挂载
net use

net use \\10.0.0.11\IPC$ /del

1.7 不同用户自定义配置

  • %U 用户同名的文件
vim /etc/samba/smb.conf
[global]
'''
config file = /etc/samba/conf.d/%U
[share]
    path = /data/share1


mkdir /etc/samba/conf.d/

vim /etc/samba/conf.d/smb1
[share]
path = /data/share2

vim /etc/samba/conf.d/smb2
[share]
path = /data/share2

mkdir /data/share2
mkdir /data/share3

touch /data/share2/share2.txt
touch /data/share3/share3.txt


# 客户端测试
smbclient //10.0.0.11/share -U smb1%123

# 可以看到是/data/share2的内容
ls

# 客户端测试
smbclient //10.0.0.11/share -U smb2%123

# 可以看到是/data/share3的内容
ls
posted @ 2023-09-09 20:02  A57  阅读(72)  评论(0)    收藏  举报