android安装openssh,通过其他电脑ssh登陆到安卓手机

原链接:

https://glow.li/technology/2015/11/06/run-an-ssh-server-on-your-android-with-termux/

With the brilliant Termux terminal emulator app you can run an SSH server on your Android.

Perviously I used SSHDroid to achieve this, but with Termux is much nicer because you have access to a working package manager.

Run the service

You neet to install the OpenSSH package

    apt install openssh

and use following command to start the ssh server.

    sshd

And there you go. Your ssh service is now running on port 8022.

    ssh localhost -p 8022

Adding your Public key

You can't do password authentication in Termux, therefore you need to put your OpenSSH public key into the ~/.ssh/authorized_keys file.

This file will need to be created and permissions set to 600.

    touch ~/.ssh/authorized_keys
    # Set Permissions to the file
    chmod 600 ~/.ssh/authorized_keys
    # Make sure the folder .ssh folder has the correct permissions
    chmod 700 ~/.ssh

If you do not have a OpenSSH key pair yet, you can generate one with the following command:

    ssh-keygen

You may or may not enter a passphrase and if you don't specify otherwise, your key pair will have been saved under ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub. You can then add it to the ~/.ssh/authorized_keys with

    cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys

Then you can test it by connecting to your ssh service

    # -i $PATH_TO_FILE/filename is only required if the id_rsa file is not ~/.ssh/id_rsa
    ssh localhost -p 8022 -i %PATH_TO_KEY-FILE%/%NAME_OF_KEY%

You can now use your private key (~/.ssh/id_rsa) to login to your Termux SSH Server. Simply copy it to your computer (by copying it to internal storage first [cp ~/.ssh/id_rsa /sdcard]) and use it in your ssh client.

OpenSSH

If you're using OpenSSH (on Linux or Cygwin) you can use it directly:

    # -i $PATH_TO_FILE/filename is only required if the id_rsa file is not ~/.ssh/id_rsa
    ssh $IP -p 8022 -i %PATH_TO_KEY-FILE%/%NAME_OF_KEY%

PuTTY

If you're using PuTTY you will need to convert it to the PuTTY Private Key format first.

  1. Download and run PuTTYgen
  2. Load the private key (id_rsa)
  3. Save the private key as a *.ppk file.
  4. Download and run PuTTY
  5. Enter the IP address of your Android device and use port 8022
  6. Under Connection>SSH>Auth you can browse for the *.pkk file
  7. Click open
  8. You can leave "login as:" blank

You should now be connected to your Android device via SSH.

If it still doesn't work

    killall sshd
    sshd -d

If it is still prompts you for a password you can enter sshd's debug mode with the above command and see exactly why your key has been rejected. The reason usually are bad permission on either your home directory, your .ssh folder or your authorized_keys file.

The correct permissions are:

    chmod 700 ~
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/*

I hope in the future Termux will allow us to register sshd as a proper service which would automatically start on system boot. Right now I have the 'sshd' command in my .bashrc file and I am using Tasker to launch Termux after boot. You can also use the Termux widget to quickly start sshd with a widget.

登陆命令:

ssh -i ssh_host_rsa_key.null  -p 8022 192.168.43.1

 

posted on 2018-05-24 17:40  乌龙院院长 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wlyyz/p/9084111.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值