Linux Setting up SSH
Here’s a step-by-step guide on setting up SSH in Linux, with example commands and outputs for each step.
Step 1: Install OpenSSH
First, ensure that OpenSSH is installed. Most Linux distributions have it by default, but you can install it with the package manager if needed.
Command:
Example Output:
Step 2: Start and Enable the SSH Service
After installation, start the SSH service and enable it to run at boot.
Commands:
Example Output:
You can verify that the SSH service is running with:
Example Output:
Step 3: Configure SSH (Optional)
SSH configuration can be modified in the /etc/ssh/sshd_config
file. Open this file with a text editor:
Command:
You can make adjustments here, such as changing the SSH port or disabling root login:
- Change the port by setting
Port 2222
(or any other unused port) - Disable root login by setting
PermitRootLogin no
After editing, save and close the file (if using nano
, press Ctrl+X
, then Y
to confirm).
Restart the SSH service to apply changes:
Step 4: Generate SSH Key Pair on Client
To set up SSH key authentication, you’ll first generate an SSH key pair on the client machine (the one you'll use to connect to the server).
Command:
Example Output:
Step 5: Copy the Public Key to the Server
To use the key for SSH login, copy your public key (id_rsa.pub
) to the server. This can be done using the ssh-copy-id
command:
Command:
Example Output:
Step 6: Connect to the Server Using SSH
Now that your key is set up, you can connect to the server without a password.
Command:
Example Output:
You're now successfully connected to your server using SSH! This setup allows secure remote access to your Linux server using SSH keys.