Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java connect to the Linux server?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Java how to connect to the Linux server", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how Java connects to the Linux server" this article.

Pom files add dependencies

Ch.ethz.ganymed ganymed-ssh3 build210

Server login public key setting

The steps are as follows:

1. Generate private key and public key file on this machine

The machine goes to the .ssh directory, c:/Users/zhufengyan/.ssh, and executes under that directory

$ssh-keygen-t rsa

Then you can go all the way back to the car.

Note: if there are two id_rsa id_rsa.pub files in the original. / ssh directory, the above enter operation will generate a new id_rsa id_rsa.pub to overwrite the original key file.

2. Put the locally generated public key information to the server

Place the locally generated public key information on the server you need to access, as follows:

Obtain public key information locally, copy information

Log in to the server with Xshell. You have root permission to join.

Cd / root/.ssh

Vi authorized_keys

Paste the above copy into the end of the file. Ok complete.

Description: assuming that you do not have an authorized_keys file on your server, then create one by yourself, paste the public key information into this file after creation, save it, and require authorization, chmod 600authorized_keys.

In addition, the effective ssh public key must meet at least the following two conditions: 1. The permission of the ssh directory must be 700. The permissions of the ssh / authorized_keys file must be 600.

Connect to the server

The steps are as follows

1. Put the private key file in the resources directory of the project

The access path in the code is

Private String privateKeypath = "src/main/resources/sshkey/id_rsa"

2. Code to connect to the server

/ * establish a connection to the server * @ param host server IP String host = "192.168.178.1"; * @ param port port int port = 22 (by default, it can be used directly) * @ param userName login server user name (work) * @ param password login server password (empty) * @ param privateKeyFile private key file String pubkeypath = "src/main/resources/sshkey/id_rsa" corresponding to the server public key * @ return returns the login connection. Remember to close the connect resource * @ throws IOException * / public static Connection getSSHConnection (String host, int port, String userName, String password, String privateKeyFile) throws IOException {Connection connection = new Connection (host, port); connection.connect (); File file = new File (privateKeyFile); boolean b = connection.authenticateWithPublicKey (userName, file, password); if (b) {return connection } else {System.out.println ("login connection failed, please check user name, password, private key file"); return null;}}

These are all the contents of the article "how to connect to the Linux server with Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report