Bird
Raised Fist0
Linux CLIscripting~5 mins

SSH connection basics in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
SSH lets you securely connect to another computer over a network. It solves the problem of safely managing remote servers without being physically present.
When you want to log into a remote server to check logs or fix issues.
When you need to transfer files securely between your computer and a remote machine.
When you want to run commands on a remote computer without being there.
When you manage cloud servers and need a secure way to access them.
When you want to set up a secure tunnel for other network services.
Commands
This command starts a secure connection to the remote server at example.com using the username 'user'.
Terminal
ssh user@example.com
Expected OutputExpected
The authenticity of host 'example.com (93.184.216.34)' can't be established. ECDSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'example.com,93.184.216.34' (ECDSA) to the list of known hosts. user@example.com's password:
This command connects to the remote server on port 2222 instead of the default port 22, useful if the server uses a custom SSH port.
Terminal
ssh -p 2222 user@example.com
Expected OutputExpected
user@example.com's password:
→
-p - Specify a custom port number for the SSH connection
This command creates a new RSA key pair for SSH authentication, which lets you connect without typing a password.
Terminal
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
Expected OutputExpected
Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX user@hostname The key's randomart image is: +---[RSA 2048]----+ | .o+*o. | | . +oo. | | . + +. | | o + | | . o S | | . . | | | | | | | +----[SHA256]-----+
→
-t - Specify the type of key to create (rsa)
→
-b - Set the number of bits in the key (2048 bits)
→
-f - Specify the filename to save the key
This command copies your public SSH key to the remote server, enabling passwordless login.
Terminal
ssh-copy-id user@example.com
Expected OutputExpected
Number of key(s) added: 1 Now try logging into the machine, with: "ssh user@example.com" and check to make sure that only the key(s) you wanted were added.
Now you connect again, but this time it uses your SSH key for authentication, so you won't need to enter your password.
Terminal
ssh user@example.com
Expected OutputExpected
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-50-generic x86_64) Last login: Tue Jun 6 10:00:00 2024 from 192.168.1.100 user@example:~$
Key Concept

If you remember nothing else from this pattern, remember: SSH lets you securely connect to and control a remote computer using a username and password or keys.

Common Mistakes
Trying to connect without the correct username.
The server won't recognize you and will deny access.
Always specify the correct username with ssh user@hostname.
Not adding the remote server's key to known hosts when prompted.
SSH will refuse to connect or warn you about security risks.
Type 'yes' to accept and add the server's key the first time you connect.
Not generating or copying SSH keys before trying passwordless login.
You will still be asked for a password every time.
Generate SSH keys with ssh-keygen and copy them with ssh-copy-id before connecting.
Summary
Use ssh user@hostname to start a secure remote connection.
Generate SSH keys with ssh-keygen to enable passwordless login.
Copy your public key to the server with ssh-copy-id for easier access.

Practice

(1/5)
1. What does the command ssh user@hostname do?
easy
A. Lists files on the remote computer.
B. Copies files from the remote computer to local.
C. Starts a secure connection to the remote computer as 'user'.
D. Deletes the user account on the remote computer.

Solution

  1. Step 1: Understand the ssh command purpose

    The ssh command is used to securely connect to another computer over a network.
  2. Step 2: Analyze the syntax ssh user@hostname

    This syntax means connect as 'user' to the computer identified by 'hostname'.
  3. Final Answer:

    Starts a secure connection to the remote computer as 'user'. -> Option C
  4. Quick Check:

    ssh user@hostname = Secure connection [OK]
Hint: ssh user@host means connect securely as user [OK]
Common Mistakes:
  • Thinking ssh copies files instead of connecting
  • Confusing ssh with file listing commands
  • Assuming ssh deletes accounts
2. Which of these is the correct syntax to connect to a remote server with ssh?
easy
A. ssh user@hostname
B. ssh -connect user hostname
C. ssh connect user@hostname
D. ssh user hostname

Solution

  1. Step 1: Recall ssh command syntax

    The correct syntax is ssh user@hostname without extra words or flags for basic connection.
  2. Step 2: Check each option

    Options A, B, and D have incorrect syntax or missing '@' sign.
  3. Final Answer:

    ssh user@hostname -> Option A
  4. Quick Check:

    Correct ssh syntax = ssh user@hostname [OK]
Hint: ssh always uses user@hostname format [OK]
Common Mistakes:
  • Adding extra words like 'connect' in command
  • Omitting '@' between user and hostname
  • Using flags incorrectly for basic ssh
3. What will happen if you run ssh alice@192.168.1.10 and the password is correct?
medium
A. You will see a list of files on your local computer.
B. The remote computer will shut down.
C. Your local files will be deleted.
D. You will be logged into the remote computer as user alice.

Solution

  1. Step 1: Understand ssh login process

    Running ssh with user and IP tries to connect and log in to that remote machine.
  2. Step 2: Check what happens on correct password

    If password is correct, you get a command prompt on the remote machine as that user.
  3. Final Answer:

    You will be logged into the remote computer as user alice. -> Option D
  4. Quick Check:

    ssh + correct password = remote login [OK]
Hint: Correct password means remote login success [OK]
Common Mistakes:
  • Thinking ssh shuts down remote machine
  • Confusing ssh with file deletion
  • Assuming ssh lists local files
4. You try to connect with ssh bob@server.com but get the error: Permission denied (publickey). What is the likely problem?
medium
A. You typed the wrong hostname.
B. Your SSH key is missing or not accepted by the server.
C. The server is turned off.
D. You forgot to install ssh on your computer.

Solution

  1. Step 1: Understand the error message

    "Permission denied (publickey)" means the server expects an SSH key for authentication but did not accept yours.
  2. Step 2: Identify cause of error

    This usually means your SSH key is missing, not loaded, or not authorized on the server.
  3. Final Answer:

    Your SSH key is missing or not accepted by the server. -> Option B
  4. Quick Check:

    Permission denied (publickey) = SSH key issue [OK]
Hint: Permission denied (publickey) means SSH key problem [OK]
Common Mistakes:
  • Assuming hostname typo causes this error
  • Thinking server power state causes this error
  • Believing ssh must be installed locally to connect
5. You want to connect to a remote server at example.com as user admin but the server uses a non-standard port 2222. Which command will connect correctly?
hard
A. ssh -p 2222 admin@example.com
B. ssh admin@example.com:2222
C. ssh admin@example.com -port 2222
D. ssh admin@2222.example.com

Solution

  1. Step 1: Recall how to specify a port in ssh

    The correct flag to specify a port is -p followed by the port number.
  2. Step 2: Analyze each option

    ssh -p 2222 admin@example.com uses -p 2222 correctly. Options A, C, and D use incorrect syntax or wrong placement.
  3. Final Answer:

    ssh -p 2222 admin@example.com -> Option A
  4. Quick Check:

    Use -p to specify port in ssh [OK]
Hint: Use ssh -p port user@host for custom ports [OK]
Common Mistakes:
  • Putting port after hostname with colon
  • Using -port instead of -p
  • Changing hostname to include port number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard