Termux's ssh usage method (connecting to cloud server, ssh key login and Termux interconnection)

write first

 As a powerful terminal simulation software, Termux can easily connect to the cloud server through ssh. Here we take the connection to the Alibaba Cloud server as an example.

1. Connect to the server

 Generally, the server purchased from the server operator has the remote connection service enabled. First install openssh on ternux.

pkg install openssh

 After the installation is complete, find the ip address of the server to be connected on the server console, take 47.115.217.154 as an example, and type in termux

ssh [email protected]

 Among them, root is the user name. If you log in with another user name, you should change rootthe part in the command, @followed by the server ip address, which can be found from the server console. Then follow the prompts to enter the password to connect successfully.

insert image description here

2. Add ssh password-free login for server and termux

 Every time you connect to the server, you need to enter a password. We can add a public key to the server to achieve password-free login.

 first type

ssh-keygen -t rsa

 Press Enter when encountering any option. For detailed explanations of the reasons, please refer to the link in the previous blog . The generated key file will be saved in the .ssh folder under the home directory of termux (this folder cannot be opened to the group group. Permissions), use the cd command to enter the folder

/data/data/com.termux/files/home/.ssh

 Then use vi or vim to open id_rsa.pub(or use cp to id_rsa.pubcopy the file to another readable folder and open it in txt format), and copy all the contents inside.

Log in to the server, enter the user directory (if you are root, enter /root), then cd into the .ssh directory, find the authorized_keys file  in it after ls, open it with vim, then paste all the copied content in, save and exit (do not know how to use For vim, you can Baidu the basic use of vim by yourself).

 Using ssh [email protected]the login server again does not require a password.

insert image description here

3. Use Termux to connect to Termux on another phone

 First of all, both devices need to be in the same local area network (or the connected device can be accessed).

 Type the following command on the connected device to view the IP of the device in the current LAN:

ifconfig

 After finding the ip of the current LAN, switch to the connected device to operate, and use the following command to open the ssh connection of Termux. Among them -p 1234, the port for ssh connection is specified. This can be selected according to the needs. If you do not add and change the parameter, port 8022 will be used by default:

sshd -p 1234

 Then use the following command to create a password for the connected device. Enter the password twice as prompted:

passwd

 After that, on another device use:

ssh -p 端口(默认8022) user@ip地址

 Enter the password according to the prompt to connect.

 Similarly, we can set the ssh public key on the connected device to realize secret-free connection.

 Use vim to open and edit the file located on the connected device ~/.ssh/authorized_keys, and paste the previously obtained id_rsa.pubcontent into it and save it.

 The above operations can also enable other devices to connect to the Termux terminal on the mobile phone.

Simple configuration file for SSH

 If there is no zsh or other command-line plug-ins, it is really troublesome to copy and paste or find the ip every time, and find the password. We can achieve quick login by configuring the ssh file.

 First, create a new file named as under the user's .sshfolder config. Note that no extension is required . The .ssh folder of Termux is generally in ~/.ssh.

The configuration in the config file can be written like this:

Host 任何你想要的缩写类型,如ut,rt
Hostname 指定连接的ip地址
user 指定连接的用户名
port 指定连接的端口

 For example like this:

Host ut
Hostname 192.168.1.1
user ubuntu
port 66

 After that, you can log in through the abbreviated shortcut ssh (it is better to use with ssh key):

ssh ut

Guess you like

Origin blog.csdn.net/m0_74075298/article/details/127224984