在挂载FTP服务器前,需确保系统已安装curlftpfs(基于FUSE的FTP挂载工具)。根据Linux发行版选择以下命令安装:
sudo apt-get update && sudo apt-get install curlftpfs
sudo yum install epel-release && sudo yum install curlftpfs # CentOS/RHEL
sudo dnf install curlftpfs # Fedora
选择一个空目录作为FTP服务器的挂载点(如~/ftp_mount),用于本地访问远程文件:
mkdir ~/ftp_mount
使用curlftpfs命令挂载FTP服务器,有两种方式:
方式一:命令行直接输入凭据(简单但不够安全,密码会暴露在命令历史中):
curlftpfs ftp://ftp.example.com ~/ftp_mount -o user=your_username:your_password
替换ftp.example.com为FTP服务器地址,your_username/your_password为登录凭据。
方式二:使用~/.netrc文件存储凭据(更安全,避免明文暴露):
① 创建或编辑~/.netrc文件:
nano ~/.netrc
② 添加以下内容(替换为实际信息):
machine ftp.example.com
login your_username
password your_password
③ 设置文件权限(仅当前用户可读):
chmod 600 ~/.netrc
④ 执行挂载命令(无需再输入凭据):
curlftpfs ftp.example.com ~/ftp_mount
挂载成功后,可通过ls命令查看挂载点中的文件,或使用df -h确认挂载状态:
ls ~/ftp_mount # 查看FTP服务器文件
df -h | grep ftp_mount # 检查挂载状态
使用完毕后,通过以下命令卸载挂载点:
fusermount -u ~/ftp_mount
若需系统启动时自动挂载,可将命令添加到/etc/fstab文件(需谨慎操作,避免密码泄露):
echo "ftp.example.com /home/your_username/ftp_mount fuse.curlftpfs user=your_username:your_password,allow_other 0 0" | sudo tee -a /etc/fstab
或使用~/.netrc文件时:
echo "ftp.example.com /home/your_username/ftp_mount fuse.curlftpfs noauto,user 0 0" | sudo tee -a /etc/fstab
然后执行sudo mount -a测试配置是否正确。
curlftpfs依赖FUSE(Filesystem in Userspace),确保系统已启用FUSE模块(默认多数Linux发行版已开启)。~/.netrc文件权限必须为600,否则会导致挂载失败。~/.netrc文件存储凭据。