Configure ssh remote connection server and set up key (password-free) login and alias login

$ ssh -L post:localhost:host_port username@server_ip -p ssh_port

It is a standard SSH connection command, where post is the local port, host_post is the server port, username is the server username, server_ip is the server ip address, and ssh_port is the server SSH port. Each time you connect to the server, you need to enter a password and cumbersome SSH connection commands. , for convenience, you can configure SSH password-free login and alias login locally. The specific operations are as follows:

First generate an SSH key pair locally. If you already have a key pair, you can skip this step.

$ ssh-keygen -t rsa -C "[email protected]"

Enter the above command in the terminal, and the private key id_rsa and public key id_rsa.pub will be generated in the ~/.ssh/ directory (winsdows should be C:\Users\user\.ssh). Copy the contents of the public key id_rsa.pub to In the server ~/.ssh/authorized_keys file, it should start with ssh-rsa and end with [email protected]. If there is no such authorized_keys file, create a new one, and then add the following content to the local ~/.ssh/config file. If there is no such file The config file is newly created:

Host server_alias
    HostName server_ip
    LocalForward post localhost:host_port # 可以不写这一行
    Port ssh_port
    User username
    IdentityFile ~/.ssh/id_rsa

Among them, server_alias is the server alias, and IdentityFile configures the local private key path (you can not write it, the default is ~/.ssh/id_rsa), which is used to configure multiple servers. After the configuration is completed, you can enter it directly in the terminal.

$ ssh server_alias

Connect to the server. If the configuration is successful, you will enter the server directly without entering a password and cumbersome SSH connection commands.

Guess you like

Origin blog.csdn.net/Hello_World1023/article/details/132634986