If you need to securely connect to a remote machine and access a command line interface, SSH is the tool for you. The Secure Shell (SSH) protocol has been used for decades as a means of remotely interfacing with Unix/Linux/BSD machines over insecure connections.
SSH operates on a traditional client-server principle where a remote user (client) can connect to a server and execute commands as if sitting in front of the machine. SSH is especially useful if you have a server (perhaps on one of the best cheap web hosts) or a Raspberry Pi that you need to control remotely. Since these systems primarily use Linux, it is easy to modify scripts, modify key configuration files, or even install software remotely.
In this tutorial, we’ll explain how to create basic SSH connections from Windows, macOS, and Linux machines.
Before Opening SSH: Credentials and Access
To initiate an SSH connection from any operating system, you must first gain proper access to the remote device and you need its address, in the form of an IP address or name. valid host. You will need to enter the address to establish your connection, then you will be hit with a request for username and password.
If you have a web hosting account with SSH enabled, you’ll 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 login. If that doesn’t work, check the instructions on the host’s site. If you’re connecting to a server on your local network, you’ll need the computer name/IP address and to make sure the remote device has an account for you.
Using SSH on Windows
Although you can invoke SSH from the command line, most people prefer to use PuTTY, a free GUI application that makes logging in easier.
1. Download and install PuTTY. Windows has its own command prompt, which can be used with SSH, but PuTTY is by far the most accessible way to open an SSH connection.
2. Open putty. PuTTY’s user interface has a Hostname / IP Address section where we enter the details of 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 you are prompted to confirm your connection. If this is your first time logging into this system, you will be prompted for it.
You will now be connected to the remote system.
5. Enter your username and password as requested. The password is not displayed on the screen, this is normal behavior and a security feature.
You now control the remote machine, all commands will be executed using the remote machine.
6. Once the SSH session is complete, type exit and hit 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 or leave it open if your computer gets hacked.
Using SSH in Linux
In all major versions of Linux, SSH is built into the command line and easy to access that way. Note that we used Ubuntu for this tutorial, but these instructions would work with almost any Linux variant.
1. Open the terminal if you are not already on the command line. Press CTRL + ALT + T to do this or you can go to the applications menu and search for “terminal” to find its application.
2. Use SSH to login to the remote machine using your username and IP address/hostname. For example, our user was “pi” and the hostname was “smartipi”, here is what we would use:
3. Type yes if you receive a warning that the SSH connection key is not known. This may happen on the first login and you may need to login again after entering it.
4. Type your password if you are prompted. You won’t see the characters as you type them.
5. Press CTRL+D or type “log out” to end your session.
Use SSH in macOS
1. Launch the terminal by clicking on the terminal icon in the dock.
The terminal is ready for use.
2. Enter the SSH command followed by [email protected] In the example below, our user was “pi” and the hostname was “raspberrypi.local”.
3. Type Yes if you are prompted to confirm the connection for the first time.
4. Type your password when prompted.
5. Once the SSH session is complete, quit with 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 or leave it open if your computer gets hacked.
How to run a command and close an SSH connection
Sometimes we just need to run a single command on a remote machine. For example, it could be a Raspberry Pi that we need to shut down. Using the -t argument, we can log in, run a command, wait for output, and then log out.
1. Open a terminal / command prompt.
2. Use SSH to login to the remote machine, Add t” at the end of the command, then insert the command you want to run and press Enter. In our example, we will connect to a Raspberry Pi and then run the poweroff command to shut down the Pi.
ssh [email protected] -t "sudo poweroff"
3. Enter password as required
The remote computer should now disconnect you and then perform the task.