Search code examples
javascriptnode.jsubuntuftp

Can't connect by FTP and Node.js to Ubuntu 22 server, what to check?


I am running this to transfer a file to a Ubuntu 22 server by FTP and Node.js:

const ftp = require("basic-ftp")
const fs = require("fs")

example()

async function example() {
    const client = new ftp.Client()
    try {
        await client.access({
            host: "xxx",
            user: "root",
            password: "",
            secure: false
        })
        console.log(await client.list())
        await client.upload(fs.createReadStream("index.html"), "index.html")
    }
    catch(err) {
        console.log(err)
    }
    client.close()
}

I got this return:

Error: connect ECONNREFUSED XXX:21
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1495:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: 'XXX',
  port: 21
}

I don't know why. I don't think I see something in /var/log/auth.log. What could be going on? What to check? I can do what I want on the server.


Solution

  • SSH keys are used with SSH/SFTP on port 22. Not with FTP on port 21.

    So it seems that you are using a wrong protocol in node.js. It's actually not common for FTP to be enabled on Linux server. SSH/SFTP is the default and much more common.

    See How to upload a file with SFTP using an ASC file and username/password in NodeJS

    Also, if you connect with key in FileZilla, why are you using password in node.js?