How to Install SSH on Linux & Useful Commands

New to Linux? Not sure what SSH is, or what you can even do with it? In this article, we’re going to explain how you can set up and use SSH on Linux. We’ll go over how it works, and some useful things you can do with it. Let’s get started!

SSH is one of the most used tools on the Linux platform. Despite this, it isn’t a program that comes set up on most mainstream Linux distributions (except Fedora and OpenSUSE). Configuring SSH is easy . If you’re just looking to connect to a remote Linux PC with SSH, it’s easiest to just install the client connection tool. This tool is just SSH itself, and nothing to run a connection server.

Still, it is a good idea to set up an SSH server on every Linux PC. This way there can always be two-way communication, and any PC can send out a connection and receive it too. Here’s how to get it running.

Install SSH Tools

Ubuntu

sudo apt install openssh-server

Arch Linux

sudo pacman -S openssh

sudo systemctl enable sshd

sudo systemctl start sshd

Debian

sudo apt-get install openssh-server

Fedora/OpenSUSE

Though SSH may already be installed, the server may not be enabled. To enable it, open a terminal and enter:

sudo systemctl enable sshd

sudo systemctl start sshd

Other

Other Linux distributions not mentioned here may have the SSH tools necessary to get everything working. If not, it is best to refer to the official manual for the version of Linux that you’re using. Alternatively, search in the place where you normally install programs, and search for: openssh-server, ssh, sshd, etc.

Next, open the configuration file for SSH, and change a setting or two. The main one being the default port. See, out of the box, SSH uses port 22. This is a very well known port. As a result, hackers and other bad actors will most likely go after this port and try to gain access to it. Consider changing the port to something random. It doesn’t really matter what port, just be sure it’s not one that is already used by other programs.

Edit the config file with: sudo nano /etc/ssh/ssh_config

Scroll down, and find # Port 22 in the file. Delete the “#”, and change the number from “22” to the port you want. Press Ctrl + O to save the changes. Then restart SSH using the following commands.

Ubuntu/Debian:

service ssh restart

Arch/OpenSUSE/Fedora:

sudo systemctl sshd restart

Note: if you do not plan on port-forwarding your SSH port to the internet, there is no need to change the SSH port. If it stays on LAN, it’s alright.

Connect with SSH to your server with:  ssh -p portnumber remote@host

Command Line File Transfer

SSH is useful when you need to remotely access another computer. This could be a computer you access over the internet, or one sitting in the same room. Sometimes when remotely accessing a Linux PC, you may need access to files. Maybe they’re configuration files or important documents. Sending files to a remote machine when you only have access to the Linux terminal can be really tedious.

This is why SSH comes with built in functionality to easily transfer files back and forth, directly over SSH.

scp /path/to/files-or-folders user@ipaddress:/path/to/destination

Replace /path/to/ and etc to reflect your own needs. For example, yours might look a little like this;

scp /home/derrik/importantfile.txt derrik@ubuntu-server:/home/derrik-server/files/

Secure FTP server anywhere

FTP is old technology, but that doesn’t mean it isn’t useful anymore. In fact, FTP is still used a lot when it comes to SSH. Why? It makes it very easy to access an entire remote Linux PC’s hard drive right within the file manager, locally. There’s no need to configure SSH to start using SFTP. Any Linux PC with an SSH server running has SFTP as well. To access it, open your file manager of choice, find “server”, “network” or something along those lines. Keep in mind, each Linux file manager is different, so it’s impossible to account for them all.

sftp://username@hostname:portnumber

When you enter the FTP address, you’ll be prompted to enter a password, as well as accept the key from the SSH server.

Note: you’ll need to forward out the port you use for SSH to access SFTP outside of your network. Refer to your router manual to learn how to port-forward.

Remote Back Up

SSH has many uses, including remotely backing up drives and partitions. To backup a hard drive from a local PC, and send the image over the network via SSH, use this:

Note: in this example, the hard drive being used is /dev/sda. You will need to change the command to suit your needs.

Partition backup

dd if=/dev/sda1 | gzip -1 - | ssh remote@host dd of=image.img.gz

Entire drive

dd if=/dev/sda | gzip -1 - | ssh remote@host dd of=image.img.gz

It is also possible to grab the remote drive’s hard disk image (or partitions):

Partition backup

ssh remote@host "dd if=/dev/sda1 | gzip -1 -" | dd of=image.img.gz

Entire drive

Edit the config file with:ssh remote@host "dd if=/dev/sda | gzip -1 -" | dd of=image.img.gz

Extract the image with: gzip -d image.img.gz

Viewing Remote Programs Locally

Sometimes the terminal isn’t enough when accessing a computer remotely. Luckily, with SSH, it is possible to do X window forwarding. This means you can use the x11 GUI server to forward remote windows to your local machine. For example, if you need to test a website on the remote Linux PC you’ll be able to start Firefox remotely, and forward the x11 Firefox window to your Linux PC, locally.

Enable X11 window forwarding by editing the ssh configuration file: sudo nano /etc/ssh/ssh_config

Look for # ForwardX11 no, remove “#“, and change “no” to “yes“.

Restart the SSH service.

Ubuntu/Debian:

service ssh restart

Arch/OpenSUSE/Fedora:

sudo systemctl sshd restart

When connecting over SSH, use the -X switch.

ssh -X -p portnumber remote@host

Conclusion

SSH is a useful tool, but not a lot of beginner Linux users know this. It’s a shame, as there are so many uses for it. In this article, we’ve just scratched the surface. When it comes to using secure shell, the only limit is your own skill.

قالب وردپرس

About Kelli Smith

Kelli Smith
Kelli runs customer support and creates content for Skillcrush, a digital skills training and education platform with friendly instructors, an active student community, and laser focus on helping you achieve your career goals with technology. She has an MBA and successfully ran an international company and her own freelancing business before pursuing her passion for tech by taking advanced web development classes. Kelli loves listening to tech podcasts at 2x speed, looking for cute Corgi photos online and teaching and performing country line dancing—as a true Texan living in Finland would do. Say hi on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *