How SSH connects to the cloud server via public key

Source: Internet
Author: User

Guide Usually we connect remotely from a remote server (Linux) Windows by using tools such as putty or Xshell. Linux can be connected directly via SSH commands. In fact, both are consistent and are transmitted through the SSH protocol.

If our Windows does not have tools such as putty, but with Git-bash, you can also connect directly via SSH. Use the following command:

SSH [email protected]/hostname/Domain name/

Next you will be asked to enter the password, successfully entered after the connection is successful. But in this way, each time you enter a polygon code, we can write a shell script similar to the following: conn_vps.sh files

First, open Git-bash (right-click on the desktop and choose Git bash here. $ for gitbash command prompt, # for comments, non-commands do not have to be entered. Same below

$ cd ~ # into the user's home directory $ mkdir SH # Create a new SH directory for storing shell scripts. $ cd sh # above are my personal habits. Don't have to be like me.

Then create the new file

$ vim conn_vps.sh

Enter VIM mode, press I to edit, enter the following:

#!/bin/bash # This line must be written, non-commented SSH [email protected]/hostname/Domain # Enter your own remote host IP, etc.

Press the ESC key at this point, and then press: Wq to save the exit.

You can now run the script:

$./conn_vps.sh$ SH conn_vps.sh # These two commands, any one can be run

Of course, if you are in the Linux environment itself, you need to give execute permissions, like this. Just like me. Created in Git-bash, do not need this step, itself has execute permission.

$ chmod 755 conn_vps.sh

Then execute, and then you will find that the password will still be entered, which is of course, we just made a simple script of the command just now.

Next SSH key is distributed.

See if there is a key

$ ll ~/.shh/# see if there are Id.rsa, and id.rsa.pub two files.

If not, you can generate a. Some words skip this step

$ ssh-keygen-t rsa-c "[email protected]" #这部其实可以不加邮箱参数, but git configures GitHub connection as needed.

#所以最好一次性做了, use the same key. e-mail to your own mailbox.

Enter for three consecutive times, if you do not set a password.

Next, upload the public key to the remote server

$ ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected] ip/host name/domain name of the remote server

Then run conn_vps.sh. The first time you still need to enter the password again, if the following prompt appears to be successful.

Now try logging into the machine, with: "SSH" [email protected] Your IP ' "and check to do sure that is only the key (s) you wante D were added.

This is successful, you can connect to your server by running conn_vps.sh.

How you want multiple cloud servers. The root password can be complicated and then connected to the cloud server in this way. yeah! is done! Well, since we can log in, let's take a look at the remote machine (Linux).

Enter LL. ssh/

[email protected] ~]# LL. ssh/total 4-rw-------1 root root 401 Sep 20:47 Authorized_keys

If you do not generate Ssh-keygen on the remote machine, there will be only one file, Authorized_keys translation is the authentication key. And this authenticated key is the ~/.ssh/id_rsa.pub of the previous command.

So this command ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected] The ip/hostname/domain name of the remote server is to paste the contents of id_rsa.pub into Authorized_keys. If another client also wants to connect to this remote server. You can use the same command again, or you can view the id_rsa.pub directly with cat, and then open the Authorized_keys file with vim on the server. Paste in, the previous do not delete, another line can be.

Of course, if you do not want to use Ssh-copy-id-i command, then you have to remember authorized_keys this word ~

How SSH connects to the cloud server via public key

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.