Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 7. SSH Remote Connection

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login: Using Server-Generated Key Pair

This page covers how to set up the SSH remote connection between local computer and the remote server.

There are two major approaches to establishing an SSH remote connection:

  • 1) Use a key pair generated by the Server
  • 2) Use a key pair generated by the Client.

In this section, we'll explain the first case, which is generally simpler than the second case.

SSH remote login using key pair generated by SERVER

Key Steps

1. Download a private key from your remote server or cloud service provider (in your web browser)

How and where you can get a private key can differ by service provider, so please follow the instructions of your service provider. In this section, we'll explain the case of AWS Lightsail.

2. Save the key to the .ssh directory under your home directory (on your local computer)

You can set another path for the key file; however, using the same location makes managing multiple key files easier.

3. For Mac mac.svg, change Access Mode to manage security (on your local computer)

windows.svg For Windows, this step may not be needed.

You need to change the access mode for the .ssh directory and the key you'll use for remote login. This is done to prohibit access to the directory and key files for other users.

  • .ssh: chmod 700
  • private key file: chmod 600

4. Run the ssh command from your local command line (on your local computer)

Three sets of information are required to run the command
1) private key file path
2) user name of your server
3) public IP address of your server

Run the following command to establish the SSH connection. The -i option is used to specify the key file path.

SSH Remote Login command

Practice

Below are the steps for establishing an SSH connection to an AWS Lightsail instance with your own SSH client.

1. Download a private key from the AWS Lightsail console to your local computer

From the AWS Lightsail Console, go to the instance page currently running and press the Download default key button. The key is already generated when you start using the service in your selected region.

Download a private key (default key) from the AWS Lightsail console to your local computer

2. Save the key to the .ssh directory under your home directory (on your local computer)

Usually, the default key is saved under the download directory. Move the file to the .ssh directory.

Save the key to the .ssh directory under your home directory (on your local computer)

3. For Mac mac.svg, change Access Mode to manage security (on your local computer)

windows.svg For Windows, this step may not be needed.

Check the current access mode of the .ssh directory and the public key file.
Run the following command under your home directory to see the .ssh directory's access mode.

Command Line - INPUT (Local)
ls -la

You may see the access mode of the directory as shown below. This means group owner users and other users have “read” (r) and “execute” (x) access to the directory.

Command Line - RESPONSE (Local)
drwxr-xr-x 6 user_a staff 192 Dec 26 .ssh

Next, check the key file's access mode. Run the following command.

Command Line - INPUT (Local)
ls -l .ssh

You may see the result like the one below. This means group owner users and other users have “read” (r) and “execute” (x) access to the public key.

Command Line - RESPONSE (Local)
-rw-r--r--@ 1 user_a staff 1679 Dec 26 LightsailDefaultKey.pem

To change access mode, run the following commands. For the public key file name, use the name on your computer.

Command Line - INPUT (Local)
chmod 700 .ssh
chmod 600 .ssh/LightsailDefaultKey.pem

Finally, check if the access mode is properly modified by running the ls command.

Command Line - INPUT (Local)
ls -la
Command Line - RESPONSE
drwx------ 6 user_a staff 192 Dec 26 .ssh
Command Line - INPUT
ls -l .ssh
Command Line - RESPONSE (Local)
:
-rw-------@ 1 user_a staff 1679 Dec 26 LightsailDefaultKey.pem

4. Run the SSH command from your local command line

Check the user name and the public IP address of your instance on the AWS Lightsail Console.

User name and the public IP address of AWS Lightsail instance

Run the following command on your local computer.

Command Line - INPUT (Local)
ssh -i [your key file path] [user name]@[public IP address]

In our case, the command will look like the one below.

Command Line - INPUT (Local)
ssh -i ~/.ssh/LightsailDefaultKey.pem ubuntu@18.143.143.190

If an SSH remote connection is successfully established, your command line will change to the user and private IP address of the server (not the public address of the server).

Command Line - INPUT (Remote)
 

Frequent Error Message

If you get an error message like the one below, you have an issue with the access mode setting. Check the access mode of your .ssh directory and the public key file.

Command Line - RESPONSE
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/user_a/.ssh/LightsailDefaultKey.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.

Exit the remote login

To exit the remote login, run the exit command.

Command Line - INPUT (Remote)
exit
Command Line - RESPONSE
logout
Connection to 18.143.143.190 closed.

You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

Exploring the Kernel and Shell in Linux

Kernel and Shell

Django MVT Architecture Explained

Django's MVT Framework

Applying Changes with Git Rebase

Rebase Branch – Git Rebase

How to Start a New Django Project

Start Django Project

Modifying User Profile with usermod Command

usermod (Modify User Account Information)

Exploring the Kernel and Shell in Linux

Kernel and Shell

Django MVT Architecture Explained

Django's MVT Framework

Applying Changes with Git Rebase

Rebase Branch – Git Rebase

How to Start a New Django Project

Start Django Project

Modifying User Profile with usermod Command

usermod (Modify User Account Information)

Tags:

SSH

Public Key

Private Key

Remote Connection

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server