嚴重問題:
若移植失敗將可能直接導致手機***無法開機***,導入相關文件需慎重!
達成效果:
1. ssh 遠程登錄 Android 終端;
2. sftp 掛載/映射 Android 根文件系統至本地網絡驅動盤符;
適用場景:
前期開發過程中,需要做大量的調試,需要對Android文件系統頻繁修改;
操作流程:
詳細操作:
一、Linux操作系統相關的部分
1. 編譯 openssh for Android
同步及編譯Android源碼的基礎操作:[編譯Android源碼入門]
在一切准備就緒之后,輸入以下命令:
source build/envsetup.sh lunch cm_mako-userdebug
mmm -B external/zlib
mmm -B external/openssl mmm -B external/openssh
將會生成以下的文件:
1.zlib
----------------------------------------
/media/Source/cm11/out/target/product/mako/system/lib/libz.so
/media/Source/cm11/out/target/product/mako/system/bin/gzip
2.openssl
----------------------------------------
out/target/product/mako/system/lib/libcrypto.so
out/target/product/mako/system/lib/libssl.so
out/target/product/mako/system/bin/ssltest
out/target/product/mako/system/bin/openssl
3.openssh
----------------------------------------
/media/Source/cm11/out/target/product/mako/system/lib/libssh.so
/media/Source/cm11/out/target/product/mako/system/bin/ssh
/media/Source/cm11/out/target/product/mako/system/bin/sftp
/media/Source/cm11/out/target/product/mako/system/bin/scp
/media/Source/cm11/out/target/product/mako/system/bin/sshd
/media/Source/cm11/out/target/product/mako/system/bin/sftp-server
/media/Source/cm11/out/target/product/mako/system/bin/ssh-keygen
/media/Source/cm11/out/target/product/mako/system/etc/ssh/sshd_config
/media/Source/cm11/out/target/product/mako/system/bin/start-ssh
把這些文件,拷貝至對應的位置即可,備份下載:http://pan.baidu.com/s/1o69DTV0
解壓:tar zxf openssh_android_binary.tgz -C /
2. 配置Adnroid操作系統中的 sshd_config 文件
adb shell 登錄 Android 操作系統后,vi /data/ssh/sshd_config,復制以下代碼:
# Package generated configuration file # See the sshd_config(5) manpage for details # What ports, IPs and protocols we listen for Port 22 # Use these options to restrict which interfaces/protocols sshd will bind to #ListenAddress :: #ListenAddress 0.0.0.0 Protocol 2 # HostKeys for protocol version 2 # custom configure start # chnange the hostkey default path, by scue HostKey /data/ssh/ssh_host_rsa_key HostKey /data/ssh/ssh_host_dsa_key HostKey /data/ssh/ssh_host_ecdsa_key # custom configre end #Privilege Separation is turned on for security UsePrivilegeSeparation sandbox # Lifetime and size of ephemeral version 1 server key KeyRegenerationInterval 3600 ServerKeyBits 768 # Logging SyslogFacility AUTH LogLevel INFO # Authentication: LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile /data/.ssh/authorized_keys # Don't read the user's ~/.rhosts and ~/.shosts files IgnoreRhosts yes # For this to work you will also need host keys in /etc/ssh_known_hosts RhostsRSAAuthentication no # similar for protocol version 2 HostbasedAuthentication no # Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication #IgnoreUserKnownHosts yes # To enable empty passwords, change to yes (NOT RECOMMENDED) PermitEmptyPasswords yes # Change to yes to enable challenge-response passwords (beware issues with # some PAM modules and threads) ChallengeResponseAuthentication no # Change to no to disable tunnelled clear text passwords #PasswordAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosGetAFSToken no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes # GSSAPI options #GSSAPIAuthentication no #GSSAPICleanupCredentials yes X11Forwarding yes X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes #UseLogin no #MaxStartups 10:30:60 #Banner /etc/issue.net # Allow client to pass locale environment variables AcceptEnv LANG LC_* # 這里指向 sftp 可執行文件的路徑! Subsystem sftp /system/bin/sftp-server # Set this to 'yes' to enable PAM authentication, account processing, # and session processing. If this is enabled, PAM authentication will # be allowed through the ChallengeResponseAuthentication and # PasswordAuthentication. Depending on your PAM configuration, # PAM authentication via ChallengeResponseAuthentication may bypass # the setting of "PermitRootLogin without-password". # If you just want the PAM account and session checks to run without # PAM authentication, then enable this but set PasswordAuthentication # and ChallengeResponseAuthentication to 'no'. #UsePAM yes # root login without passwd, by scue. # custom configure start PermitRootLogin without-password RSAAuthentication yes PubkeyAuthentication yes PermitEmptyPasswords yes # custom configure end
這個配置可以在 start-ssh & 執行之后,啟動sshd的同時也啟動了 sftp-server ,同時使root帳戶無密碼可登錄(但需要RSA驗證),同時還要執行以下命令行操作:
mount -o remount,rw /system mv /system/etc/ssh/sshd_config{,.bak} ln -s /data/ssh/sshd_config /system/etc/ssh/sshd_config
雖說Android代碼中 sshd 執行時自動查找的是 /data/ssh/sshd_config,但是還是創建一個鏈接至 /system/etc/ssh/sshd_config的好,說不准哪天情況有變~
3. Android終端中生成 ssh_host_* 等等這些雜項文件:
在終端中輸入以下命令:
cd /data/ssh/ ssh-keygen -t rsa -f ssh_host_rsa_key -N "" ssh-keygen -t dsa -f ssh_host_dsa_key -N "" ssh-keygen -t ecdsa -f ssh_host_ecdsa_key -N ""
4. 上傳本地id_rsa.pub文件 >> 至 Android 手機中:
從sshd_config中的 AuthorizedKeysFile /data/.ssh/authorized_keys,這一行來看,很明了地知道,這個RSA公鑰應當保存至/data/.ssh/authorized_keys文件中(這個RSA公鑰在Linux中可以通過命令行 ssh-keygen -t rsa -C "your_email" 來生成,win上單獨講解)。
在Linux終端中輸入以下命令:
adb root adb push ~/.ssh/id_rsa.pub /sdcard/id_rsa.pub adb shell mkdir -p /data/.ssh cat /sdcard/id_rsa.pub >> /data/.ssh/authorized_keys chmod 0600 /data/.ssh/authorized_keys
這樣子,就可以在Linux終端中,輸入 ssh root@ip_addr 來登錄Android手機設備進行操作了,同時 sftp root@ip_addr 對文件操作也是可以的,Enjoy!
二、Windows操作系統相關的部分
大多數人,還是在Windows中操作,為了方便對Android中的設備文件進行操作,可通過”映射網絡驅動器“的方式,掛載Android根目錄至一個”盤符“,以root權限進行讀寫操作都是可以的。
主要的工具是:stfpdrive,下載鏈接:http://pan.baidu.com/s/1c048BTm,具體操作看圖示:
最后,點擊"connect"即可鏈接至手機(注:ip地址請根據設備的自身實際情況進行修改),最終效果圖如下: