ssh(openssh)小记

来源:互联网 发布:手机为什么仅限2g网络 编辑:程序博客网 时间:2024/05/16 02:11

SSH(Secure Shell)具有客户端/服务器体系结构。OpenSSH支持SSH协议1和协议2,本文忽略前者。

ssh

客户端

~/.ssh/config
/etc/ssh/ssh_config

sshd

服务器

/etc/ssh/sshd_config

认证(Authentication)

5种认证方式?

  • GSSAPI-based authentication
  • host-based authentication
  • public key authentication
  • challenge-response authentication
  • password authentication

在ssh的配置文件中,通过 PreferredAuthentiactions可以更改优先顺序,默认值

gssapi-with-mic, hostbased, publickey,keyboard-interactive, password

GSSAPI-based authentication

GSSAPI: Generic Security Services Application Program Interface

sshd配置选项:

GSSAPIAuthentication

是否启用,默认no

GSSAPIKeyExchange

 

...

...

基于主机的认证(host-based authentication)

在sshd的配置文件中,相关的选项:

HostbasedAuthentication

是否启用,默认为 no

HostbasedUsesNameFromPacketOnly

默认为 no

IgnoreRhosts

是否忽略 .rhost和 .shosts ,默认为yes

IgnoreUserKnownHosts

是否忽略 ~/.ssh/known_hosts,默认为no

如果客户端主机名存在于服务器中的

  • /etc/hosts.equiv
  • /etc/shosts.equiv
  • ~/.rhosts
  • ~/.shosts

文件内,

而且客户端的host key存在于服务器中的:

  • /etc/ssh/ssh_known_hosts
  • ~/.ssh/known_hosts

文件内

认证通过。

基于公钥的认证(public key authentication)

在sshd的配置文件中,相关的选项:

PubkeyAuthentication

是否启用,默认为yes

RevokedKeys

指定一个文件,包含所有被废弃的key

AuthorizedKeysFile

指定用来进行认证的公钥文件,默认 .ssh/authorized_keys

服务器文件:

  • ~/.ssh/authorized_keys

包含允许登录的公钥

客户端通过 ssh-keygen 生成公钥密钥对(有3种算法可用):

DSA

~/.ssh/id_dsa

~/.ssh/id_dsa.pub

RSA

~/.ssh/id_rsa

~/.ssh/id_rsa.pub

ECDSA

~/.ssh/id_ecdsa

~/.ssh/id_ecdas.pub

工具ssh-copy-id可用来将公钥拷贝到服务器的 ~/.ssh/authorized_keys 文件中:

ssh-copy-id -i ~/.ssh/id_dsa.pub dbzhang800@abc.example.net

使用这种方式时,ssh-agent通常会被使用。

certificate authentication

这是 public key authentication 的一个变体,相关配置:

TrustedUserCAKeys

AuthorizedPrincipalsFile

Challenge-response authentication

sshd配置选项

ChallengeResponseAuthentication

是否允许,默认yes(debian系默认no)

UsePAM

是否使用PAM(Pluggable Authentication Module)

服务器发送任意的challenge文字,客户端解码后送回。

例子:

  • BSD Authentication
  • PAM (非BSD系统)

基于密码的认证(password authentication)

如果其他的都失败了,将采用该方式,sshd配置选项

PasswordAuthentication

是否启用,默认yes

PermitEmptyPasswords

是否允许空密码,默认no

UsePAM

是否使用PAM

ssh-agent

使用 ssh-keygen 生成公钥密钥对时,一般会选择一个passphrase对本地的密钥进行加密。这样以来,每次使用到密钥时,都需要输入这个passphrase,挺麻烦。

ssh-agent可以稍微减轻一点这种痛苦:

ssh-agent 是一个守护进程(daemon),设计任务就是对密钥进行高速缓存。

运行ssh-agent,它启动时会有如下的输出:

debao@ubuntu~$ ssh-agentSSH_AUTH_SOCK=/tmp/ssh-jeQdoYU11925/agent.11925; export SSH_AUTH_SOCK;SSH_AGENT_PID=11926; export SSH_AGENT_PID;echo Agent pid 11926;

可以看出:这是shell命令,它会设置2个环境变量。但我们需要用eval执行它。一般来说,都合并到一块进行

debao@ubuntu:~$ eval `ssh-agent`Agent pid 12385

而后,使用 ssh-add 密钥加入ssh-agent的缓存:

$ ssh-add ~/.ssh/id_dsaEnter passphrase for /home/debao/.ssh/id_dsa: Identity added: /home/debao/.ssh/id_dsa (/home/debao/.ssh/id_dsa)

参考

  • http://www.openssh.com/index.html


原创粉丝点击