DEV Community

Cover image for Mastering the ssh Command - Tips and Tricks
k1lgor
k1lgor

Posted on

Mastering the ssh Command - Tips and Tricks


Introduction

If you've ever needed to remotely access another computer, then you're probably familiar with the 'ssh' command. This powerful tool is used to securely connect to and manage remote systems, and it can be incredibly helpful for system administrators, developers, and anyone who needs to work on a remote machine. In this blog post, we'll explore some tips and tricks for using the 'ssh' command like a pro.

Basic Usage

Before we dive into some more advanced features, let's review the basic usage of the ssh command. To establish an SSH connection, simply type:

ssh username@hostname
Enter fullscreen mode Exit fullscreen mode

Replace username with your username on the remote system, and hostname with the IP address or domain name of the remote system. You may also need to specify a port number using the -p flag if the SSH server is running on a non-standard port.

Once you enter this command, you'll be prompted to enter your password for the remote system. If everything goes well, you'll be connected to the remote machine and can start running commands as if you were logged in locally.

Advanced Usage

Now that we've covered the basics, let's take a look at some more advanced features of the ssh command.

  1. Publick Key Authentication

Typing in your password every time you want to connect to a remote system can be a hassle, but luckily ssh supports public key authentication. This allows you to log in without typing your password, as long as you have a public key installed on the remote system. Here's how to set it up:

  • Generate a public/private key pair on your local machine using the ssh-keygen command. Make sure to keep your private key safe and secure.
  • Copy your public key to the remote system using the ssh-copy-id command. This will automatically add your public key to the remote system's authorized keys list.
  • Now, when you connect to the remote system, you should be able to log in without typing your password.
  1. Proxying Connections

Sometimes, you may need to connect to a remote system through an intermediate server. This is where proxying comes in handy. To set up a proxy connection, use the -J flag followed by the hostname of the intermediate server:

ssh -J username@intermediate_host username@destination_host
Enter fullscreen mode Exit fullscreen mode

This will establish a connection to the intermediate server first, and then to the final destination.

  1. Port Forwarding

Port forwarding is a way to forward network traffic from one port on your local machine to a port on a remote machine, through an SSH tunnel. This can be incredibly helpful for accessing services that are not available directly from your local network. Here's an example:

ssh -L 8080:localhost:80 username@remote_host
Enter fullscreen mode Exit fullscreen mode

This will forward all traffic from port 8080 on your local machine to port 80 on the remote host. Now, if you open a web browser on your local machine and navigate to localhost:8080, you'll see the website that's hosted on the remote machine.

Conclusion

The ssh command is a powerful tool that can be incredibly helpful for managing remote systems. By mastering some of the advanced features we've covered in this blog post, you'll be able to work more efficiently and securely on remote machines. Whether you're a system administrator or a developer, understanding the ssh command is an essential skill for anyone who needs to work with remote systems.

Thank you for reading 🧑‍💻

Stay tuned for more 🚀

✌️ and logout


Buy Me A Coffee

Top comments (0)