海运的博客

使用avahi配置mdns/dns-sd网络发现samba/ftp

发布时间:April 4, 2022 // 分类: // No Comments

配置avahi使用mdns/dns-sd零配置发现samba/smb共享存储:

apt install avahi-daemon
cat <<EOF > /etc/avahi/services/smb.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">Samba</name>
  <service>
    <type>_smb._tcp</type>
    <host-name>smb.haiyun.me.local</host-name>
    <port>445</port>  
    <txt-record>path=/share</txt-record>
    <txt-record>u=guest</txt-record>
    <txt-record>p=pass</txt-record>
  </service>
</service-group>
EOF
echo '192.168.168.1 smb.haiyun.me.local' >> /etc/avahi/hosts 

ftp服务器:

cat <<EOF > /etc/avahi/services/ftp.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">FTP</name>
  <service>
    <type>_ftp._tcp</type>
    <host-name>ftp.haiyun.me.local</host-name>
    <port>21</port>  
    <txt-record>path=/share</txt-record>
    <txt-record>u=guest</txt-record>
    <txt-record>p=pass</txt-record>
  </service>
</service-group>
EOF
echo '192.168.168.2 ftp.haiyun.me.local' >> /etc/avahi/hosts 

注意:
1.host-name可使用FQDN标准域名,但是smb有的客户端不识别。
2.avahi默认会为hosts内目标ip添加反查域名,所以当在/etc/avahi/hosts内添加多个域名指向同一ip时会出错:

Static host name smb.haiyun.me.local: avahi_server_add_address failure: Local name collision

可以使用avahi-publish手工发布域名且不添加ip反查域名:

avahi-publish -a -R smb.haiyun.me.local 168.168.168.1

也可以修改systemd service让avahi启动时自动启动avahi-publish:

apt install avahi-utils
cat <<EOF > /usr/local/bin/avahi-host.sh
#!/bin/bash
/usr/bin/avahi-publish -a -R smb.haiyun.me.local 168.168.168.1 > /dev/null 2>&1 &
EOF
chmod +x /usr/local/bin/avahi-host.sh
sed -i '/ExecStart/aExecStartPost=\/usr\/local\/bin\/avahi-host\.sh' /lib/systemd/system/avahi-daemon.service 
systemctl daemon-reload 
systemctl restart avahi-daemon.service 
#手工修改添加ExecStartPost=/usr/local/bin/avahi-host.sh
#systemctl edit --full avahi-daemon.service 

使用dig测试mdns/dns-sd:

dig -p 5353 @224.0.0.251 ftp.haiyun.me.local
dig -p 5353 @224.0.0.251 _smb._tcp.local ptr
dns-sd -B _services._dns-sd._udp
dns-sd -B _smb._tcp
dns-sd -Z _smb._tcp
dns-sd -Q smb.haiyun.me.local
dns-sd -Q smb.haiyun.me.local aaaa

当同一ip有多个服务时也可使用基于golang dnssd的mdns/dns-sd服务:

package main

import (
        "context"
        "encoding/json"
        "fmt"
        "github.com/brutella/dnssd"
        "net"
        "os"
        "os/signal"
        "time"
)

type service struct {
        Name   string
        Type   string
        Domain string
        Host   string
        Ips    []string
        Port   int
        Text   map[string]interface{}
}

type config struct {
        Service []service
}

func main() {
        cfg_str, err := os.ReadFile("config.json")
        if err != nil {
                fmt.Println("load config file error:", err)
                os.Exit(1)
        }
        var cfg config
        err = json.Unmarshal(cfg_str, &cfg)
        if err != nil {
                fmt.Println("decode json err:", err)
                os.Exit(1)
        }

        if resp, err := dnssd.NewResponder(); err != nil {
                fmt.Println(err)
        } else {
                for _, v := range cfg.Service {
                        var ips []net.IP
                        for _, ip := range v.Ips {
                                ips = append(ips, net.ParseIP(ip))
                        }
                        text := make(map[string]string)
                        for key, value := range v.Text {
                                text[key] = fmt.Sprintf("%v", value)
                        }
                        sd_cfg := dnssd.Config{
                                Name:   v.Name,
                                Type:   v.Type,
                                Domain: v.Domain,
                                Host:   v.Host,
                                IPs:    ips,
                                Port:   v.Port,
                                Text:   text,
                        }

                        srv, err := dnssd.NewService(sd_cfg)
                        if err != nil {
                                fmt.Println(err)
                        }
                        go func() {
                                time.Sleep(1 * time.Second)
                                handle, err := resp.Add(srv)
                                if err != nil {
                                        fmt.Println(err)
                                } else {
                                        fmt.Printf("%s  Got a reply for service %s: Name now registered and active\n", time.Now().Format("2006-01-02 15:04:05"), handle.Service().ServiceInstanceName())
                                }
                        }()
                }

                ctx, cancel := context.WithCancel(context.Background())
                defer cancel()
                go func() {
                        stop := make(chan os.Signal, 1)
                        signal.Notify(stop, os.Interrupt)

                        select {
                        case <-stop:
                                cancel()
                        }
                }()

                err = resp.Respond(ctx)
                if err != nil {
                        fmt.Println(err)
                }
        }
}

配置文件保存到同目录config.json:

{
  "service":[
    {
      "name":"Samba",
      "type":"_smb._tcp",
      "domain":"local",
      "host":"smb.haiyun.me",
      "ips":[
        "10.0.0.1",
        "fdb2:808c:d0ef:0:20c:29ff:fe7c:264d"
      ],
      "port":445,
      "text":{
        "u":"user",
        "p":"pass",
        "path":"/share"
      }
    },
    {
      "name":"Ftp",
      "type":"_ftp._tcp",
      "domain":"local",
      "host":"ftp.haiyun.me",
      "ips":[
        "10.0.0.1",
        "fdb2:808c:d0ef:0:20c:29ff:fe7c:264d"
      ],
      "port":21,
      "text":{
        "u":"user",
        "p":"pass",
        "path":"/share"
      }
    }
  ]
}

更多服务类型及参数见:
http://www.dns-sd.org/ServiceTypes.html
本地多个不同网段mdns中继:
https://www.haiyun.me/archives/1439.html
参考:
https://github.com/brutella/dnssd
https://kodi.wiki/view/Avahi_Zeroconf
https://linux.die.net/man/5/avahi.service
https://github.com/lathiat/avahi/issues/40
https://pi3g.com/2019/04/10/avahi-how-to-assign-several-local-names-to-same-ip/

samba/smb文件共享显示短文件名

发布时间:March 12, 2022 // 分类: // No Comments

当samba遇到windows上不能作为文件名的字符时会将文件名转换为8.3 format,可以禁用文件名转换并将不合适的字符转换为windows可用的文件名。

#可查看ASCII码表hex值
catia:mappings = 0x22:0xa8,0x2a:0xa4,0x2f:0xf8,0x3a:0xf7,0x3c:0xab,0x3e:0xbb,0x3f:0xbf,0x5c:0xff,0x7c:0xa6
mangled names = no
vfs objects = catia

参考:
https://www.oreilly.com/openbook/samba/book/ch05_04.html
https://unix.stackexchange.com/questions/299130/samba-how-to-display-files-with-colon-in-their-names
https://www.samba.org/samba/docs/current/man-html/vfs_catia.8.html
https://lwp.interglacial.com/appf_01.htm

ubuntu挂载samba/smb共享中文乱码

发布时间:December 3, 2021 // 分类: // No Comments

安装smb/samba客户端及手工挂载:

apt-get install cifs-utils
mount -t cifs -o user=user,pass=pass,file_mode=0644,dir_mode=0755 //192.168.1.1/share /mnt/
#有的参数不一样并需要指定版本
#mount -t cifs -o username=user,password=pass,iocharset=utf8,vers=3.0 //192.168.1.1/temp /mnt/

修改fstab配置文件开机自动挂载:

cat /etc/fstab
//192.168.1.1/share /mnt               cifs    user=user,pass=pass 0       0

如果中文乱码先查看编码是否为utf8:

cat /etc/default/locale 
LANG="en_US.UTF-8"
LANGUAGE="en_US:en"
#LANG="zh_CN.UTF-8"
#LANGUAGE="zh_CN:zh"

查看是否安装nls_utf8内核模块:

ls /lib/modules/$(uname -r)/kernel/fs/nls/nls_utf8.ko

安装nls_utf8内核模块:

apt install linux-modules-extra-$(uname -r)

还是不行挂载添加指定utf8编码:

iocharset=utf8

参考:
https://askubuntu.com/questions/519796/unable-to-mount-cifs-with-iocharset-utf8

Ubuntu/Centos下Samba配置与Windows匿名/密码共享文件

发布时间:March 7, 2017 // 分类: // No Comments

安装:

yum install samba
apt install samba

Samba配置文件:

[global]
workgroup     = MYGROUP
#netbios name  = MYSERVER
server string = Samba Server
#interfaces = eth0
#hosts allow = 127. 192.168.168.

#unix charset    = utf8
#dos charset     = cp950

log file = /var/log/samba/log.%m
max log size = 50
log level = 3

#使用用户验证
security = user
#map未知用户使用smbguest访问,匿名时使用,windows10需启用匿名访问,否则提示网络错误
map to guest = Bad User
guest account = smbguest

load printers   = no
#create mask = 666  
#directory mask = 777  
#密码存储类型
passdb backend = tdbsam
server min protocol = SMB2
client max protocol = SMB3
client min protocol = SMB2
allow insecure wide links = yes

[guest]                                
comment    = guest file space
path       = /data/temp
writable   = yes   
#不可视目录               
browseable = no   
#可匿名访问           
guest ok   = yes 
#如需guest可访问但不可写,smbguest可写  
#writable   = no         
#write list = smbguest
#跟踪软链接
follow symlinks = yes
wide links = yes
        
[demo]                                
comment    = demo file space  
path       = /data/demo
writable   = yes                  
browseable = no              
guest ok   = no
#定义smbguest可访问           
valid users = smbguest
#强制新建的文件归属
#force user = root
force group = smbshare 
create mask = 0660
#新建目录包含粘滞位权限,非目录所有者用户只能删除自己的文件,目录所有者不受此限制
directory mask = 1770

新建samba验证用户,新建的用户必须是系统用户

groupadd smbshare
#这里的-g如果用-G guest访问会出现权限问题
useradd -M -g smbshare -s /sbin/nologin smbguest
pdbedit -a smbguest

新建共享目录:

mkdir -p /data/demo
chown root:smbshare /data/demo
#2 任何用户新建文件及目录属于smbshare组,1仅创建者可删除文件,就算smbshare可写用户组其它用户
chmod 2775 /data/demo
mkdir -p /data/guest
chown root:smbshare /data/guest
chmod 2775 /data/guest
分类
最新文章
最近回复
  • opnfense: 谢谢博主!!!解决问题了!!!我之前一直以为内置的odhcp6就是唯一管理ipv6的方式
  • liyk: 这个方法获取的IPv6大概20分钟之后就会失效,默认路由先消失,然后Global IPV6再消失
  • 海运: 不好意思,没有。
  • zongboa: 您好,請問一下有immortalwrt設定guest Wi-Fi的GUI教學嗎?感謝您。
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...