I am trying to create a script to upload some files to a server via SFTP. I can do that manually by opening an interactive lftp-session and providing username and password there. For the script, I would like to

  • not hardcode credentials in the script (for obvious reasons)
  • not mention them on the commandline (I want the command in my .bash_history, but of course not the credentials)
  • have lftp read the credentials from .netrc or something similar

I cannot seem to get this working. My current workaround is a wrapper-script that parses the .netrc for the credentials and adds them to a lftp-script which I delete afterwards. This simulates the steps I perform manually, but seems like re-implementing existing functionality poorly. While this works, the question remains:

Can lftp read .netrc for SFTP-connections?

If so, are there special syntax-requirements if customs ports are part of the setup? I need to connect to sftp://username:password1@example.com:12322.


The best bet would be to use key pair to access the server as you do for ssh. Three steps:

  1. Create a key pair (if you don't have yet):

    ssh-keygen -t rsa -C "" -f ~/.ssh/id_rsa
  2. Copy public key to the server:

    ssh-copy-id user@example.com -p 12322
  3. Connect to the server:

    lftp sftp://user@example.com:12322

If you will use some non-standard path for the key, note that you should add the key to ssh-agent or to your .ssh/config before!


According to the lftp man page, ~/.netrc is supported/used by lftp:


The file is consulted to get default login and password to FTP server. Passwords are also searched here if an URL with user name but with no password is used.

Based on this reading, I suspect that your URL, using custom ports, should be fine.


You can store your credentials in ~/.netrc. The file should look like:

machine ftp.cluster.host.net (do not include 'sftp://' here)
login yournickname
password xFrPkIB8767

Then you can connect with

lftp sftp://yournickname@ftp.cluster.host.net/

The nickname is required, but the password is pulled from .netrc. Your terminal history is clean!

You can also directly pass a command to lftp:

lftp -c "open sftp://yournickname@ftp.cluster.host.net; ls"

Pretty usefull for scripts and bash aliases!

  • Now how do you do this with a custom port - just writing hostname:port in .netrc and in the lftp command doesn't work Dec 18 '20 at 13:37
  • Did you try with -p? Something like lftp -c "open sftp://yournickname@ftp.cluster.host.net -p 1313; ls" The [documentation[(lftp.yar.ru/lftp-man.html) does not give any other solution and I don't have any server with custom port to perform tests, sorry
    – roneo.org
    Dec 19 '20 at 14:14
  • Try also lftp -c -p 1313 "open sftp://yournickname@ftp.cluster.host.net; ls"
    – roneo.org
    Dec 19 '20 at 14:28
  • I found this is working: lftp -c "set cmd:default-protocol sftp ; open ftp.hostname.com:1313 ; ls" but the port should NOT be specified in the .netrc file - it should only be as the example above. Dec 28 '20 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.