获取root,安装Samba

pkg install root-repo
su

安装完成后输入su,手机如果有Magisk,就会提示是否赋予Termux root权限,这个当然选择是

如果没有Magisk,那很遗憾,Termux无法读取外部存储,也就无法读取外接的SSD中的数据了,这像下图所示:

安装Samba

pkg install samba 恕我直言,我真是没想到Termux的系统仓库里真的会有Samba,就连AI都告诉我要去手动编译源码才行。

安装完成后Samba是不会启动的,当然现在也不要启动Samba,因为根本没有配置文件。

调整Samba配置文件 整个Samba说到底就一个配置文件是核心,其他都是浮云,Termux仓库都有编译完成的Samba了,算是帮我们解决了90%的难题了。

Samba配置文件的位置

进入路径:

  • 示例配置文件位置 /data/data/com.termux/files/usr/share/doc/samba

里面有一个smb.conf.example文件,把它下载下来,这样你就不需要用到vi去改它了,因为改的东西实在太多了,就这一个配置文件我调了几小时啊!!!!不是看不见目录,就是打不开,要么就是不用用户名密码就能读取,目前你们能看到完整版都是我辛辛苦苦调出来的

  • 实际配置文件位置 /data/data/com.termux/files/usr/etc/samba

这个目录下,/data/data/com.termux/files/usr/etc目录下默认没有samba目录,需要自行创建

smb.conf文件内容 以下是smb.conf文件所有的内容,注意文件名,要把.example去掉!

# vim: filetype=samba
[global]
   ; these are very recommended to have, even if maybe superfluous
   workgroup = WORKGROUP
   unix password sync = no
   pam password change = no
   obey pam restrictions = no
   passwd program = /data/data/com.termux/files/usr/bin/true
   add user script = /data/data/com.termux/files/usr/bin/true
   add group script = /data/data/com.termux/files/usr/bin/true
   add user to group script = /data/data/com.termux/files/usr/bin/true
   add machine script = /data/data/com.termux/files/usr/bin/true
   delete user script = /data/data/com.termux/files/usr/bin/true
   delete user from group script = /data/data/com.termux/files/usr/bin/true
   delete group script = /data/data/com.termux/files/usr/bin/true
   rename user script = /data/data/com.termux/files/usr/bin/true
   name resolve order = lmhosts host bcast
   bind interfaces only = yes
   interfaces = 127.0.0.0/8 wlan0
 
   hosts allow = 192.168.0.
   server string = Samba on Termux
   security = user
   server role = standalone
   delete readonly = yes
   inherit permissions = yes
 
   ; Make smbd do less and announce nothing
   ; for automatic discovery, you may need to remove some of these
   local master = no
   preferred master = no
   lm announce = no
   multicast dns register = no
   host msdfs = no
   dns proxy = no
   allow dns updates = disabled
   dns update command = /data/data/com.termux/files/usr/bin/false
   server services = smb
   dcerpc endpoint servers = rpcecho
 
   ; if you disable NetBIOS, which enables automatic discovery, 1139 can be removed from smb ports
   ;disable netbios = yes
   ; remember: ports < 1024 cannot be bound to by non-root processes
   ; Windows can only see SMB servers on port 445, so if your phone is rooted
   ; use iptables to redirect requests for port
   ; 445 to, say, 4445 (capabilities may work instead. no clue)
   smb ports = 445 139
   max connections = 6
   max smbd processes = 8
   deadtime = 15
 
   ; disable guest access
   map to guest = Never
   restrict anonymous = 2
 
   ; basic attempt at performance tuning for my phone
   ; change as necessary for your device
   ;aio read size = 16384 
   aio write size = 0
   use sendfile = yes 
   min receivefile size = 16384
   socket options = TCP_NODELAY SO_KEEPALIVE
   oplocks = yes
   level2 oplocks = yes
   kernel oplocks = no
   strict locking = no
   max stat cache size = 1024
 
   ; turn off things that cannot be set on sdcardfs and vfat filesystems
   nt acl support = no
   unix extensions = no
   ea support = no
   store dos attributes = no
   map archive = no
   map hidden = no
   map readonly = no
   map system = no
 
   log file = /data/data/com.termux/files/usr/var/log/samba/log.%m
   log level = 0
   max log size = 50
   ;logging = syslog
   ;syslog only = yes
   ;syslog = 0
   enable core files = no
 
   ; disable printing support as much as possible
   load printers = no
   printing = bsd
   printcap name = /dev/null
   disable spoolss = yes
   show add printer wizard = no
   max print jobs = 0
   lpq command = /data/data/com.termux/files/usr/bin/false
   lprm command = /data/data/com.termux/files/usr/bin/false
   print command = /data/data/com.termux/files/usr/bin/false
 
#============================ Share Definitions ==============================
; remember to run termux-setup-storage first
[external]
   comment = microSD
   path = /mnt/media_rw/F2DB-002C
   vfs objects = aio_pthread
   aio_pthread:aio open = yes
   read only = no
   browseable = yes
   writable = yes
   guest ok = no
   fstype = exFAT
   admin users = dct
   valid users = dct
;   force group = media_rw
;   create mask = 0770
;   force create mode = 0770
 
[dct-home]
   comment = microSD
   path = /data/data/com.termux/files/home
   vfs objects = aio_pthread
   aio_pthread:aio open = yes
   read only = no
   browseable = yes
   writable = yes
   guest ok = no
   admin users = dct
   valid users = dct
;   force group = media_rw
;   create mask = 0770
;   force create mode = 0770

配置文件说明 21行:这里需要改成你的局域网的IP地址,否则无法使用

47行:Termux的默认配置文件中的端口为4445和1139,因为1000以下端口如果要使用必须使用root才行!普通用户使用1000端口会报错!然而Windows默认只能使用445端口,因此必须使用root用户才能完成本次博客中的内容!因此我这里已经改成成了445和139了,如果没有root权限则需要改回去!

102行:/mnt/media_rw/F2DB-002C是SSD自动挂载的路径,你们连上去后可能名称会有所不同,这个肯定是要改的!使用到root权限的MT管理器可以看到挂载后的文件路径

110、111、125、126行:这里需要注意了,我创建过一个叫dct的用户,你们可以改成别的,但是用户后面需要创建!

118行:这里写的是你Termux的根目录,你可以方便的去上传本地文件上去了,而不是使用Filezilla

创建Samba用户

我发现啊,这一步必须要root才行

也就是说,必须是root用户才能使用-a来添加Samba用户

su
cd /data/data/com.termux/files/usr/bin
# 这里的dct可以换成你自己的用户名
./smbpasswd -a dct

在输入两遍密码后,Samba用户就有了

启动Samba服务 这里必须用root启动,因为端口号是小于1000的

cd /data/data/com.termux/files/usr/bin
./smbd -s /data/data/com.termux/files/usr/etc/samba/smb.conf

查看Samba启动情况 ps -ef |grep smb

查看Samba日志 如果启动有问题,或者无法访问,可以找到Samba日志去找到问题原因,路径为:

/data/data/com.termux/files/usr/var/log/samba/log.smbd