0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

lftpで "`ls' at 0 [Connection refused]"が起きた時の対処法

Posted at

lftpで ls コマンドを実行すると、以下のような表示が出ることがありました。
connecting... みたいなのが一番下の行でLoopして、Ctrl+ C で抜け出さないといけなくなりました。

.bash
lftp ftp-user@xx.xx.xxx.134:~> ls
FileCopy(0x5620ad0d03e0) enters state INITIAL
FileCopy(0x5620ad0d03e0) enters state DO_COPY
---- dns cache hit
---- attempt number 1 (max_retries=1000)
---- Connecting to xx.xx.xxx.134 (xx.xx.xxx.134) port 21
**** Socket error (Connection refused) - reconnecting
---- Closing control socket
---- dns cache hit
---- attempt number 2 (max_retries=1000)
---- Connecting to xx.xx.xxx.134 (xx.xx.xxx.134) port 21
**** Socket error (Connection refused) - reconnecting
---- Closing control socket
`ls' at 0 [Connection refused]

ちなみに、サーバー側のシステムログ(journalctl -af)や、vsftpdのログ(/var/log/vsftpd.log)も見てみましたが、何もそれらしきメッセージは見られませんでした。

エラーメッセージ

Socket error (Connection refused)

ファイアウォールを確認

サーバー側の21番ポートや、パッシブモードで使用するサーバーのポート番号の範囲にあるポートがfirewalldで許可されていることを確認します。

.bash
sudo firewall-cmd --permanent --list-all
public
  ...
  ......
  services: dhcpv6-client https mdns ssh
  ports: 80/tcp 443/tcp 65000-65535/tcp 21/tcp
  protocols:
  forward: yes
  masquerade: no
  forward-ports:
  source-ports:
  ......

コントロールコネクションの宛先ポートである21番と、パッシブモードで使用するサーバー側のポート範囲 (65000から65535番まで)が開放されています。

ポートでLISTENしているかどうか確認

ファイアウォールで確認したポートをnetstat等で確認し、LISTENしているかどうかを確認します。

.bash
[ec2-user@ip-xxx-xx-xx-250 ~]$ netstat -nao | grep ":21" # 21番ポートを指定して確認
[ec2-user@ip-xxx-xx-xx-250 ~]$ netstat -nao # すべての行を出力
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0    268 xxx.xx.xx.250:22        xxx.xx.xx.xxx:8955     ESTABLISHED on (0.03/0/0)
tcp        0      0 xxx.xx.xx.250:22        xxx.xx.xx.xxx:37103      SYN_RECV    on (6.90/5/0)
tcp        0      0 xxx.xx.xx.250:22        xxx.xx.xx.xxx:50816      SYN_RECV    on (1.78/5/0)
tcp        0      0 xxx.xx.xx.250:37356     1xx.xxx.xxx.xxx:80      TIME_WAIT   timewait (32.93/0/0)
tcp6       0      0 :::443                  :::*                    LISTEN      off (0.00/0/0)
tcp6       0      0 :::3306                 :::*                    LISTEN      off (0.00/0/0)
tcp6       0      0 :::80                   :::*                    LISTEN      off (0.00/0/0)
tcp6       0      0 :::22                   :::*                    LISTEN      off (0.00/0/0)

grepをかけずに見てみても、21番ポートが見当たりません。
エラーの字義通りなら、ソケットでFTP/FTPS接続が拒否されており、それはコントロールコネクションがおこなわれるはずの21番ポートで起こっている何かが原因かもしれません。

ポートを使用しているプロセスがvsftpdであることを確認

上記で21番ポートが存在する場合は、以下をおこないます。
ねんのため lsof -i | grep ":21"等でポートを指定してvsftpdであることを確認します。

ポートがクラッシュしているかもしれない

Socket error (Connection refused)であり、かつ21番ポートがファイアウォールでは許可されているがLISTENしていない場合、以下をおこないます。

.bash
# システムログやvsftpdのログをみる
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo journalctl -af # 最新のログを逐次ターミナルに表示させる
[ec2-user@ip-xxx-xx-xx-250 log]$ cd /var/log/ # vsftpdのログを探す (アプリケーション固有のログ)
[ec2-user@ip-xxx-xx-xx-250 log]$
[ec2-user@ip-xxx-xx-xx-250 log]$ ls # vsftpdのログを探す (アプリケーション固有のログ)
README         chrony                 dnf.log    dnf.rpm.log          hawkey.log-20250504  mariadb  tallylog             vsftpd.log-20250511  xferlog-20250504
amazon         cloud-init-output.log  dnf.log.1  firewalld            hawkey.log-20250511  php-fpm  vsftpd.log           wtmp                 xferlog-20250511
audit          cloud-init.log         dnf.log.2  hawkey.log           httpd                private  vsftpd.log-20250420  xferlog
btmp           dnf.librepo.log        dnf.log.3  hawkey.log-20250420  journal              sa       vsftpd.log-20250427  xferlog-20250420
btmp-20250501  dnf.librepo.log.1      dnf.log.4  hawkey.log-20250427  lastlog              sssd     vsftpd.log-20250504  xferlog-20250427
# ログの中身を見る
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo vi vsftpd.log-20250511
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo vi vsftpd.log
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo vi xferlog-20250511
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo vi xferlog

# vsftpdを再起動
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo systemctl restart vsftpd.service
[ec2-user@ip-xxx-xx-xx-250 log]$

# 再度、LISTENしているか確認
[ec2-user@ip-xxx-xx-xx-250 log]$ sudo netstat -nao | grep ":21"
tcp6       0      0 :::21                   :::*                    LISTEN      off (0.00/0/0) # LISTEN

vsftpdを再起動して、21番ポートがLISTENしていれば、エラーが解消されるはずです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?