centos ftp

煎雞蛋湯發表於2018-03-20

本文於2018年1月12號釋出在個人部落格中,因為個人部落格關閉,全部遷移到CSDN,以下是正文:


最近在做一個專案調研,測試了一下ftp,整理記錄到這裡

ftp server

在CentOS 7的包管理中,ftp server的包名稱為:

[root@localhost ~]# yum search vsftpd
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Loading mirror speeds from cached hostfile
* base: mirrors.sohu.com
* extras: mirrors.163.com
* updates: mirrors.163.com
=============================== N/S matched: vsftpd ===============================
vsftpd-sysvinit.x86_64 : SysV initscript for vsftpd daemon
vsftpd.x86_64 : Very Secure Ftp Daemon

Name and summary matches only, use “search all” for everything.
[root@localhost ~]#

安裝

yum install -y vsftpd

看到下面的內容(0:3.0.2-22.el7不一定相同)表示安裝成功:

Installed:
 vsftpd.x86_64 0:3.0.2-22.el7

Complete!

啟動

預設情況下,vsftpd安裝完成不會啟動:

[root@localhost ~]# systemctl status vsftpd
● vsftpd.service – Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)

啟動方法:

systemctl start vsftpd

再看看狀態:

[root@localhost ~]# systemctl status vsftpd
● vsftpd.service – Vsftpd ftp daemon
Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2018-01-11 15:22:29 PST; 6s ago
Process: 3573 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
Main PID: 3576 (vsftpd)
CGroup: /system.slice/vsftpd.service
└─3576 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

Jan 11 15:22:29 localhost.localdomain systemd[1]: Starting Vsftpd ftp daemon…
Jan 11 15:22:29 localhost.localdomain systemd[1]: Started Vsftpd ftp daemon.

關閉vsftpd:

systemctl stop vsftpd

設定開機啟動

[root@localhost ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

建立使用者

ftp client訪問ftp server時需要使用使用者名稱/密碼的方式,使用者名稱密碼同ftp server上的OS使用者名稱密碼,建立方法:

[root@localhost ~]# useradd -m ftpclient
[root@localhost ~]# passwd ftpclient 
Changing password for user ftpclient.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

ftp client

安裝

在CentOS 7的包管理中,ftp client包名稱為:

ftp.x86_64 : The standard UNIX FTP (File Transfer Protocol) client

安裝:

yum install -y ftp

驗證:

[root@localhost ~]# whereis ftp
ftp: /usr/bin/ftp /usr/share/man/man1/ftp.1.gz

使用

本文測試使用兩臺虛擬機器:

192.168.120.130: 安裝了vsftpd,作為ftp server
192.168.120.131: 安裝了ftp,作為ftp client

登入ftp server:

[root@localhost ~]# ftp 192.168.120.130
Connected to 192.168.120.130 (192.168.120.130).
220 (vsFTPd 3.0.2)
Name (192.168.120.130:root): ftpclient
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>

檢視當前目錄:

ftp> pwd
257 "/home/ftpclient"
ftp> ls
227 Entering Passive Mode (192,168,120,130,227,109).
150 Here comes the directory listing.
226 Directory send OK.
ftp>

上傳檔案:

ftp> put /home/hello.txt ~/hello.txt
local: /home/hello.txt remote: ~/hello.txt
227 Entering Passive Mode (192,168,120,130,103,97).
150 Ok to send data.
226 Transfer complete.
40 bytes sent in 9.8e-05 secs (408.16 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (192,168,120,130,114,12).
150 Here comes the directory listing.
-rw-r--r-- 1 1001 1001 40 Jan 13 23:26 hello.txt
226 Directory send OK.
ftp>

下載檔案:

ftp> ls
227 Entering Passive Mode (192,168,120,130,90,75).
150 Here comes the directory listing.
-rw-r--r-- 1 1001 1001 40 Jan 13 23:26 hello.txt
-rw-rw-r-- 1 1001 1001 39 Jan 13 23:28 server.txt
226 Directory send OK.
ftp> get server.txt /home/server.txt
local: /home/server.txt remote: server.txt
227 Entering Passive Mode (192,168,120,130,136,226).
150 Opening BINARY mode data connection for server.txt (39 bytes).
226 Transfer complete.
39 bytes received in 6.2e-05 secs (629.03 Kbytes/sec)
ftp>

未解決問題

No route to host

ftp連線server時報錯:“ftp: connect: No route to host”,網上說需要開啟vsftpd的21/tcp埠:

firewall-cmd --zone=public --permanent --add-port=21/tcp
firewall-cmd --reload

在嘗試登入,可以成功,但執行部分命令仍然報錯:“ftp: connect: No route to host”

暫時關閉firewall來規避該問題:

systemctl stop firewalld

相關文章