当前位置: 首页 > news >正文

linux环境下sftp配置只可上传不可下载实验2

环境server版本22.04 

客户要求为,搭建sftp协议,实现用户可以远程ssh登陆,可以文件上传,但禁止文件下载和删除。

用户配置

useradd -m -s /bin/bash nodule

目录配置

sudo mkdir -p /data/upload
# 目录归属
sudo chown nodule:nodule /data/upload# 只给:写 + 进入,不给读
sudo chmod 733 /data/upload

ssh配置如下

sudo vi /etc/ssh/sshd_config加入如下代码:
Match User noduleChrootDirectory /dataForceCommand internal-sftpAllowTcpForwarding noX11Forwarding no

重启ssh服务

sudo  systemctl restart ssh

 

增加监控脚本,用户处理上传后的文件,修改文件权限或文件夹权限,配置不可下载不可删除

#!/bin/bash
set -uWATCH_ROOT="/data/sftp/upload/incoming"
LOG="/var/log/sftp_recursive_watch.log"# ======================
# 基础检查
# ======================
if ! command -v inotifywait >/dev/null 2>&1; thenecho "inotifywait not found" >&2exit 1
fiif [ ! -d "$WATCH_ROOT" ]; thenecho "WATCH_ROOT not exist: $WATCH_ROOT" >&2exit 1
fi# ======================
# 文件保护逻辑
# ======================
protect_file() {local FILE="$1"[ -f "$FILE" ] || return 0chmod 200 "$FILE" 2>/dev/nullchattr +a "$FILE" 2>/dev/nullecho "$(date '+%F %T') protected file $FILE" >> "$LOG"
}# 扫描目录内已有文件
scan_dir_files() {local DIR="$1"find "$DIR" -type f -print0 | while IFS= read -r -d '' f; doprotect_file "$f"done
}# ======================
# 监听函数(关键)
# ======================
watch_dir() {local DIR="$1"echo "$(date '+%F %T') watching $DIR" >> "$LOG"inotifywait -m \-e create \-e moved_to \-e close_write \--format '%e %f' \"$DIR" 2>>"$LOG" | \while read -r EVENT FILE; dolocal FULL="$DIR/$FILE"# 新目录if [[ "$EVENT" =~ CREATE|MOVED_TO ]] && [ -d "$FULL" ]; thenecho "$(date '+%F %T') new dir $FULL" >> "$LOG"# 先扫一遍已有文件scan_dir_files "$FULL"# 再监听该目录(后台)watch_dir "$FULL" &continuefi# 普通文件if [ -f "$FULL" ]; thensleep 0.2protect_file "$FULL"fidone
}# ======================
# 启动流程
# ======================echo "========== watcher start $(date '+%F %T') ==========" >> "$LOG"# 启动前兜底扫描
scan_dir_files "$WATCH_ROOT"# ⚠️ 关键修复点:
# 不再使用 find | while22.04 会产生子 shell)
# 使用 process substitution,保证 watcher 不被回收
while IFS= read -r d; dowatch_dir "$d" &
done < <(find "$WATCH_ROOT" -type d)echo "$(date '+%F %T') all watchers started" >> "$LOG"# 阻塞主进程(必须)
wait

 

后台执行脚本如下:

sudo nohup bash run01.sh  > run01.log 2>&1 &

 

查找run01进程

wx@wx:~$ ls
run01.sh
wx@wx:~$ sudo nohup bash run01.sh  > run01.log 2>&1 &
[1] 2951
wx@wx:~$ ps aux | grep run01
root        2951  0.0  0.1  11496  5656 pts/0    S    10:07   0:00 sudo nohup bash run01.sh
root        2952  0.0  0.0  11496   880 pts/3    Ss+  10:07   0:00 sudo nohup bash run01.sh
root        2953  0.0  0.0   7628  3896 pts/3    S    10:07   0:00 bash run01.sh
root        2959  0.0  0.0   7368  1892 pts/3    S    10:07   0:00 bash run01.sh
root        2963  0.0  0.0   7368   304 pts/3    S    10:07   0:00 bash run01.sh
wx          2966  0.0  0.0   6476  2352 pts/0    S+   10:08   0:00 grep --color=auto run01
wx@wx:~$ 

 

sftp 客户端连接测试

wx@wx-VivoBook-ASUSLaptop-X415EA-V4200EA:~/桌面$ ls
001.txt  test3  test4
wx@wx-VivoBook-ASUSLaptop-X415EA-V4200EA:~/桌面$ sftp nodule@192.168.166.129
nodule@192.168.166.129's password: 
Connected to 192.168.166.129.
sftp> pwd
Remote working directory: /
sftp> ls
upload  
sftp> cd upload/
sftp> put 001.txt 
Uploading 001.txt to /upload/001.txt
001.txt                                       100%    4     1.3KB/s   00:00    
sftp> put -r test*
Uploading test3/ to /upload/test3
Entering test3/
001.txt                                       100%    4     4.8KB/s   00:00    
002.txt                                       100%    4     5.6KB/s   00:00    
003.txt                                       100%    4     6.6KB/s   00:00    
Uploading test4/ to /upload/test4
Entering test4/
001.txt                                       100%    4     7.3KB/s   00:00    
002.txt                                       100%    4     7.8KB/s   00:00    
003.txt                                       100%    4    27.3KB/s   00:00    
sftp> ls -l
--w-------    1 1001     1001            4 Jan 28 10:08 001.txt
drwxrwxr-x    2 1001     1001         4096 Jan 28 10:08 test3
drwxrwxr-x    2 1001     1001         4096 Jan 28 10:08 test4
sftp> get 001.txt 
Fetching /upload/001.txt to 001.txt
remote open "/upload/001.txt": Permission denied
sftp> get -r test3
Fetching /upload/test3/ to test3
Retrieving /upload/test3
remote open "/upload/test3/003.txt": Permission denied
Download of file /upload/test3/003.txt to test3/003.txt failed
remote open "/upload/test3/002.txt": Permission denied
Download of file /upload/test3/002.txt to test3/002.txt failed
remote open "/upload/test3/001.txt": Permission denied
Download of file /upload/test3/001.txt to test3/001.txt failed
sftp> get -r test4
Fetching /upload/test4/ to test4
Retrieving /upload/test4
remote open "/upload/test4/003.txt": Permission denied
Download of file /upload/test4/003.txt to test4/003.txt failed
remote open "/upload/test4/002.txt": Permission denied
Download of file /upload/test4/002.txt to test4/002.txt failed
remote open "/upload/test4/001.txt": Permission denied
Download of file /upload/test4/001.txt to test4/001.txt failed
sftp> rm 001.txt 
Removing /upload/001.txt
remote delete /upload/001.txt: Permission denied
sftp> rm  test3
Removing /upload/test3
remote delete /upload/test3: Failure
sftp> rm  test3/
001.txt  002.txt  003.txt  
sftp> rm  test3/001.txt 
Removing /upload/test3/001.txt
remote delete /upload/test3/001.txt: Permission denied
sftp> rm test4/00
001.txt  002.txt  003.txt  
sftp> rm test4/001.txt 
Removing /upload/test4/001.txt
remote delete /upload/test4/001.txt: Permission denied
sftp> 

 

结果

put上传文件,上传文件夹均成功,get 下载文件和文件夹均失败,rm删除文件夹文件,均失败。

 

上传后的文件权限检查如下:

wx@wx:~$ sudo ls -l /data/upload/
total 12
--w------- 1 nodule nodule    4 Jan 28 10:08 001.txt
drwxrwxr-x 2 nodule nodule 4096 Jan 28 10:08 test3
drwxrwxr-x 2 nodule nodule 4096 Jan 28 10:08 test4
wx@wx:~$ sudo ls -l /data/upload/test3
total 12
--w------- 1 nodule nodule 4 Jan 28 10:08 001.txt
--w------- 1 nodule nodule 4 Jan 28 10:08 002.txt
--w------- 1 nodule nodule 4 Jan 28 10:08 003.txt
wx@wx:~$ sudo ls -l /data/upload/test4
total 12
--w------- 1 nodule nodule 4 Jan 28 10:08 001.txt
--w------- 1 nodule nodule 4 Jan 28 10:08 002.txt
--w------- 1 nodule nodule 4 Jan 28 10:08 003.txt
wx@wx:~$ sudo lsattr  /data/upload/
--------------e------- /data/upload/test3
--------------e------- /data/upload/test4
-----a--------e------- /data/upload/001.txt
wx@wx:~$ sudo lsattr  /data/upload/test3
-----a--------e------- /data/upload/test3/003.txt
-----a--------e------- /data/upload/test3/002.txt
-----a--------e------- /data/upload/test3/001.txt
wx@wx:~$ sudo lsattr  /data/upload/test4
-----a--------e------- /data/upload/test4/003.txt
-----a--------e------- /data/upload/test4/002.txt
-----a--------e------- /data/upload/test4/001.txt
wx@wx:~$ 

 

http://www.jsqmd.com/news/313427/

相关文章:

  • Claude Code的完美平替:OpenCode #x2B; GitHub Copilot
  • ‌为什么你的测试总在“环境配置”上浪费时间?
  • TestOps实战:如何让测试不再“事后补救”
  • 我在菏泽小城做AI测试:一个非985人的突围实录
  • 做了 8 年前端开发,35 岁的我突然没人要了
  • ‌从被裁员到被挖角:我的AI转型日记
  • Transformer 大模型架构深度解析(4)详解 Transformer 架构
  • 2026养发加盟新趋势:如何选择靠谱品牌?
  • TDengine 脱敏函数用户手册
  • 2026国内最新汽车胶公司top5推荐!优质高端定制/防水型汽车胶厂商权威榜单发布,技术创新与品质保障助力汽车制造升级
  • 用 Java 搞 AI:自主开发 + 生态复用才是长期竞争力
  • 浮点数在内存中的存储
  • Java 企业 AI 转型:大模型多端接入与落地实践
  • Pipelined ADC流水线模数转换器的工作原理
  • 如何在Android上恢复已删除的文件
  • 大模型应用输出结果可解释性的保障方法
  • 在小米上检索照片/视频的5种方法
  • Linux Systemd 停止服务时杀死子进程的机制及 KillMode 参数详解
  • 知识图谱如何在制造业实际落地应用
  • 2026白转黑加盟项目怎么选?关键看技术与扶持体系
  • 年省500万内幕:北京企业订国际机票别再用平台了!这4家本土供应商把亏损千万的公司变成盈利巨头
  • 2026南京遗产继承律师推荐指南
  • 学霸同款2026 TOP8 AI论文写作软件:本科生毕业论文必备测评
  • 【收藏必备】Web安全完全指南:从上网到开发,小白也能掌握的防护技巧
  • 程序员转行网络安全必备:三大基础工具实战指南(附案例+简历技能点,建议收藏)
  • 科技新助力
  • ‌别再手动对比日志了!AI日志分析工具实测TOP3
  • 基于CNN的陕西降雨量气象分析-大数据深度学习算法毕设毕业设计项目Flask
  • 2026版最新黑客网站整理大全,全新整理黑客网站大全!收藏这一篇就够了
  • 2026年最适合初学者练手的10个网络安全项目,收藏这篇就够了!