If you ever need to securely connect to a remote computer and access the SSH command line interface, this is the tool for the job. The Secure Shell (SSH) protocol has been used for decades as a means of remotely interacting with Unix/Linux/BSD machines over insecure connections.
SSH works on the traditional client-server principle, where a remote user (client) can connect to the server and execute commands as if they were sitting at the machine. SSH is especially useful if you have a server (probably one of the best cheap web hosts) or a Raspberry Pi that needs to be controlled remotely. Because these systems mostly use Linux, it’s easy to do things like editing scripts, changing key configuration files, or even installing software remotely.
In this tutorial, we’ll look at how to create basic SSH connections. from Windows, macOS and Linux computers.
Before opening SSH: credentials and access
To initiate an SSH connection from any operating system, you first need proper access to the remote device, and you need its address in the form of either an IP address or a valid hostname. You will need to enter an address to establish a connection, and then you will be prompted for a username and password.
If you have a web hosting account with SSH enabled, you should probably be able to use the domain name as the address and then the same username and password you use to access FTP or cPanel to log in. If they don’t work, check the instructions on the host’s website. If you are logging into a server on your local network, you will need a computer name/IP address and make sure the remote device has an account for you.
Using SSH on Windows
While you can call SSH from the command line, most people prefer to use PuTTY, a free GUI application that makes it easy to connect.
1. Download and install PuTTY. Windows has its own command line that can be used with SSH, but PuTTY is by far the most readily available tool for opening an SSH connection.
2. Open PuTTY. The PuTTY user interface has a hostname/IP address section where we enter details about the machine we want to SSH into.
3. Enter the appropriate address in the Hostname or IP Address field and click Open.
4. Answer Yes if prompted to confirm the connection. If this is your first time connecting to this system, you will be prompted.
You will now be connected to the remote system.
5. Enter your username and password when prompted.The password is not displayed on the screen, this is normal behavior and a security feature.
Now you control the remote computer, any commands will be executed using the remote computer.
6. When the SSH session ends type exit and press Enter or just close the window. It’s important to close an SSH session when not in use so you don’t accidentally send commands to it and leave it open if your computer is hacked.
Using SSH in Linux
All major versions of Linux have SSH built into the command line, making it easy to access. Please note that we used Ubuntu for this tutorial, but these instructions will work with almost any version of Linux.
1. Open a terminal if you are not already at the command line. This can be done by pressing CTRL + ALT + T, or you can go to the applications menu and search for “terminal” to find its application.
2. Use SSH to login to a remote computer using your username and IP address/hostname. For example, our user was “pi” and the hostname was “smartipi” here is what we will use:
ssh pi@smartipi.local
3. Enter yes if you get a warning that the SSH connection key is unknown. This may happen the first time you connect and you may need to reconnect after logging in.
4. Enter your password if prompted. You won’t see the characters as you type them.
5. Press CTRL + D or type “logout” to end the session.
Using SSH on macOS
1. Start a terminal by clicking the terminal icon in the dock.
2. Type SSH followed by username@server. In the example below, our user was “pi” and the hostname was “raspberrypi.local”.
ssh pi@raspberrypi.local
3. Enter Yes if you are prompted to confirm the connection for the first time.
4. Enter your password when prompted.
5. When the SSH session ends log out by pressing CTRL+D or type exit and press Enter. It’s important to close an SSH session when not in use so you don’t accidentally send commands to it and leave it open if your computer is hacked.
How to run a command and close the SSH connection
Sometimes we just need to run one command on a remote machine. For example, it could be a Raspberry Pi that we need to shut down. Using the argument – t, we can log in, run a command, wait for the output, and then log out.
1.Open a terminal/command prompt.
2. Use SSH to login to the remote machine, append “-t” to the end of the command, then paste the command you want to run and hit enter.In our example, we will login to Raspberry Pi and then run the poweroff command to shut down the Pi.
ssh pi@raspberrypi .local -t "sudo poweroff"
3. Enter a password if necessary
The remote machine should now log out and then complete the task.