Login Using SSH Private Key

Daily post #3 – Today I’ll share how to login to a server using SSH and a private key 🙂

#1. Save Your Private Key

The very first thing is to save your private key in a text file. It could be in .txt or .pem format.

Next, open terminal, navigate to the folder which contains your private key and change it’s permissions by typing in the following command:

[code]chmod 600 filename.txt[/code]

#2. Prepare Credentials

Before you login, you’ll need these SSH credentials:

  • hostname
  • username
  • port (default SSH/SFTP port is 22)

#3. Login Via SSH

Once you have the credentials, just open the terminal, navigate to the folder containing your private key file and type the following command:

[code]ssh -i filename.txt username@host -p port[/code]

You of course have to replace filename, username, host and port in that command with your respective server credentials.

If it returns a “permission denied” error, you’ll have to make sure you changed your private key’s permissions (check step #1).

#4. Other useful commands

Once you’re connected. You can make use of the following commands:

  • pwd (present working directory) – does pretty much what it says!
  • ls -al – shows a list of files and folders with their permissions
  • rm myFile.txt – delete a file
  • rm my*.txt – delete files starting with my and ending with .txt
  • rm -rf myfolder – delete a folder

That’s all in this post! Next post, tomorrow!