Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19737673
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: LINUX

2009-02-18 11:03:54

使用SSH安全地进行自动化系统管理

建议参考书籍:The Secure Shell: The Definitive Guide by Daniel J. Barrett and Richard Silverman (O'Reilly and Associates, 2001).

Sshcfengine的重要伙伴。SSH and cfengine使用同样的分布式认证模型。建议配置cfengine为首次链接时相信其他机器,以免key的产生和分发出现问题,可能导致cfengine出错。

基础

       Ssh提供了RSADSA两种加密机制,我们默认是使用RSA

 

使用Public- Key认证

       为了方便使用ssh-agent,一般使用在客户端使用root用户。

[root@svr-87-13 ~]# ssh-keygen -t rsa -b 2048

Generating public/private rsa key pair.

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

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

5b:8c:93:9f:88:db:8c:ff:02:06:5e:ce:ad:82:1a:e9 root@svr-87-13

[root@svr-87-13 ~]#

    使用较长的密钥仅仅是ssh链接建立的时候比较慢,建立后没有影响的。Ssh –c可以选择要加密的方式,有des3desblowfish等,一般推荐使用快速安全的blowfish

mkdir -p ~/.ssh

chmod 0700 ~/.ssh

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

chmod 0600  ~/.ssh/authorized_keys

    这样就可以不需要密码登陆了。还可以修改/etc/ssh/sshd_config,禁止使用密码登陆:PasswordAuthentication no

 

使用ssh-agent

     ssh-agent的基本使用

       启动:ssh-agent bash 或者ssh-agent screen(参见,)

       X环境下为了便于使用可以修改:~/.Xclien ts (or ~/.xinitrc)

#!/bin/bash

 

cd ~

exec ssh-agent bin/startx-continue

 

查看keyssh-add –l

       # ssh-add

Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

       这种操作一般只对当前窗口有效,可以在每次login时启动一个ssh-agent,但是并发太多依旧有问题,可以考虑使用keychain,它可以使所有login共享一个ssh-agent。参考资料:http://www-106.ibm.com/developerworks/library/l-keyc2/

      

     ssh-agent的高级使用

 

不启动新进程使用ssh-agent

% eval 'ssh-agent'

使用eval是因为ssh-agent启动的时候会报一些环境变量。

# ssh-agent

SSH_AUTH_SOCK=/tmp/ssh-vyVNHR2957/agent.2957; export SSH_AUTH_SOCK;

SSH_AGENT_PID=2958; export SSH_AGENT_PID;

echo Agent pid 2958;

这样启动的缺点在于在退出时要通过外部手段来杀死agent

比如一下实例:

#!/bin/bash
 
# Start the agent (don't display PID)
eval 'ssh-agent' >/dev/null
# Now, ask for the key once
ssh-add
 
# Now, perform a bunch of SSH operations
ssh host1 'command1'
ssh host1 'command2'
ssh host2 'command3'
 
# Finally, kill the agent and exit
kill $SSH_AGENT_PID
exit 0
 

     密钥转发

 

密钥一般只转发指定的信任的机器的root用户。

 

非交互式环境使用ssh-agent.

      

限制RSA认证

       具体配置选项参见教材

       另还讲述了端口转发的配置,暂略。

 

对公共账户使用SSH

暂略,到时再来研究perl脚本。

阅读(3946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~