Setting up an SSH server on Ubuntu

Alright, setting up an SSH server on Ubuntu! This is one of those things that can be super confusing for new Linux users, but don’t worry bro! we’ve got you covered with this handy guide.

First off, what the ***** is SSH? Well, it stands for Secure Shell and basically allows you to remotely access your Ubuntu server from another computer using a secure connection. This can be super useful if you want to manage your server without having to physically be in front of it or if you’re working on multiple projects at once and need to switch between them quickly.

So, let’s get started! First thing’s first make sure that SSH is installed on your Ubuntu machine. If it isn’t already, open up a terminal window (you can do this by pressing Ctrl + Alt + T) and type:


# This script installs SSH on an Ubuntu machine using the apt-get command.
# It is useful for remote access to the machine or for switching between projects quickly.

# First, update the list of available packages on the machine.
sudo apt-get update

# Then, install the openssh-server package, which allows for SSH connections.
# The -y flag automatically confirms any prompts during the installation process.
sudo apt-get install openssh-server -y

This will download and install the OpenSSH server package. Once that’s done, you should be good to go!

Now, setting up your SSH keys. This is a super handy feature that allows you to log in without having to enter a password every time which can save you a ton of time and hassle if you’re working on multiple projects at once or need to access your server frequently.

To set up your SSH keys, open up another terminal window (or just leave the one we opened earlier) and type:


# Install ssh-copy-id package using apt-get with sudo privileges
sudo apt-get install ssh-copy-id -y

# This command will allow you to log in without entering a password every time
# by setting up SSH keys, saving time and hassle when working on multiple projects
# or frequently accessing a server.

# To set up SSH keys, open a new terminal window or use the one previously opened
# and type the following command:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@server_ip_address

# This command will copy your public key to the server, allowing you to log in
# without a password. Make sure to replace "user" with your username and
# "server_ip_address" with the IP address of your server.

This will download and install the `ssh-copy-id` package. Once that’s done, you can generate your SSH keys by typing:

# This code segment generates SSH keys using the ssh-keygen command
ssh-keygen

Follow the prompts to create a new key or use an existing one it doesn’t really matter which option you choose here since we’re just setting up our server for now. Once that’s done, copy your public SSH key by typing:

# This code segment is used to display the contents of the id_rsa.pub file, which contains the public SSH key.
# The "cat" command is used to concatenate and display the contents of a file.
# The "~/.ssh" directory is the default location for storing SSH keys.
# The "id_rsa.pub" file is the public key file, which contains the key that will be used for authentication.

cat ~/.ssh/id_rsa.pub

This will display the contents of your `~/.ssh/id_rsa.pub` file which is basically just a string of characters that represents your public SSH key. Copy this string to your clipboard (you can do this by highlighting it and pressing Ctrl + C).

Now, let’s add our new SSH keys to the server we want to access using `ssh-copy-id`. To do this, type:


# This script is used to add new SSH keys to a server for remote access.
# It requires the use of the `ssh-copy-id` command.

# First, we need to specify the user and server IP address we want to access.
# This is done by using the [username]@[server_ip] format.
sudo ssh-copy-id [username]@[server_ip]

# The `sudo` command is used to run the following command with root privileges.
# This is necessary for making changes to the server's SSH configuration.

# The `ssh-copy-id` command is used to copy the public SSH key to the server.
# This allows for secure authentication when accessing the server remotely.

# Once the command is executed, the user will be prompted to enter their password.
# This is to verify that the user has permission to make changes to the server.

# If successful, the response will include a message stating that the key has been added.
# This means that the SSH key has been successfully copied to the server and can now be used for remote access.

Replace `[username]` with your Ubuntu username and `[server_ip]` with the IP address of the server you want to access. This will add your public SSH key to the authorized keys file on that server, which means you won’t have to enter a password every time you log in!

And there you have it setting up an SSH server on Ubuntu using `ssh-copy-id` and generating new SSH keys. It might seem like a lot of work at first, but trust us once you get the hang of it, managing your servers remotely will be a breeze!

SICORPS