Best way to manage ssh hosts and private keys

不言
Release: 2019-03-15 10:00:12
Original
3546 people have browsed it

As a system administrator, handle multiple remote systems on a regular basis. Need to use ssh system many times during work. Many remote Linux servers are accessed via passwords, many via private keys. So that's more typical for managing all of this stuff. This article will introduce you to the details about using key files to correctly organize the ssh server.

Best way to manage ssh hosts and private keys

Configuration file syntax:

We can add multiple ssh host details to the ~/.ssh/config file . Edit the configuration file in your favorite editor such as vi, vim or nano.

$ vi~/.ssh/config
Copy after login

The syntax is as follows:

Host<NICK_NAME> 
     HostName<IP地址远程> 
     IdentityFile <PATH TO私有文件> 
     User<LOGIN AS USERNAME> 
     Port<SSH要使用的端口> 
     LocalForward <本地端口> <REMOTE_LOCATION:PORT>
Copy after login

1. Add the first SSH host

For example, our first ssh host is running A php development web server with details name php-web1, user root, port 22, and accessible via password. Add the following content in the configuration file.

Host php-web1
     HostName 192.168.1.100
     User root
Copy after login

Now try using SSH as the following command.

$ ssh php-web1
Copy after login

2. Add a second SSH host

Our second host server (php-web2) can use ssh with user root on the default port 22 Key pair access. Add the following content in the configuration file.

Host php-web2
     HostName  192.168.1.101
     IdentityFile ~/.ssh/php-web2.pem
     User root
Copy after login

Now try using SSH as the following command.

$ ssh php-web2
Copy after login

3. Add a third SSH host

Our third ssh host server (php-db1) is running on port 2222 and can be accessed through user ubuntu key pair access. Add the following content in the configuration file.

Host php-db1
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     User ubuntu
Copy after login

Now try using SSH as the following command.

$ ssh php-db1
Copy after login

4. Set up forwarding using SSH

In this setup we need to forward the local system port 3306 to the remote server (php-db1) on port 3306 ) host. Add the following content in the configuration file.

Host php-db1-mysql-tunnel
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     LocalForward 3306 127.0.0.1:3306
Copy after login

Now try using SSH as the following command.

$ ssh php-db1-mysql-tunnel
Copy after login

Final configuration file

The final configuration file ~/.ssh/config is as follows.

Host php-web1
     HostName 192.168.1.100
     User root

Host php-web2
     HostName  192.168.1.101
     IdentityFile ~/.ssh/php-web2.pem
     User root

Host php-db1
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     User ubuntu

Host php-db1-mysql-tunnel
     HostName 192.168.1.110
     Port 2222
     IdentityFile ~/.ssh/php-db1.pem
     LocalForward 3306 127.0.0.1:3306
Copy after login

The above is the detailed content of Best way to manage ssh hosts and private keys. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
ssh
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!