29

I want to connect to server via sftp using specific IP and a specific (non-default) PORT .

How can I choose non-default port when connecting using sftp?

Yaron
  • 13,483

2 Answers2

37

You can use the -oPort=port_number option

sftp -oPort=port_number host_name

man sftp

sftp - secure file transfer program

sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config] [-o ssh_option] [-P sftp_server_path] [-R num_requests] [-S program] [-s subsystem | sftp_server] host

-o ssh_option
Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for specifying options for which there is no separate sftp command-line flag. For example, to specify an alternate port use: sftp -oPort=24. For full details of the options listed below, and their possible values, see ssh_config(5).

Yaron
  • 13,483
  • 2
    I got the answer :

    sftp://xx.xx.xxx.xxx:port

    ex. sftp://158.45.47.66:4343

    – Meet Vaishnani Aug 10 '17 at 05:06
  • @MeetVaishnani - do you need to access the sftp server using a browser or using a terminal/command-line? – Yaron Aug 10 '17 at 05:11
  • I want to use it from browser. – Meet Vaishnani Aug 10 '17 at 05:48
  • @MeetVaishnani - Okay - understood. you might want to add your comment, as an answer to your question. – Yaron Aug 10 '17 at 05:53
  • 2
    Worth noting: sftp foo@host.tld -oPort=1234 will not work, you have to put the ssh option before the user@host – DM8 Feb 28 '18 at 15:24
  • @DM8 - who did you refer your comment to? I think that in my answer the port parameter is before the host_name parameter – Yaron Mar 04 '18 at 13:04
  • @Yaron ...myself and anyone like me headscratching trying to figure it out :) – DM8 Mar 05 '18 at 20:54
  • When using the command line, a more complete example might be sftp -oPort=2222 LOC-FILE.TXT user123@remote.host:Downloads/REMOTE-FILE.TXT You need to use the username of the remote system (e.g, user123) in order to properly log onto that system. – PatS Apr 24 '25 at 19:37
12

You can use -P option or -oPort option to SFTP to a server from a specific port.

sftp -oPort=port_no username@x.x.x.x

eg. sftp -oPort=8022 rahul@x.x.x.x

or

sftp -P port_no username@x.x.x.x

eg. sftp -P 8022 rahul@x.x.x.x

Note that -P is capital P not small