Introduction of-SSH Principle of Linux operating system and application of free-secret and remote command operation

Source: Internet
Author: User
Tags free ssh



PS: The use of SSH, to manage a large number of servers can bring great convenience. When we access another server from one server, we can enter password access without compromising efficiency. But when we need to access multiple servers, or hundreds of dozens of, it's a bit of a hassle. Or do some batch upgrades on hundreds of dozens of of servers, modify the configuration, and so on, if you need to check the password-by-step or log-in operation will be very time-consuming. You can consider ssh-free SSH remote execution commands to target host group process operations.






SSH principle and application



First, what is SSH



Simply put, SSH is a network protocol that is used to encrypt logins between computers.



If a user logs on to another remote computer using the SSH protocol from the local computer, we can assume that the login is secure, and that the password will not be compromised even if intercepted in the middle.



Early, the Internet communication is clear communication, once intercepted, the content is undoubtedly exposed. In 1995, Finnish scholar Tatu Ylonen designed the SSH protocol to encrypt all login information and become a basic solution for Internet security, which has become a standard configuration for Linux systems.



It should be noted that SSH is only a protocol, there are many implementations, both commercial and open source implementation. The implementation of this article is OpenSSH, it is free software, the application is very extensive.



In addition, this article only discusses the use of SSH in a Linux shell.



Second, the most basic usage



SSH is primarily used for remote logins. Suppose you want to log on to the remote host with the username user, as long as a simple command is available.



$ssh [email protected]



If the local user name matches the remote user name, the user name can be omitted at logon.



$SSH Host



The default port for SSH is 22, which means that your login request is sent to port 22 on the remote host. Using the P parameter, you can modify this port.



[Email protected]



The above command indicates that SSH connects directly to port 2222 of the remote host.



Third, man-in-the-middle attack



SSH guarantees security because it uses public-key cryptography.



The whole process is this:



(1) The remote host receives the user's login request and sends its own public key to the user.



(2) The user uses this public key to encrypt the login password and send it back.



(3) The remote host with its own private key, decrypt the login password, if the password is correct, consent to user login.



The process itself is secure, but when implemented, there is a risk: if someone intercepts a login request and then pretends to be a remote host, the fake public key is sent to the user, making it difficult for the user to discern the authenticity. Because unlike the HTTPS protocol, the public key of the SSH protocol is not notarized by the Certificate Authority (CA), that is, it is issued by itself.



It can be assumed that if an attacker is plugged in between a user and a remote host (for example, in a public WiFi zone), a forged public key is used to obtain the user's login password. Then use this password to log on to the remote host, then SSH security mechanism is gone. This risk is known as the "man-in-the-middle attack" (Man-in-the-middle attack).



How is the SSH protocol coping?



Fourth, password login



If you are logged in to the other host for the first time, the following prompt will appear:






$ssh [email protected]



The authenticity of host ' host (10.1.1.1) ' Can ' tbe established.



RSA key fingerprint is98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.



Is you sure want tocontinueconnecting (yes/no)?






This passage means that you cannot confirm the authenticity of the host computer, only know its public key fingerprint, ask you still want to continue to connect?



A natural question is, how do users know what the remote host's public key fingerprint should be?



The answer is no good way, the remote host must be posted on their own web site public key fingerprint, so that users self-check.



Assuming that the risk is measured, the user decides to accept the public key of the remote host.






Is you sure want tocontinueconnecting (yes/no)? Yes






The system will appear with a hint that the host hosts have been recognized.






warning:permanently added ' host,10.1.1.1 ' (RSA) tothe list of known hosts.






You will then be asked to enter a password.






Password: (Enter Password)






If the password is correct, you can log in.



When the public key of the remote host is accepted, it is saved in the file $home/.ssh/known_hosts. The next time you connect to this host, the system will recognize that its public key has been saved locally, thus skipping the warning section and prompting for a password directly.



Each SSH user has its own known_hosts file, and the system also has a file like this, usually/etc/ssh/ssh_known_hosts, that holds the public key for a remote host that can be trusted by all users.



Fifth, public key login



Log in with a password, you must enter the password every time, very troublesome. Fortunately, SSH also provides a public key login, which eliminates the steps to enter a password.



The so-called "public key Login" principle is simple, that is, the user stores their own public key on the remote host.



When logged in, the remote host sends a random string to the user, which is encrypted with his or her private key and then sent back. The remote host decrypts with a pre-stored public key and, if successful, proves that the user is trustworthy, allowing the login shell to be logged in and no longer requiring a password.



This approach requires the user to provide their own public key. If there is no ready-made, you can generate one directly with Ssh-keygen:



$ssh-keygen



After you run the above command, a series of prompts will appear and you can return all the way.



One of the questions is whether you want to set a password on the private key (passphrase), and if you are concerned about the security of the private key, you can set one here.



At the end of the run, the $home/.ssh/directory will be reborn into two files: Id_rsa.pub and Id_rsa. The former is your public key, and the latter is your private key.



Then enter the following command to transfer the public key to the remote host hosts:



$SSH-copy-id [email protected]



OK, then you log in again, you do not need to enter the password.



If not, open the remote host's/etc/ssh/sshd_config this file, check the following lines before the "#" comment is removed.





Rsaauthentication Yes



Pubkeyauthentication Yes



Authorizedkeysfile.ssh/authorized_keys



Then, restart the remote host's SSH service.



Vi.. authorized_keys file



The remote host stores the user's public key in the $home/.ssh/authorized_keys file of the user's home directory after logging in. The public key is a string, just append it to the end of the Authorized_keys file.



Instead of using the Ssh-copy-id command above, use the following command to explain the saving process for the public key:



$ssh [email protected] ' mkdir-p. SSH &&>. Ssh/authorized_keys ' <~/.ssh/id_rsa.pub



This command consists of multiple statements, broken down in turn, to see:



(1) "$ ssh [email protected]" means login to the remote host;



(2) mkdir. SSH &&>. Ssh/authorized_keys in single quotes, indicating the command executed on the remote shell after login:



(3) The role of "$ mkdir-p. SSH" is to create one if the. ssh directory in the home directory does not exist;



(4) The role of ' cat >>. Ssh/authorized_keys ' < ~/.ssh/id_rsa.pub is to redirect the local public key file ~/.ssh/id_rsa.pub to the remote file Authorized_ The end of the keys.



After writing the Authorized_keys file, the settings for the public key login are complete.



Sixth. Remote Command





After you have configured SSH encryption, the remote login server will not enter the password again, which will bring great convenience to the operation server.



Using SSH locally [email protected] ' ls/local ' will execute the ls/local command on the remote machine, and if you want to execute multiple commands on a remote machine consecutively, you can enclose the commands in single or double quotes.



For example: SSH [email protected] ' cd/local; Pwd;ls '



If you want to start a command on a remote machine locally and return it, you can ssh [email protected] "/local/x.sh 1>/dev/null 2>&1 &"



Note If there is no "", such as ssh [email protected] cd/local; LS will only execute the cd/local command, the LS command is executed locally, with double quotes or single quotes, the enclosed command is treated as an argument to the SSH command, so it is executed remotely.



The difference between single and double quotation marks in SSH commands: To illustrate the problem with an example, assume that the Java environment variable is configured on the local machine, perform the echo $JAVA _home=/opt/jdk locally, and if I want to see the Java environment variable on the remote machine, I can only use single quotes. SSH [email protected] ' echo $JAVA ', then the $java will not be parsed by the shell, but as a string, when the parameters echo $JAVA passed to SSH;



If we use SSH [email protected] "echo $JAVA", then the shell will first parse $java to get its value, then the command becomes SSH [email protected] ' echo/opt/jdk '.












This article is from the "one Gourd soy sauce" blog, please be sure to keep this source http://zhushenghang.blog.51cto.com/11105203/1825699



Introduction of-SSH Principle of Linux operating system and application of free-secret and remote command operation


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.