Working on a project, you are constantly faced with the need to perform certain commands in terminal. On a local server, this issue is solved quite simply - either through the built-in console of your operating system, or using the IDE, for example, the built-in terminal in Visual Code.
However, there is a situation when you have deployed your project on a hosting and you need to use the server console directly on an external server. This feature is usually available in various hosting/server account management panels.
But what if we want to work with a remote server via a terminal on our local machine?
Let's start with the fact that you log into the hosting and create an SSH key through the terminal on the server.
Here is a step-by-step instruction:
Creating an SSH key on the hosting
- Log in to the hosting
Use the control panel of your hosting to access the terminal. This can usually be done through the "SSH" or "Console" section in the control panel.
- Open the server terminal
If you have access to the terminal via the web interface, open this terminal.
- Create an SSH key
In the terminal, run the following command to create a new SSH key:
ssh-keygen -t rsa -b 2048
You will be asked to specify a file name to save the key. Press Enter to save the key in the standard location (
~/.ssh/id_rsa
), or specify another path if necessary.You may also be asked to enter a password to protect the key (this is optional, but recommended for increased security). If you don't want to use a password, just press Enter.
- Check the generated keys
After executing the command, the keys will be created. You can check this by running the command:
ls ~/.ssh
You should see the files
id_rsa
andid_rsa.pub
. - Add the public key to
authorized_keys
Open the
authorized_keys
file:nano ~/.ssh/authorized_keys
Copy the contents of your public key using the command:
cat ~/.ssh/id_rsa.pub
Paste this key into the open
authorized_keys
file (to insert it in the Nano editor, right-click or use Shift +Insert).Make sure that your key is on a separate line.
- Save the changes
If you are using
nano
, press Ctrl+X, then Y, and then Enter to save the changes and exit the editor. - Set the correct permissions
Run the following commands to set the required access rights:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Connecting from a local machine
Now you are ready to connect to the server from the local machine using your new SSH key.
- Copy the private key to the local machine
You can use
SCP
to copy theid_rsa
file to your local machine. For example:scp username@server_address:~/.ssh/id_rsa /path/to/your/local/directory/
Replace user_name and server_address with your data.
- Set permissions for the private key on the local machine
chmod 600 /path/to/your/local/directory/id_rsa
- Connecting to the server
In the terminal of your local machine, run:
ssh -i /path/to/your/local/directory/id_rsa username@server_address
Now you can connect to your server using an SSH key.
That is, we enter the appropriate data into the terminal of our IDE (in this case, VSCode) and connect to an external server where our account is located. And then work with the terminal in a hosting/external server environment.
You can use the standard path to your SSH key ~/.ssh/id_rsa
from the local machine. Your command example might look like this:
ssh -i ~/.ssh/id_rsa username@server_address
At the same time, do not forget that the key file must have the correct access rights (usually it is chmod 600 ~/.ssh/id_rsa
). Also make sure that your SSH agent is running and your key is added if you want to avoid specifying -i
every time. To do this, you can use the command:
ssh-add ~/.ssh/id_rsa
The path ~/.ssh/id_rsa
on the local machine refers to the SSH key file, which is located in the .ssh
directory located in your home directory. Here's how to find this file:
Home directory:
This is the directory that your operating system provides for storing your personal files. This is usually /home/your username
on Linux and macOS, or C:\Users\ваше_имя_пользователя
on Windows (when using WSL or other shells).
.ssh directory:
Inside your home directory, you can find a hidden directory named .ssh
. To see it, you may need to enable the display of hidden files (usually you need to press Ctrl + H
in the file manager on Linux, or use the command ls -a
in the terminal).
id_rsa file:
This file is your private SSH key (its name can be anything, depending on how you name it). If you created SSH keys using the ssh-keygen
command, then by default they are located in this directory .ssh
Thus, you can quickly set up a connection to your server console via the local terminal of your IDE (or operating system console) using an SSH key to work with your project on an external server.