Debian文件系统共享与权限设置指南
Samba是Debian上最常用的跨平台文件共享工具,可实现Windows与Linux系统间的文件共享。
sudo apt update && sudo apt install samba命令安装Samba服务。/etc/samba/smb.conf,在文件末尾添加共享配置(以共享/home/user/shared为例):[shared]
path = /home/user/shared
browseable = yes # 允许网络浏览共享
writable = yes # 允许写入(需结合权限设置)
guest ok = yes # 允许匿名访问(若需认证,设为no并添加valid users)
create mask = 0777 # 新建文件权限
directory mask = 0777 # 新建目录权限
sudo chmod -R 0777 /home/user/shared(生产环境建议限制为必要权限,如0775)和sudo chown nobody:nogroup /home/user/shared(将目录所有者设为nobody,避免权限冲突)。sudo smbpasswd -a username(将username替换为系统用户),设置Samba专用密码。sudo systemctl restart smbd && sudo systemctl restart nmbd使配置生效。\\Debian_IP\shared访问;Linux系统使用smbclient //Debian_IP/shared -U username命令挂载。NFS是Linux系统间高效的网络文件共享协议,适合局域网内Linux主机共享。
sudo apt update && sudo apt install nfs-kernel-server安装NFS服务。/etc/exports文件,添加共享规则(以共享/mnt/nfs给192.168.1.0/24网段为例):/mnt/nfs 192.168.1.0/24(rw,sync,no_subtree_check)
参数说明:rw(读写权限)、sync(同步写入,确保数据一致性)、no_subtree_check(禁用子树检查,提升性能)。sudo exportfs -a使配置生效。sudo systemctl restart nfs-kernel-server。nfs-common(sudo apt install nfs-common),创建挂载点(sudo mkdir -p /mnt/nfs_client),运行sudo mount Debian_IP:/mnt/nfs /mnt/nfs_client挂载共享。SSHFS通过SSH协议挂载远程文件系统,适合需要加密传输的场景。
sudo apt update && sudo apt install sshfs安装SSHFS。sudo mkdir -p /mnt/sshfs创建本地目录。sshfs user@remote_host:/remote/path /mnt/sshfs(替换user为远程用户名,remote_host为远程主机IP或域名,/remote/path为远程共享目录),输入远程用户密码即可挂载。fusermount -u /mnt/sshfs卸载。ls -l命令查看文件/目录的权限信息(如-rw-r--r-- 1 user group 1024 Jan 1 10:00 file.txt),其中:
-=普通文件、d=目录、l=符号链接);rw-=读写、r--=只读);chmod u+x file.txt(给所有者添加执行权限)、chmod g-w file.txt(移除组写权限)、chmod o=r file.txt(设置其他用户为只读)。chmod 755 file.txt(所有者7=读写执行,组和其他5=读执行)、chmod 644 file.txt(所有者6=读写,组和其他4=只读)。chown new_user file.txt:将文件所有者改为new_user;chgrp new_group file.txt:将文件所属组改为new_group;chown -R user:group /path/to/directory(修改目录及其内容的所有者/组)。chmod u+s file(如/usr/bin/passwd)。chmod g+s directory。chmod +t /tmp(/tmp目录默认开启粘滞位)。ACL允许为特定用户或组设置额外权限,突破传统权限模型的限制。
/etc/fstab文件,在挂载选项中添加acl(如UUID=xxxx / ext4 defaults,acl 0 1);sudo mount -o remount /。setfacl -m u:username:rwx /path/to/file(给username添加读写执行权限);setfacl -m g:groupname:r-x /path/to/file(给groupname添加读执行权限);getfacl /path/to/file;setfacl -x u:username /path/to/file(删除用户的ACL权限)。umask用于设置新建文件/目录的默认权限,计算方式为:默认权限 - umask值。
666(读写),目录为777(读写执行);022(文件644=666-022,目录755=777-022)、002(文件664,目录775,适合团队协作)。umask 022临时生效,或添加到~/.bashrc(用户级)或/etc/profile(系统级)永久生效。