puppet ssh模块

简介: 转载:http://blog.51cto.com/ywzhou/1577502作用:通过SSH模块管理客户端的ssh远程服务,并用key认证方式替代密码认证方式,提高安全性;本例分两阶段,首先是ssh的安装、配置及服务管理,然后是使用如何转换成key认证方式。

转载:http://blog.51cto.com/ywzhou/1577502

作用:通过SSH模块管理客户端的ssh远程服务,并用key认证方式替代密码认证方式,提高安全性;

本例分两阶段,首先是ssh的安装、配置及服务管理,然后是使用如何转换成key认证方式。

第一阶段

1、服务端配置ssh模块

(1)模块清单

[root@puppet ~]# tree /etc/puppet/modules/ssh/
/etc/puppet/modules/yum/
├── files
│   └── sshd_config
├── manifests
│   ├── config.pp
│   ├── init.pp
│   ├── install.pp
│   ├── params.pp
│   ├── key.pp
│   └── service.pp
└── templates

(2)定义参数类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/params.pp
class ssh::params {
  case $operatingsystem {    #条件判断,根据操作系统来定义同名参数不同的值
    Solaris: {
      $ssh_package_name = 'openssh'
      $ssh_service_config = '/etc/ssh/sshd_config'
      $ssh_service_name = 'sshd'
    }
    /(RedHat|CentOS|Fedora|Ubuntu|Debian)/: {
      $ssh_package_name = 'openssh-server'
      $ssh_service_config = '/etc/ssh/sshd_config'
      $ssh_service_name = 'sshd'
    }
    default: { 
      fail("Module ssh is not supported on ${::operatingsystem}")
    }
  }
}

(3)定义安装类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/install.pp
class ssh::install {
  package { ssh::params::ssh_package_name:
    ensure => installed,
  }
}

(4)定义配置类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/config.pp
class ssh::config {
  file { ssh::params::ssh_service_config:
    ensure  => present,
    owner   => 'root',
    group   => 'root',
    mode    => 0600,
    source  => "puppet:///modules/ssh/sshd_config",
    require => Class["ssh::install"],
    notify  => Class["ssh::service"],
  }
}

(5)定义服务类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/service.pp
class ssh::service {
  service { ssh::params::ssh_service_name:
    ensure     => running, 
    hasstatus  => true,
    hasrestart => true,
    enable     => true,
    require    => Class["ssh::config"],
  }
}

(7)定义ssh主类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/init.pp
class ssh {
  include ssh::params
  include ssh::install,ssh::config,ssh::service
}

(8)定义节点文件,调用模块

[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
node "centostest.ewin.com" {
  include ntp, yum, puppet, host, ssh
}

(9)应用节点文件

[root@puppet ~]# vi /etc/puppet/manifests/site.pp
import "centostest.pp"

(10)复制配置文件

复制一份SSH的配置文件(/etc/ssh/sshd_config)到puppet的目录下(/etc/puppet/modules/ssh/files/sshd_config),并在文件顶部插入一条注释说明:

### config  by  puppet ###

2、测试第一阶段

(1)客户端运行puppet agent -t,同时观察日志

[root@centostest ~]# tailf /var/log/messages
Nov 12 10:56:08 centostest puppet-agent[39921]: (/Stage[main]/Ssh::Config/File[/etc/ssh/sshd_config]/content) content changed '{md5}53ad75eb1f2269d23f6e4228353cbca3' to '{md5}a271889f546ff10b7af311ba35ed5e1c'
Nov 12 10:56:09 centostest puppet-agent[39921]: (/Stage[main]/Ssh::Service/Service[sshd]) Triggered 'refresh' from 1 events
Nov 12 10:56:09 centostest puppet-agent[39921]: Finished catalog run in 2.59 seconds
结论:配置文件更新,服务类刷新了一次,ssh的基本管理配置成功

(2)服务端通过ssh远程客户端

[root@puppet ~]# ls /root/.ssh/              #查看密钥目录
[root@puppet ~]# ssh centostest.ewin.com     #Ssh远程连接,可以用IP地址
The authenticity of host 'centostest.ewin.com (10.188.1.85)' can t be established.
RSA key fingerprint is 0d:79:7b:52:6c:8a:0c:cf:95:8e:f7:25:b4:95:98:c8.
Are you sure you want to continue connecting (yes/no)? yes      #输入yes确认连接
Warning: Permanently added 'centostest.ewin.com,10.188.1.85' (RSA) to the list of known hosts.               #添加到known_hosts列表中
root@centostest.ewin.com s password: #输入客户端centostest的root用户的密码
Last login: Wed Nov 12 10:52:34 2014 from ywzhou-pc.ewin.com #记录的最后一次登陆记录
结论:第一次通过ssh连接客户端会创建主机密钥并记录到known_hosts,提示输入root密码,这便是最常用的密码认证方式。
[root@centostest ~]# exit                 #退出ssh连接,返回到puppet服务器
logout
Connection to centostest.ewin.com closed.
[root@puppet ~]# ls /root/.ssh/           #再次查看密钥目录,出现了生成的记录文件
known_hosts
[root@puppet ~]# ssh centostest.ewin.com  #重新远程连接客户端,不再确认密钥
root@centostest.ewin.com''s password: 
Last login: Wed Nov 12 11:06:36 2014 from puppet.ewin.com
结论:第二次连接会读取known_hosts中记录的主机密钥,只需输入root密码后即可连接

第二阶段

说明:sshkey认证方式可以在linux服务器上(如puppet服务器)生成密钥,通过puppet服务器ssh到客户端进行管理;也可以在windows机器上使用连接管理工具(如Xshell 4或SecureCRT)生成密钥,这也是运维人员常用的软件;sshkey认证方式可以安全的、无需密码的、快速的连接客户端进行管理;

先介绍如何在puppet服务器上生成无密码的密钥(私钥和公钥一对),将公钥分发到客户端,关闭ssh的password认证连接。

3、使用ssh-keygen命令生成认证密钥

(1)服务端生成密钥

[root@puppet ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): #私钥保存路径
Enter passphrase (empty for no passphrase): #输入一个密语字符串,这里可以直接回车,空密码
Enter same passphrase again:            
Your identification has been saved in /root/.ssh/id_dsa. #私钥
Your public key has been saved in /root/.ssh/id_dsa.pub. #公钥
The key fingerprint is:
af:eb:5a:45:a7:91:dc:80:6e:03:75:ec:71:ae:2d:a0 root@centostest.ewin.com
参数说明:-t表示密钥类型,dsa或rsa,都是SSH2的,不支持SSH1;后面还可接-C "puppet test sshkey"表示注释,用来标示密钥的用途。

(3)查看生成的密钥

[root@puppet ~]# ls /root/.ssh/
id_dsa  id_dsa.pub  known_hosts
说明:三个文件分别是私钥、共钥、已连接过的SSH客户端密钥记录,下面来查看密钥文件:
[root@centostest ~]# cat /root/.ssh/id_dsa
-----BEGIN DSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,AB0A77C490AD5CE6
LCpOrQ8tKnAzDW1efWEB5AB2BXbnGUJLxKNpiwqyxR96XGVQEjgzqQwcjB6GJLI5
HUCW8JpS5IqdegJgsY76QRE4PD9XRiZRWEhaP+9v0NMFGF6lU93ahrk5DR3Jmpgx
jhJEPZTCSB889SaGhz+E89BOrxjRDootyiyYOjlwekvHQPJ0nrMt27shMYJ4apym
tXahjdlwlbzJzrONhq9eMklmZnfy/l8VdiDnVcUUIQMLN5IPZBcJiqFGUbLMspI3
E5zBI7jGcF/QYeOv7IeK8cjaZ8AwBznZOnOB9HUEBuwDAFS/Q6urAaeri2XvVgBy
1AcGljNZCfM4ltogNGzrZeVydtcqQstEPjCr8bpWkN4eq+PvXw3eA/5WkDaLpX88
qt8EkZ3HMa1zu2pRZ5iCG4z2rTxO9vFUXJnU+Un720jGYTEihWDycEiOco0brCgt
eQt8fwnbNO/GmQCeX5UeoGo92zqYMKUosHx65Kx+kmaxfAmaL7GPszCM3D9NgqY9
L6+eEyVyLaNecmD+ishPAFsyqA0pfIJClsNNQXYHL1Mbm5ADCAjfiguJjQZBzQt1
l8zSc6dCxaKgZoQ+epmjGjN5jbyXcqER
-----END DSA PRIVATE KEY-----
[root@centostest ~]# cat /root/.ssh/id_dsa.pub 
ssh-dss AAAAB3NzaC1kc3MAAACBAJrukh4ukPZ75uQEtLdczpv9RrjJDAY15wtJtVBtl1bUNxOdUiE3hlcqBRMrU01bokdz6LZQgy7j24k11/dS5ojawzkC9J5a4q4tBu3geasmmg+sd1GcC/mAYumCg2YzVGlsrUwbzgU62m7+FZSej0ySGORoaGcHLxdrpy24A5qbAAAAFQDt4RpHlMCV2bv2/kLcCZdI416S3QAAAIEAl2P8cZgSYRbO2pGMTp4wP7jfq/1EXq/+kNa4SpEpUT/qQbfLUSNSEcKMsQKGSiDih4AzTfTPxHf4UGwfB/Cxv9WdYEALrImE3KvETsY561u7UcgBLQkqLJsJLHp/q4qCLkgLfiohzn2aQKivtgqUCrQ9wAZUhW9FUZwskVWM884AAACAclP+RlLbblUL5eTzq4DEpOtWKbX5UXXwxoOyrc5ADS6NtM9D3jyB/pJ/Oiz4uU33J810EmYJLkPTph0FKk94MiCkQLg6zaoV9Ljlbw2BS46YzdEv5bCKjqaW9jIh+o/PnfF8kGRyIImGLce6SasGhRqkm6E3z2djBMtA/u9TZok= root@centostest.ewin.com
说明:将公钥文件id_dsa.pub中的三段(类型、密钥编码、用户)定义到密钥类中

(4)定义密钥类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/key.pp
class ssh::key {
  ssh_authorized_key { "puppet":   #管理机的主机名,也是公钥名,不重复就行
    ensure  => present,            #基础属性,指定key是否生成,present、absent
    type    => "ssh-dss",          #key的加密类型
    key     => "AAAAB3NzaC1kc3MAAACBAJrukh4ukPZ75uQEtLdczpv9RrjJDAY15wtJtVBtl1bUNxOdUiE3hlcqBRMrU01bokdz6LZQgy7j24k11/dS5ojawzkC9J5a4q4tBu3geasmmg+sd1GcC/mAYumCg2YzVGlsrUwbzgU62m7+FZSej0ySGORoaGcHLxdrpy24A5qbAAAAFQDt4RpHlMCV2bv2/kLcCZdI416S3QAAAIEAl2P8cZgSYRbO2pGMTp4wP7jfq/1EXq/+kNa4SpEpUT/qQbfLUSNSEcKMsQKGSiDih4AzTfTPxHf4UGwfB/Cxv9WdYEALrImE3KvETsY561u7UcgBLQkqLJsJLHp/q4qCLkgLfiohzn2aQKivtgqUCrQ9wAZUhW9FUZwskVWM884AAACAclP+RlLbblUL5eTzq4DEpOtWKbX5UXXwxoOyrc5ADS6NtM9D3jyB/pJ/Oiz4uU33J810EmYJLkPTph0FKk94MiCkQLg6zaoV9Ljlbw2BS46YzdEv5bCKjqaW9jIh+o/PnfF8kGRyIImGLce6SasGhRqkm6E3z2djBMtA/u9TZok=", #生成的密钥值,一般是十六进制数字的长字符串
    target  => "/root/.ssh/authorized_keys", #存储的SSH密钥文件的路径
    user    => 'root',                       #指定是哪个用户(客户端)的key
    require => Class["ssh::install"],
  }
}
说明:可以使用puppet的user资源定义一个用户来分配sshkey,常用于多级系统管理员的IT部门。

(5)定义ssh主类

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/init.pp
class ssh {
  include ssh::params
  include ssh::install, ssh::config, ssh::service, ssh::key
}

(6)定义节点文件,调用模块

[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
node "centostest.ewin.com" {
  include ntp, yum, puppet, host, ssh
}

(7)应用节点文件

[root@puppet ~]# vi /etc/puppet/manifests/site.pp
import "centostest.pp"

(8)修改ssh配置文件

[root@puppet ~]# vi /etc/puppet/modules/ssh/files/sshd_config
PasswordAuthentication no   #关闭密码认证

4、测试第二阶段

(1)客户端运行puppet agent -t,同时观察日志

[root@centostest ~]# tailf /var/log/messages
Nov 12 11:39:39 centostest puppet-agent[28301]: (/Stage[main]/Ssh::Config/File[/etc/ssh/sshd_config]/content) content changed '{md5}a271889f546ff10b7af311ba35ed5e1c' to '{md5}1983ed7e2956b9bd4c627e45906b193e'
Nov 12 11:39:39 centostest puppet-agent[28301]: (/Stage[main]/Ssh::Service/Service[sshd]) Triggered 'refresh' from 1 events
Nov 12 11:39:49 centostest puppet-agent[28534]: (/Stage[main]/Ssh::Key/Ssh_authorized_key[puppet]/ensure) created
结论:更新了配置文件、重启了服务、创建了密钥记录文件

(2)服务端通过ssh远程客户端

[root@puppet ~]# ssh centostest.ewin.com
Last login: Wed Nov 12 11:08:49 2014 from puppet.ewin.com
[root@centostest ~]#
结论:使用密钥认证方式连接,无需输入密码直接就连接上了

(3)查看客户端生成的密钥文件

[root@centostest ~]# cat /root/.ssh/authorized_keys 
# HEADER: This file was autogenerated at Tue Nov 12 11:39:49 +0800 2014
# HEADER: by puppet.  While it can still be managed manually, it
# HEADER: is definitely not recommended.
ssh-dss AAAAB3NzaC1kc3MAAACBAJrukh4ukPZ75uQEtLdczpv9RrjJDAY15wtJt
VBtl1bUNxOdUiE3hlcqBRMrU01bokdz6LZQgy7j24k11/dS5ojawzkC9J5a4q4tBu
3geasmmg+sd1GcC/mAYumCg2YzVGlsrUwbzgU62m7+FZSej0ySGORoaGcHLxdrpy2
4A5qbAAAAFQDt4RpHlMCV2bv2/kLcCZdI416S3QAAAIEAl2P8cZgSYRbO2pGMTp4w
P7jfq/1EXq/+kNa4SpEpUT/qQbfLUSNSEcKMsQKGSiDih4AzTfTPxHf4UGwfB/Cxv
9WdYEALrImE3KvETsY561u7UcgBLQkqLJsJLHp/q4qCLkgLfiohzn2aQKivtgqUCr
Q9wAZUhW9FUZwskVWM884AAACAclP+RlLbblUL5eTzq4DEpOtWKbX5UXXwxoOyrc5
ADS6NtM9D3jyB/pJ/Oiz4uU33J810EmYJLkPTph0FKk94MiCkQLg6zaoV9Ljlbw2B
S46YzdEv5bCKjqaW9jIh+o/PnfF8kGRyIImGLce6SasGhRqkm6E3z2djBMtA/u9TZ
ok= puppet
结论:可以看到客户端的认证密钥文件中成功记录了前面生成的公钥;通过头部注释信息可以看到是由puppet部署的。另外如果不是指定root用户,就在/home/<user_name>/.ssh中查找。

注意:authorized_keys中只会保存由puppet的ssh_authorized_key部署的公钥,其他的自动删除。

(4)其他服务端访问测试

[root@zabbix ~]# ssh 10.188.1.85
The authenticity of host '10.188.1.85 (10.188.1.85)' cant be established.
RSA key fingerprint is 0d:79:7b:52:6c:8a:0c:cf:95:8e:f7:25:b4:95:98:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.188.1.85' (RSA) to the list of known hosts.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
结论:可以看到Permission denied权限拒绝,无法登陆,密码认证方式被关闭了,成功转换到KEY认证。

扩展阶段

说明:添加一台管理机公钥,在windows系统下使用工具进行sshkey认证连接。

5、生成认证密钥

(1)使用Xshell创建用户密钥

[
img_76715c35ec55c62876d7d8471c56b077.jpe
转载shu.io/upload_images/9967595-f9572dac78ae4653.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 "QQ截图20141117150812.png")

[图片上传中...(image-41b50-1540881867449-17)]

说明:密码可以为空

(2)使用SecureCRT创建用户密钥

[图片上传中...(image-789f14-1540881867449-10)]

说明:进入KEY的目录,用记事本打开Identity.pub查看公钥信息

(3)将公钥注册页面上的信息添加到密钥类文件中

[root@puppet ~]# vi /etc/puppet/modules/ssh/manifests/key.pp
class ssh::key {
  ssh_authorized_key { "puppet":
    ensure => present, 
    type   => "ssh-dss",
    key    => "AAAAB3NzaC1kc3MAAACBAMzwtzbkG9yV8C9zM3n85FzJJaZrEPJbDQ1ZFfhkpZm2VYPHaFxCI3M2jTvHtkBV2OKlkHg2mrZ2Xt6Tp+d3E5ophZMdZmuOApiQPhD5z0Ld0/Wb2RJzLTwYy/BO+UNPnlorpy0kJjvjCrAACcIIVtbUMquKcdydfL8KV+shPEm3AAAAFQCRQw1nUvCqvL8tvOM+8cc6A2/tcQAAAIAWpKoqsoHIQp6bFeY7+tNQFo11Yzl3T3UJq7HG7CkOWQdMKwJJBXvub3Sgp9hA4415A12G8GKfxRn6JotmLLiAXeQmYYGLR9wwGTqYNNZwJL2JhtHe4HroGp7CuP5M71NcZ5QtvwIMqxa30kYOKf4RaV5mpf/K13VItrjcK5hxAwAAAIEAxodCPc3l63iBkjDhAUzfZyU3KtgIa/AO6E6t/60j485w0aYl2fpam+oA2akvtgZSMIZ1o9dkQotMCubz82wsMo6nDia8V89D3goEn0Uu1512dxpPWXN7cH8C4BC0Vu1QXHYY8ASkSAPMsSTsD49Ir736BIFSk2oTOcIKIJYpNO0=",
    target => "/root/.ssh/authorized_keys",
    user   => 'root',
    require => Class["ssh::install"],
  }

  #添加ywzhou-pc的公钥资源
  ssh_authorized_key { "ywzhou-pc":
    ensure => present,
    type   => "ssh-dss", 
    key    => "AAAAB3NzaC1kc3MAAACBAPrbA22nnuySLeoddXjkehPkX3qUjRt8vbJPgGoWzRW8gAN88+xU7806/nqKso5pNAxP6XsDDkG/MYhtvXXgTlOxpeAkXZQpVUtHZp/ycnTRlqCzmDZ5uHqrChcVKEP/jT9qLcxdEkyBClHefBIKiN4SeeB6hF+X4+3ZGecb8ljvAAAAFQCrbJQ0w24MRAFUeV3LLNRn3S4z3wAAAIAzehQUM1wtbXHhgG4PuvkCVQJEqJNFgefHlL10Tjeb8qVCVlfV7eULbmlwVnMReUBwc3tq4e15Bfpa5SoBtRpK3Srb0z/0Nyicfp1ADl+JW4VR0TxcsFKBLSvCwma2ch9wKg5I1AwXlRTV+ayD4RyXmLhg1Mf8i2bZblNaazdRhAAAAIEAsFQbDzmYkd8rGRjj2MJ2bQP2vutVzCMmRp8iDEbRPSiNX0hXhPioet/uyXaqjBjY6n26HsIGOUl2W8CYGpjuFtZ9qw0m7gNKghFOSjqRdOYQAG9/BG9kysIH6UFMarHxSXd9CGs2eRh4o3lXgA7LvSMzHDSRHY5dyxlYCBXPDw8=", 
    target => "/root/.ssh/authorized_keys", 
    user   => 'root', 
    require => Class["ssh::install"],
  }
}

6、测试扩展阶段

(1)客户端运行puppet agent -t,同时查看日志

[root@centostest ~]# tailf /var/log/messages
Nov 12 12:17:30 centostest puppet-agent[10082]: (/Stage[main]/Ssh::Key/Ssh_authorized_key[ywzhou-pc]/ensure) created

(2)客户端查看公钥记录文件

[root@centostest ~]# cat /root/.ssh/authorized_keys 
# HEADER: This file was autogenerated at Wed Nov 12 12:17:30 +0800 2014
# HEADER: by puppet.  While it can still be managed manually, it
# HEADER: is definitely not recommended.
ssh-dss AAAAB3NzaC1kc3MAAACBAPrbA22nnuySLeoddXjkehPkX3qUjRt8vbJPgGoWz
RW8gAN88+xU7806/nqKso5pNAxP6XsDDkG/MYhtvXXgTlOxpeAkXZQpVUtHZp/ycnTRlq
CzmDZ5uHqrChcVKEP/jT9qLcxdEkyBClHefBIKiN4SeeB6hF+X4+3ZGecb8ljvAAAAFQC
rbJQ0w24MRAFUeV3LLNRn3S4z3wAAAIAzehQUM1wtbXHhgG4PuvkCVQJEqJNFgefHlL10
Tjeb8qVCVlfV7eULbmlwVnMReUBwc3tq4e15Bfpa5SoBtRpK3Srb0z/0Nyicfp1ADl+JW
4VR0TxcsFKBLSvCwma2ch9wKg5I1AwXlRTV+ayD4RyXmLhg1Mf8i2bZblNaazdRhAAAAI
EAsFQbDzmYkd8rGRjj2MJ2bQP2vutVzCMmRp8iDEbRPSiNX0hXhPioet/uyXaqjBjY6n2
6HsIGOUl2W8CYGpjuFtZ9qw0m7gNKghFOSjqRdOYQAG9/BG9kysIH6UFMarHxSXd9CGs
2eRh4o3lXgA7LvSMzHDSRHY5dyxlYCBXPDw8= ywzhou-pc
ssh-dss AAAAB3NzaC1kc3MAAACBAMzwtzbkG9yV8C9zM3n85FzJJaZrEPJbDQ1ZFfhkp
Zm2VYPHaFxCI3M2jTvHtkBV2OKlkHg2mrZ2Xt6Tp+d3E5ophZMdZmuOApiQPhD5z0Ld0/
Wb2RJzLTwYy/BO+UNPnlorpy0kJjvjCrAACcIIVtbUMquKcdydfL8KV+shPEm3AAAAFQC
RQw1nUvCqvL8tvOM+8cc6A2/tcQAAAIAWpKoqsoHIQp6bFeY7+tNQFo11Yzl3T3UJq7HG
7CkOWQdMKwJJBXvub3Sgp9hA4415A12G8GKfxRn6JotmLLiAXeQmYYGLR9wwGTqYNNZwJ
L2JhtHe4HroGp7CuP5M71NcZ5QtvwIMqxa30kYOKf4RaV5mpf/K13VItrjcK5hxAwAAAI
EAxodCPc3l63iBkjDhAUzfZyU3KtgIa/AO6E6t/60j485w0aYl2fpam+oA2akvtgZSMIZ
1o9dkQotMCubz82wsMo6nDia8V89D3goEn0Uu1512dxpPWXN7cH8C4BC0Vu1QXHYY8ASk
SAPMsSTsD49Ir736BIFSk2oTOcIKIJYpNO0= puppet

(3)window管理机使用Xshell工具连接客户端

说明:因为设置的是空密码,直接回车即可连接。

**(4)window管理机使用SecureCRT工具连接客户端 **

说明:因为没有在puppet中定义SecureCRT的公钥,因此认证失败,如何添加自己尝试。 

7、ssh资源(sshkey和ssh_authorized_key)

(1)sshkey资源

说明:sshkey资源负责如何安装key到/etc/ssh/ssh_known_hosts,不能管理用户的授权密钥,因此不常用。
sshkey { 'resource title':
  name         => #密钥相关的主机名
  ensure       => #基本属性,present, absent
  host_aliases => #主机别名,支持数组表示多个别名
  key          => #密钥,通常是一长串编码字符窜
  target       => #存储SSH密钥的文件
  type         => #SSH类型,ssh-dss (dsa), ssh-ed25519 (ed25519), ssh-rsa (rsa), 
                  #ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521
}

(2)ssh_authorized_key资源

说明:ssh_authorized_key资源负责管理授权密钥,大部分都使用这个资源类型

ssh_authorized_key { 'resource title':
  name     => "hostname",   #主机名
  ensure   => present,      #基础属性,指定key是否生成,present、absent
  type     => "ssh-dss",    #key生成的加密类型,ssh-dsa或ssh-rsa
  key      => "密钥值",     #生成的密钥值,一般是十六进制数字的长字符串
  target   => "/home/sky/.ssh/authorized_keys", #存储的SSH密钥文件的路径
  user     => 'sky',        #指定是哪个用户的key
  options  => ,             #其他选项
}

(3)官方例子

查看密钥字符串

cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAy5mtOAMHwA2ZAIfW6Ap70r+I4EclYHEec5xIN59ROUjss23Skb1OtjzYpVPaPH8mSdSmsN0JHaBLiRcu7stl4O8D8zA4mz/vw32yyQ/Kqaxw8l0K76k6t2hKOGqLTY4aFbFISV6GDh7MYLn8KU7cGp96J+caO5R5TqtsStytsUhSyqH+iIDh4e4+BrwTc6V4Y0hgFxaZV5d18mLA4EPYKeG5+zyBCVu+jueYwFqM55E0tHbfiaIN9IzdLV+7NEEfdLkp6w2baLKPqWUBmuvPF1Mn3FwaFLjVsMT3GQeMue6b3FtUdTDeyAYoTxrsRo/WnDkS6Pa3YhrFwjtUqXfdaQ== nick@magpie.puppetlabs.lan

翻译成SSH密钥资源

ssh_authorized_key { 'nick@magpie.puppetlabs.lan':
  user => 'nick',
  type => 'ssh-rsa',
  key  => 'AAAAB3NzaC1yc2EAAAABIwAAAQEAy5mtOAMHwA2ZAIfW6Ap70r+I4EclYHEec5xIN59ROUjss23Skb1OtjzYpVPaPH8mSdSmsN0JHaBLiRcu7stl4O8D8zA4mz/vw32yyQ/Kqaxw8l0K76k6t2hKOGqLTY4aFbFISV6GDh7MYLn8KU7cGp96J+caO5R5TqtsStytsUhSyqH+iIDh4e4+BrwTc6V4Y0hgFxaZV5d18mLA4EPYKeG5+zyBCVu+jueYwFqM55E0tHbfiaIN9IzdLV+7NEEfdLkp6w2baLKPqWUBmuvPF1Mn3FwaFLjVsMT3GQeMue6b3FtUdTDeyAYoTxrsRo/WnDkS6Pa3YhrFwjtUqXfdaQ==',
}

应用SSH资源到USER资源中

user { 'nick':
  ensure         => present,
  purge_ssh_keys => true,
}

将会删除~/.ssh/authorized_keys中不被ssh_authorized_key管理的密钥

目录
相关文章
|
4月前
|
监控 网络协议 网络安全
ssh服务中如何批量管理100多台机器(Paramiko、 psutil模块)、跳板机(堡垒机)
ssh服务中如何批量管理100多台机器(Paramiko、 psutil模块)、跳板机(堡垒机)
|
Java 应用服务中间件 数据库连接
maven(二) maven项目构建ssh工程(父工程与子模块的拆分与聚合)
前一节我们明白了maven是个什么玩意,这一节就来讲讲他的一个重要的应用场景,也就是通过maven将一个ssh项目分割为不同的几个部分独立开发,很重要,加油
170 0
|
开发工具 数据安全/隐私保护 git
|
Java 应用服务中间件 Linux
puppet连载九:linux安装jdk、tomcat模块
安装jdk1.8.0_91和tomcat8.0.36 mkdir -p /etc/puppet/modules/linuxjdktomcat/{manifests,templates,files} vi /etc/puppet/modules/linuxjdktomcat/manifests/init.
1078 0
|
MySQL 关系型数据库 Linux
puppet连载10:linux安装percona57/56/55、sysbench、tpcc模块
在服务端/puppet/soft下建my.cnf,内容为https://www.jianshu.com/p/c63fc6c71279 在服务端/puppet/soft下建changemysql57pass.
1073 0
|
Linux 开发工具 git
puppet连载七:linux基础组件安装模块
linux基础组件安装模块linuxbaseinstall 更换源,安装gcc gcc-c++ glibc-devel make ncurses-devel openssl-devel autoconf git mkdir -p /etc/puppet...
1073 0
puppet连载六:创建测试模块test
创建测试模块 mkdir -p /etc/puppet/modules/test/{manifests,templates,files} vi /etc/puppet/modules/test/manifests/init.
893 0
|
网络协议 Linux 安全
puppet连载八:linux优化模块
linux优化模块 在服务端先建立文件limits.con vi /puppet/soft/limits.conf soft nofile 102400 hard nofile 102400 soft nproc 102400 hard nproc 102400 保存,退出 在服务端建立文件sysctl.
805 0
|
测试技术
puppet cron 模块
转载:http://blog.51cto.com/ywzhou/1577299 Puppet模块章节环境说明 服务端 | 客户端 操作系统:CentOS 6.
993 0
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1113 0