And so, we generated ssh keys and it's time to connect to the server. The first thing to do is to forward the public keys to the server.
ssh-copy-id -i ~/.ssh/id_rsa.pub user@serverInstead of user, you substitute the login of your user, and instead of server, you indicate the host or ip address of the remote server to which you want to connect. If your ssh does not work on the standard port, then you need to additionally specify the port.
ssh-copy-id -i ~/.ssh/id_rsa.pub '-p port user@server'Where port is the port your ssh server is listening on
The second thing to do before connecting is to check that the ssh agent knows about your keys. To do this, you can use the following command:
ssh-add -l 2048 SHA256:FrcsHSW/ATlQzIhOGdU8oT/CqNJGeWZWgWYY0XA0yag igor-sverdlov@mail.ru (RSA)If the required private ssh key is not available or you transferred the keys to another server, then you can add the key to the agent by running this command:
ssh-add ~/.ssh/id_rsa Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)In this case, you will be asked to enter the password for the key. Also, when debugging, you need to understand what fingerprint a given ssh key has. To do this, you can use the following command:
ssh-keygen -lf ~/.ssh/id_rsa.pub 2048 SHA256:FrcsHSW/ATlQzIhOGdU8oT/CqNJGeWZWgWYY0XA0yag igor-sverdlov@mail.ru (RSA)
In most cases, it is convenient to upload ssh keys automatically to the server. To do this, you need to create a configuration file on the client.
touch ~/.ssh/config printf "Compression yes\nForwardAgent yes">> ~/.ssh/configTo see all the available config options, you can run the command:
man ssh_config
Finally, we are ready to execute the ssh connect command. It looks like this:
ssh user@serverIf the port of the ssh connection differs from the standard one, then it can be set using the option -p.
ssh -p port user@serverTo forward the graphical interface, you can use the option -X.
ssh -X user@serverIt is often convenient to configure aliases through the config. The content of the config ~/.ssh/config looks like this:
Host dev Hostname server User user ForwardX11 yes Compression yes ForwardAgent yesNow you can connect to the server server under the user user using the following command
ssh devYou can run a command on a remote server like this:
ssh user@server command Thu 06 Jan 2022 03:11:12 PM UTCIn this example, the date command was used
ssh -f -N -L local_port:endpoint_ip:endpoint_port user@server -p ssh_port # example ssh -f -N -L 3031:127.0.0.1:8080 217.14.247.114 -p 522Where: