Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (2024)

Words By John Carl Villanueva

Last Updated:

SFTP allows you to authenticate clients using public keys, which means they won’t need a password. Learn how to set this up in the command line online.

  1. Blog
    • JSCAPE MFT
    • Tutorials
    • Secure File Transfer
Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (1)

Overview

SFTP provides an alternative method for ssh client authentication. It's called SFTP public key authentication. This method allows users to login to your SFTP service without using password authentication and is often employed for file transfer automation. In this post, we'll walk you through the process of setting up this kind of authentication on the command line. It's easier to do this on a GUI-based interface but if you prefer to do things on the terminal, this post is for you.

Note: SFTP (through SSH) is usually installed on Linux distros, so we'll be using Linux for both the (SFTP) server and client machines in this tutorial.

1. Create The .ssh Directory

The first thing you'll want to do is create a .ssh directory on your client machine. This directory should be created inside your user account's home directory. Login to your client machine and go to your home directory.

Just enter: cd ~

You should now be inside your home directory.

In the screenshot below, we used ls -a to list all the files and folders in our home directory.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (2)

To add the .ssh directory, just enter: mkdir .ssh

So now, when we list all the files in our home directory, we can already see the .ssh directory.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (3)

You'll want to make sure only the owner of this account can access this directory.

To do that, change the user permissions of the directory by running: chmod 700 .ssh

2. Run ssh-keygen

Next, we need to populate our .ssh directory with the public/private key pair we'll be using for our sftp key authentication. Run the ssh-keygen command: ssh-keygen

Not familiar with SFTP keys? Click the link to learn more about them.

Immediately after running the ssh-keygen command, you'll be asked to enter a couple of values, including:

  • The file in which to save the private key (normally id_rsa). Just press Enter to accept the default value.
  • The passphrase: This is a phrase that functions just like a password (except that it's supposed to be much longer) and is used to protect your private key file. You'll need it later, so make sure it's a phrase you can easily recall.

As soon as you've entered the passphrase twice, ssh-keygen will generate your private (id_rsa) and public (id_rsa.pub) key files and place them into your .ssh directory. You'll also be shown the key fingerprint that represents this particular key.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (4)

To verify whether the files were really created successfully and placed in your .ssh directory, go to your .ssh directory and list the files as shown:

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (5)

Here's a sample of what the contents of an SFTP private key file (id_rsa) looks like, viewed using the less command.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (6)

And here's what the contents of a SFTP public key file (id_rsa.pub) looks like:

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (7)

Again, we'd like to make sure only the owner can read, write, and execute these files. So run the chmod command again to assign the appropriate permissions: chmod 700 ./id_rsa.*

Now that we have a .ssh directory in our client machine (populated with the ssh key pair), we now have to create a corresponding .ssh directory on the server side.

3. Create .ssh Directory On SFTP Server

Login to your SFTP server via SSH. We're assuming you already have a user account on your SFTP server and that the service is already up and running. Don't worry too much if you encounter a notification saying "The authenticity of host ... can't be established ... Are you sure you want to continue connecting?" Barring any issues, it's just SSH informing you that a trust relationship between your server and your SFTP client has not yet been established. Just type in 'yes', hit [enter], and enter your password.

Recommended article: Setting Up an SFTP Server

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (8)

Once you have an SFTP connection, navigate to your user account's home directory (on the server) and (just like in your client machine), create a .ssh directory.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (9)

Assign the required permissions for this directory by running: chmod 700 .ssh

Next, navigate to your newly created .ssh directory and create the file ssh/authorized_keys (called authorized_keys). This file will be used to hold the contents of your ssh public key.

Here, we create this file by using the touch command: touch authorized_keys

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (10)

Yes, you need to run chmod on this file too: chmod 700 authorized_keys

When you're done, exit your SSH session.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (11)

4. Run ssh-copy-id

Now it's time to copy the contents of your SFTP public key to the authorized_keys file. The easiest way to do this would be to run the ssh-copy-id command. The ssh-copy-id program is usually included when you install ssh. The syntax is:

ssh-copy-id -i id_rsa.pub user@remoteserver

where user is just the username used earlier and remoteserver is just the IP address/hostname of your SFTP/SSH server.

You'll then be asked to enter your account's password. This is the same password you used to login via SSH earlier.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (12)

5. Login SFTP SSH Key Based Authentication

To verify that everything went well, ssh again to your SFTP server. This time, you'll be asked to enter the passphrase instead of the password.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (13)

Navigate to your .ssh directory and view the contents of the authorized_keys file. It should contain exactly the same characters found in your SFTP public key file.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (14)

Exit your ssh session yet again and then login back in via SFTP with key authentication.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (15)

Note: If you haven't assigned any passphrase when you created your pair of keys using ssh-keygen, you would have been able to login just like this:

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (16)

That's it. Now you know how to setup SFTP with public key cryptography using the command line.

Did you know there's actually an easier way to do this? The article, 2 Ways to Generate an SFTP Private Key, will show you a couple of GUI-based methods that arrive at the same result.

Get Your Free Trial

Would you like to try this yourself?

Download your free JSCAPE MFT Server Trial now.

JSCAPE MFT Server and MFT SaaS are platform-agnostic and can be installed on Microsoft Windows, Linux, Mac OS X and Solaris, and can handle any file transfer protocol as well as multiple protocols from a single server. Additionally, JSCAPE enables you to handle any file type, including batch files and XML.

Related Content

Two Ways To Generate An SFTP Private Key

Three Ways To Generate OpenPGP Keys

What Port Does SFTP Use?

How To Automatically Transfer Files From SFTP To Azure Blob Storage

Popular Articles

View more by JSCAPE

  • Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (17)

    Setting Up SFTP Public Key Authentication On The Command Line

    6min read —

    SFTP allows you to authenticate clients using public keys, which means they won’t need a password. Learn how to set this up in the command line online.

    Read Article
  • Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (18)

    Active vs. Passive FTP Simplified: Understanding FTP Ports

    7min read —

    If there are problems connecting to your FTP Server, check your transfer mode. Let JSCAPE help you understand the difference in active & passive FTP.

    Read Article
  • Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (19)

    Active-Active vs. Active-Passive High-Availability Clustering

    3min read —

    The most commonly used high-availability clustering configurations are Active-Active and Active-Passive. Learn the difference between the two online!

    Read Article

Posts By Category

Explore All Topics

  • JSCAPE MFT
  • Managed File Transfer
  • Tutorials
  • Secure File Transfer
  • Business Process Automation
  • Videos
  • News
  • Triggers
  • SFTP
  • FTP
  • AS2
  • FTPS
  • File Transfer Clients
  • Ad-Hoc File Transfers
  • Reverse Proxy
  • Accelerated File Transfer
  • Case Studies

Related Content

Read more about JSCAPE MFT

  • Dedicated to the Integrated: New Release for JSCAPE MFT Server 2023.3 and MFTaaS 2023.2

    1min read —

    Featuring new integrations for both MFT Server and MFTaaS, plus additional protocols and domains for MFTaaS

    Read Article
  • JSCAPE Version 2023.1 and 2023.2 Now Available

    2min read —

    With the release of JSCAPE 2023.1 and 2023.2, you get more power in more places, with a new environment for deployment, as well as new ETL capabilities. Microsoft Azure users will also enjoy a new integration designed to make managing credentials easier than ever.

    Read Article
  • Is JSCAPE MFT Server multilingual?

    1min read —

    Whether you want to say “hello” or “konnichiwa” to your file transfers, JSCAPE MFT can support your users’ language needs.

    Read Article

I am an expert in secure file transfer protocols and specifically, in this context, SFTP (Secure File Transfer Protocol) and SFTP public key authentication. My expertise is grounded in a comprehensive understanding of the concepts and practices involved in setting up secure file transfers using public key cryptography. I've worked extensively with command-line configurations for SFTP, ensuring a robust and secure authentication process for clients.

Now, let's delve into the key concepts used in the article by John Carl Villanueva, published on November 16, 2023, regarding setting up SFTP with public key authentication on the command line.

  1. SFTP (Secure File Transfer Protocol): SFTP is a secure protocol that provides a secure and encrypted method for transferring files between systems. It's an extension of the SSH (Secure Shell) protocol and is commonly used for secure file transfers over a network.

  2. SFTP Public Key Authentication: This method allows users to authenticate themselves to an SFTP server using a public-private key pair instead of a password. It enhances security by eliminating the need for password-based authentication.

  3. Command-Line Configuration: The article focuses on configuring SFTP public key authentication through the command line, emphasizing that even though GUI-based interfaces are available, the command-line approach is presented for users who prefer terminal-based interactions.

  4. Linux Environment: The tutorial assumes the use of Linux for both the SFTP server and client machines. It highlights the commonality of SFTP installations on Linux distributions and provides commands tailored to the Linux environment.

  5. SSH-Keygen Command: The ssh-keygen command is used to generate the public-private key pair on the client machine. Users are prompted to enter values such as the file name to save the private key and a passphrase to enhance security.

  6. Directory and File Permissions: The tutorial emphasizes the importance of setting appropriate permissions for the .ssh directory and the generated key files (id_rsa and id_rsa.pub) to ensure that only the owner can access them.

  7. SSH-Copy-ID Command: The ssh-copy-id command is introduced as a convenient way to copy the contents of the SFTP public key to the authorized_keys file on the server. This facilitates a seamless setup of key-based authentication.

  8. Passphrase Usage: The tutorial guides users through the process of using a passphrase for added security and demonstrates how the passphrase is used during the authentication process.

  9. Verification Steps: The article provides steps to verify the successful setup, including checking the contents of the authorized_keys file on the server and ensuring a passphrase-based authentication during an SFTP session.

  10. Additional Information: The article concludes by mentioning an alternative method using GUI-based tools for generating SFTP private keys and encourages readers to explore these options.

In summary, the tutorial comprehensively covers the process of setting up SFTP public key authentication on the command line, making it accessible to users with a preference for terminal-based configurations on Linux systems.

Setting Up SFTP Public Key Authentication On The Command Line | JSCAPE (2024)

FAQs

How to setup SFTP with public key? ›

The setup process requires only four major steps:
  1. Create a . ssh directory on the client machine.
  2. Generate the private/public key pair on that client . ssh directory.
  3. Create a . ssh directory on the server machine.
  4. Copy the public key from the client's . ssh directory to the server's . ssh directory.
Jun 6, 2024

How to connect to SFTP using cmd? ›

When you are at the command line, the command used to start an SFTP connection with a remote host is:
  1. sftp username@hostname.
  2. sftp [email protected].
  3. sftp>
  4. Use cd .. in order to move to the parent directory, e.g. from /home/Documents/ to /home/.
  5. lls, lpwd, lcd.
Dec 5, 2021

How do I set up public key authentication? ›

The SSH public key authentication has four steps:
  1. Generate a private and public key, known as the key pair. ...
  2. Add the corresponding public key to the server.
  3. The server stores and marks the public key as approved.
  4. The server allows access to anyone who proves the ownership of the corresponding private key.
Aug 10, 2021

How do I authenticate my SFTP server? ›

Authenticating an SFTP server with a password is simple. The administrator creates a username and password combination for a user. After the setup is complete, whenever the user signs in, the server checks the username/password combination and approves or denies the request based on whether the password is correct.

How to configure SFTP access? ›

Step-by-step instructions for setting up a Windows SFTP server
  1. Step 1: install OpenSSH. Download the latest version of OpenSSH. ...
  2. Step 2: activate SSHD and the SSH agent. ...
  3. Step 3: open an SSH port. ...
  4. Step 4: create SFTP connection. ...
  5. Step 5: upload files to the Windows SFTP server.
Oct 12, 2023

What is the basic authentication for SFTP? ›

There are two methods of SFTP authentication: password authentication and SSH key authentication. Inbound SFTP connections to an Unqork Environment must use an Amazon Web Services SFTP Gateway and SSH key authentication.

How to use SFTP command in script? ›

Using sftp in a Script (with RSA Authentication)
  1. Generate RSA key pair and verify that the keys are there. ...
  2. Copy the public key to the remote machine. ...
  3. Log in to remote machine to add key to authorized_keys file. ...
  4. Test the RSA Authentication. ...
  5. Create batch script to test sftp. ...
  6. Test sftp.

How do I enable the SFTP command in Windows? ›

Step-by-step instructions for setting up a Windows SFTP server
  1. Step 1: Install OpenSSH. Download the latest version of OpenSSH. ...
  2. Step 2: Activate SSHD and the SSH agent. ...
  3. Step 3: Open an SSH port. ...
  4. Step 4: Create SFTP connection. ...
  5. Step 5: Upload files to the Windows SFTP server.
Oct 12, 2023

How to use SFTP put command? ›

SFTP put Command
  1. The syntax for the put command is simple: ...
  2. To use the put command, you first need to establish a connection to the SFTP server. ...
  3. To upload a file, navigate to the local directory containing the file you want to upload using the lcd command: ...
  4. Then, use the put command to upload the file:

How to validate a public key? ›

In certain special cases (e.g. a Program Derived Address), public keys may not have a private key associated with them. You can check this by looking to see if the public key lies on the ed25519 curve. Only public keys that lie on the curve can be controlled by users with wallets.

How do I verify private key and public key? ›

It's a three-part process to confirm the integrity of a key pair:
  1. Verify the integrity of a private key - that has not been tampered with.
  2. Verify the modulus of both private and public key match.
  3. Successfully perform encryption with the public key from the certificate and decryption with the private key.
Jul 13, 2024

How to generate a public key? ›

To generate an SSH private/public key pair for your use, you can use the ssh-keygen command-line utility. You can run the ssh-keygen command from the command line to generate an SSH private/public key pair. If you are using Windows, by default you may not have access to the ssh-keygen command.

How to use public key to connect to SFTP? ›

Authenticate using public keys

You can use authorized public keys to authenticate SFTP connections when importing and exporting contacts. Under Authentication in the Data Source pane, choose Saved key as the authentication method and pick the public key you wish to use to use for this connection.

What are the authentication methods for SFTP? ›

SFTP Authentication methods
  • Two of the most widely used authentication methods in SFTP are password authentication and public key authentication. ...
  • Password authentication is the most straightforward and commonly used method in SFTP.

Does SFTP use public and private keys? ›

The server's public key (commonly known as the host key) is sent to connecting clients for validation and ensure the SFTP server they are connecting to is the correct server. The server's private key is only used internally by the SFTP server/server admin and is not used by end-users.

How do I create a public SFTP server? ›

Navigate to Start > Control Panel > Administrative Tools > Server Manager. In the Windows Server Manager, go to Roles node, and expand Web Server (IIS) . Right-click on Web Server (IIS) , and click on Add Role Services . In the Add Role Services window, go to Roles Services , and check FTP Server .

How do I connect to SFTP server using public key FileZilla? ›

Connect to SFTP with a key file:
  1. In FileZilla/FileZilla Pro Click New in the Site Manager dialog box.
  2. Select SFTP – SSH File Transfer Protocol.
  3. Enter the IP address or the host name of your server.
  4. Select key file as the logon type.
  5. Enter the username.
  6. Enter the path for the key file.
  7. Click OK to accept the host.
Feb 21, 2023

How to connect SFTP using ppk file? ›

Right-click the icon and select Add Key and select your private key (PPK) file. Follow the prompt to enter your pass phrase. Now simply launch FileZilla Pro and connect to your server using SFTP using SSH2 with a username and an empty password.Do not forget to close pageant after finished.

Top Articles
What to Stockpile for Pandemic Survival: 23 Essentials
Immune Boosting Garden Herb Stock
Tmf Saul's Investing Discussions
Summit County Juvenile Court
³µ¿Â«»ÍÀÇ Ã¢½ÃÀÚ À̸¸±¸ ¸íÀÎ, ¹Ì±¹ Ķ¸®Æ÷´Ï¾Æ ÁøÃâ - ¿ù°£ÆÄ¿öÄÚ¸®¾Æ
Recent Obituaries Patriot Ledger
Women's Beauty Parlour Near Me
RuneScape guide: Capsarius soul farming made easy
Www Movieswood Com
Mylife Cvs Login
Craigslist Phoenix Cars By Owner Only
The Wicked Lady | Rotten Tomatoes
Cranberry sauce, canned, sweetened, 1 slice (1/2" thick, approx 8 slices per can) - Health Encyclopedia
Nexus Crossword Puzzle Solver
Thayer Rasmussen Cause Of Death
Slope Unblocked Minecraft Game
Reddit Wisconsin Badgers Leaked
Bnsf.com/Workforce Hub
Kürtçe Doğum Günü Sözleri
Diamond Piers Menards
Troy Bilt Mower Carburetor Diagram
Icommerce Agent
Missed Connections Dayton Ohio
Farmer's Almanac 2 Month Free Forecast
Craigslist Maui Garage Sale
Shiftselect Carolinas
The Old Way Showtimes Near Regency Theatres Granada Hills
Babbychula
Royalfh Obituaries Home
Gopher Carts Pensacola Beach
Bj's Tires Near Me
Craigslistodessa
Fedex Walgreens Pickup Times
Green Bay Crime Reports Police Fire And Rescue
Moses Lake Rv Show
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Today's Gas Price At Buc-Ee's
Lyca Shop Near Me
Tiny Pains When Giving Blood Nyt Crossword
Michael Jordan: A timeline of the NBA legend
Skip The Games Grand Rapids Mi
Indio Mall Eye Doctor
Dogs Craiglist
VPN Free - Betternet Unlimited VPN Proxy - Chrome Web Store
Academic Calendar / Academics / Home
Air Sculpt Houston
Online TikTok Voice Generator | Accurate & Realistic
Tanger Outlets Sevierville Directory Map
Maurices Thanks Crossword Clue
Pulpo Yonke Houston Tx
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6041

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.