ssk-keygen + ssh-copy-id 无密码登陆远程LINUX主机_洪流之源的博客-程序员宅基地

技术标签: linux  

ssh-copy-id是ssh client套件內一个预设的指令,简单的来说他只是一个script,当你在本地主机已经产生了RSA /DSA和authentication,可以通过ssh-copy-id的指令将认证傳送到远端主机。

如何建立RSA /DSA和authentication?

$ ssh-keygen -t dsa 或者ssh-keygen -t rsa

Generating public/private dsa key pair.

Enter file in which to save the key (/root/.ssh/id_dsa) 

RSA与DSA的差别

ssh-keygen can create RSA keys for use by SSH protocol version 1 and RSA  or DSA keys for use by SSH protocol version 2

简单来说,若您只使用SSH protocol version 2,建议使用DSA來建立authentication。

    使用下例中ssk-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机。 

ssh-keygen 创建公钥和密钥。 
ssh-copy-id 把本地主机的公钥复制到远程主机的authorized_keys文件上。
ssh-copy-id 也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/authorized_keys设置合适的权限 。

步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥
ligh@local-host$ ssh-keygen -t  rsa
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key] 
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. 
The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 
ligh@local-host

步骤2: 用 ssh-copy-id 把公钥复制到远程主机上(ssh-copy-id -i 认证档案 用户名@主机
ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub  [email protected]
ligh@remote-host‘s password:
Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in: 
.ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting.
[注: ssh-copy-id 把密钥追加到远程主机的 .ssh/authorized_key 上.]

步骤3: 直接登录远程主机
ligh@local-host$ ssh remote-host 
Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 
[注: SSH 不会询问密码.] 

ligh@remote-host$ 

对22端口的修改问题:

基本上對外服務的主機,ssh listen port都會改掉預設的22 port,如此一來可以減少主機被入侵的機會,更改ssh listen port是最基本的第一道防線,請將設定檔內的 Port 22 進行更改,並且重新啟動ssh的服務。

      但是當您改掉ssh listen port之後,ssh-copy-id這個好用的指令將無法運用,這樣一來不是很可惜嗎?於是我們就可以對ssh-copy-id這個script進行一些修改,讓他可以支援指定不同的service port 。

      更改ssh-copy-id

      $ cp /usr/bin/ssh-copy-id /usr/bin/ssh-copy-id.orig
      $ vi /usr/bin/ssh-copy-id

      #!/bin/sh
       
      # Shell script to install your identity.pub on a remote machine
      # Takes the remote machine name as an argument.
      # Obviously, the remote machine must accept password authentication,
      # or one of the other keys in your ssh-agent, for this to work.
       
      ID_FILE="${HOME}/.ssh/identity.pub"
       
      while getopts ':i:p:P:h' OPTION
      do
          case $OPTION in
              i)
              if [ -n "$OPTARG" ]; then
                  if expr "$OPTARG" : ".*.pub" > /dev/null ; then
                      ID_FILE="$OPTARG"
                  else
                      ID_FILE="$OPTARG.pub"
                  fi
              fi
              ;;
              P|p)
                  PORT=$OPTARG;
              ;;
              h)
                  echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
                  exit 1
              ;;
          esac;
      done;
       
      shift $(($OPTIND - 1))
       
      if [ $# -lt 1 ] && [ x$SSH_AUTH_SOCK != x ] ; then
         GET_ID="$GET_ID ssh-add -L"
      fi
       
      if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
        GET_ID="cat ${ID_FILE}"
      fi
       
      if [ -z "`eval $GET_ID`" ]; then
        echo "$0: ERROR: No identities found" >&2
        exit 1
      fi
       
      if [ -z $PORT ]; then
          PORTOPTION=""
      else
          PORTOPTION="-p $PORT "
      fi;
       
      { eval "$GET_ID" ; } | ssh $PORTOPTION $1 "umask 077; test -d .ssh || 
      mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1
       
      cat <<EOF
      Now try logging into the machine, with "ssh $PORTOPTION'$1'", and check 
in:
       
        .ssh/authorized_keys
       
      to make sure we haven't added extra keys that you weren't expecting.
       
      EOF

      主要是增加了port的設定進去,建議可以直接複製貼上取代即可。
      以上script參考來源:http://blog.vieth.biz/2009/03/23/ssh-copy-id-with-port/


      ssh-copy-id with port的運用

      $ ssh-copy-id -i ~/.ssh/id_dsa.pub -p 1234 boon@tunnel.sdboon
      wawa@remotehost's password: (需要輸入一次密碼)
      Now try logging into the machine, with "ssh -p 22222 '[email protected]'", and check in:
       
        .ssh/authorized_keys
       
      to make sure we haven't added extra keys that you weren't expecting.

      ssh-copy-id with port的認證就完成囉!



版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weicao1990/article/details/42461511

智能推荐

Ruby版本升级问题解决_JustinNeil的博客-程序员宅基地

Redis集群环境中需要ruby环境,执行gem install redis时,提示如下错误:在centOS中yum源中对ruby的支持版本为2.0.0,可gem 安装redis需要最低是2.3.0,采用rvm来更新rubygpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39D...

JUC多线程及高并发5-----volatile_5:/^caulxxuavm^-程序员宅基地

面试题笔记5问题:谈谈你在那些地方用过volatile?分俩个方面:1.单例模式DCL代码:之前的单例模式:懒汉式,饿汉式。下面举例:一个线程访问:package jMM;public class SingletonDemo { //1,主类 private static SingletonDemo instance =null; //2,构造..._5:/^caulxxuavm^

今日头条面试相关_abcabsd的博客-程序员宅基地

原文地址: http://cv.qiaobutang.com/post/55e6a1110cf2633c1ba8ce8f互联网-今日头条2016年校园招聘面试题,笔试题,面试流程,面试经验-完结2015-09-02 朱迪-目录-Part1 今日头条2016最新校招信息Part2 想进今日头条?简历要这么写才行!Part3 今日头条历

centous7下zeppelin的安装配置_zeppelin配置中缺少依赖_Candriy的博客-程序员宅基地

Apache Zeppelin是一款基于web的notebook(类似于ipython的notebook),支持交互式地数据分析。原生就支持Spark、Scala、SQL 、shell, markdown等。开源对于 Zeppelin 而言,并不依赖 Hadoop 集群环境,我们可以部署到单独的节点上进行使用版本选择:zeppelin两个版本区别:zeppelin版本有两种分别是:..._zeppelin配置中缺少依赖

212 https请求的方法-程序员宅基地

/** * APIREQUEST 获取数据 * @param $url 请求地址 * @param array $params 请求的数据 * @return array|mixed */function APIREQUEST($url, $params = array()){ $curl = curl_init(); curl_setopt($curl, CURL...

在Eclipse中不显示.project和.classpath和.setting目录_敌军bug的博客-程序员宅基地

在Eclipse中,默认工程的视图如下:我们开发的过程中,为了方便于项目的管理,我们更想把其结构变成这样:但是如果不想要显示.project, .classpath, .gitignore文件和.setting文件夹方法:1.选中工程,选择Filters2.选中.resources勾选,点击OK.就不显示.settings文件夹, .classpath 文件, .p...

随便推点

修改Firefox的User-Agent_firefox useragent 海淘_漫步_的博客-程序员宅基地

火狐浏览器修改userAgent的办法一:在火狐浏览器地址栏输入“about:config”,按下回车进入设置菜单。找到“general.useragent.override”,如果没有这一项,则点右键“新建”->“字符串”,输入这个字符串。将其值设为自己想要的UserAgent(下面附有常见UA)火狐浏览器修改userAgent的办法二:user Agent Switch_firefox useragent 海淘

本地maven项目添加jar包_maven导入本地jar包-程序员宅基地

协作开发的时候遇到几次协作伙伴的jar包拉不下来或者没有的情况,特此记录下从网上学习到的解决方法,以备下次回忆: 硬件环境:macOS Big Sur 11.0.1 开发工具:IntelliJ IDEA 2019.02 解决方法:打开本地的 终端窗口,输入复制粘贴下面命令(相关jar包和路径需要按个人实际情况填写)。 mvn install:install-file -DgroupId=com.security.plat -DartifactId=..._maven导入本地jar包

鼠标滚轮事件onGenericMotionEvent(MotionEvent event)_网络卷毛的博客-程序员宅基地

1.在View或者Activity重写onGernericMotionEvent方法,能监听鼠标或者其它外接设备的事件,例如 @Override public boolean onGenericMotionEvent(MotionEvent event) { if ((event.getSource() &amp; InputDevice.SOURCE_CLA..._ongenericmotionevent

使用gSOAP与WebService - 第一部分 为VC++从WSDL读取信息_Fyter的博客-程序员宅基地

CurrencyConvertor - How use gSOAP and WebServices - Part 1 Get ready with VC++ 6 from WSDL file Download Demo - 42.1 KB Download Source Files - 219.6 KB IntroductionIn this articl

linux 程序无法运行_lwlgzy的博客-程序员宅基地

提示:error while loading shared libraries: libXXXXX.so.1: cannot open shared object file: No such file or directory相应的库已经存在情况库路径:“/usr/local/mysql/lib” export LD_LIBRARY_PATH=/usr/local/mysql/lib:$...

【数学建模】day14-建立GM(1,1)预测评估模型应用_weixin_30568715的博客-程序员宅基地

学习建立GM(1,1)灰色预测评估模型,解决实际问题:SARS疫情对某些经济指标的影响问题一、问题的提出  2003 年的 SARS 疫情对中国部分行业的经济发展产生了一定影响,特别是对部分 疫情较严重的省市的相关行业所造成的影响是显著的,经济影响主要分为直接经济影响 和间接影响。直接经济影响涉及商品零售业、旅游业、综合服务等行业。很多方面难以 进行定量的评估,现仅就 SARS 疫情较..._某地接待海外旅游人数matlab

推荐文章

热门文章

相关标签