Windows(SecureCRT / Xshell / Putty)
Linux (ssh)
1、用于代替 telnet, 进行远程登录。 # ssh user@host 2、如果bending用户名与远程主机用户名一致,登录时可以省略用户名。 # ssh host 3、ssh 协议默认端口 22,也就是缺省情况是连接远程主机的22号端口,使用 -p 参数可以指定端口号 # ssh -p 2222 user@host 4、ssh 远程执行命令 # ssh user@host 'ls -l /etc' 输入正确的密码后,ssh会链接远程服务器的sshd服务器程序,然后执行远程服务器上的 ls –l /etc 命令 ,并把输入结果传到本地服务器。相当于你先登陆到远程服务器,然后再实行命令ls –l /,最后再登出服务器。
1
2
3
4
|
# ssh user@host
The authenticity of host
'host (12.18.429.21)'
can't be established.
RSA key fingerprint is 98:2e:d7:e0:de:9f:ac:67:28:c2:42:2d:37:16:58:4d.
Are you sure you want to
continue
connecting (
yes
/no
)?
|
1
|
Are you sure you want to
continue
connecting (
yes
/no
)?
yes
|
1
|
Warning: Permanently added
'host,12.18.429.21'
(RSA) to the list of known hosts.
|
Your home directory ~ and your ~/.ssh directory on the remote machine must be writable only by you: rwx------ and rwxr-xr-x are fine, but rwxrwx--- is no good, even if you are the only user in your group (if you prefer numeric modes: 700 or 755, not 775).
Your private key file (on the local machine) must be readable and writable only by you: rw-------, i.e. 600.
Your ~/.ssh/authorized_keys file (on the remote machine) must be readable (at least 400), but you'll need it to be also writable (600) if you will add any more keys to it.
Also, if SELinux is set to enforcing, you may need to run restorecon -R -v ~/.ssh (see e.g. Ubuntu bug 965663 and Debian bug #658675; this is patched in CentOS 6).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
[zbj@web ~]
# ssh-keygen -b 1024 -t dsa
Generating public
/private
dsa key pair.
#提示正在生成,如果选择4096长度,可能需要较长时间
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.
#提示公钥和私钥已经存放在
/root/
.
ssh
/目录下
The key fingerprint is:
71:e5:cb:15:d3:8c:05:ed:05:84:85:32:ce:b1:31:ce zbj@web
#提示key的指纹
说明:
-b 1024 采用长度为1024字节的公钥/私钥对,最长4096字节,一般1024或2048就可以了,太长的话加密解密需要的时间也长。
-t dsa 采用dsa加密方式的公钥/私钥对,除了dsa还有rsa方式,rsa方式最短不能小于768字节长度。
1、在本地生成一对密钥文件(公钥和私钥)
# ssh-keygen
# 以上命令等价于 ssh-keygen -t rsa
# -t:指定密钥的类型,默认为SSH-2 的rsa类型;
[root@localhost ~]
# ssh-keygen
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:
The key's randomart image is:
+--[ RSA 2048]----+
| oo.|
| . * . |
| o o = |
| . o o |
| S . . o |
| . o |
| . . = .|
| E . + = +.|
| . o.o +o.|
+-----------------+
[zbj@web ~]$
ls
-l .
ssh
total 8
-rw------- 1 zbj zbj 1675 Feb 14 14:29 id_rsa
-rw-r--r-- 1 zbj zbj 389 Feb 14 14:29 id_rsa.pub
|
1
2
3
|
2、将公钥上传到欲远程连接的主机上面
# ssh-copy-id user@host
|
1
2
3
4
5
|
## RHEL / CentOS
# service sshd restart
## ubuntu
# /etc/init.d/ssh restart
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
## 如果没有 .ssh 目录执行 mkdir .ssh 进行创建
# 在需要开启ssh连接的用户目录下建立 .ssh目录
# mkdir .ssh
# chmod 700 .ssh
# cd .ssh
# mv Identity.pub authorized_keys
# chmod 600 authorized_keys
## 如果有多个公钥需要保存,必须使用追加方式
# cat Identity.pub >> authorized_keys
# 如果密钥的格式不是OpenSSH的,需要进行转换
# ssh-keygen -i -f <公钥文件> >> ./.ssh/authorized_keys
# 转换为公钥为openssh能识别的公钥格式,转换后的文件名为authorized_keys,这是ssh的公钥文件)
# ssh-keygen -i -f Identity.pub >> authorized_keys
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# vi /etc/sshd_config
Protocol 2
ServerKeyBits 2048
PermitRootLogin no
#禁止root登录而已,与本文无关,加上安全些
#以下三行没什么要改的,把默认的#注释去掉就行了
RSAAuthentication
yes
PubkeyAuthentication
yes
AuthorizedKeysFile .
ssh
/authorized_keys
PasswordAuthentication no
#初次试验的时候最好用yes,否则配置错误的话重启sshd后你就悲剧了,key 登陆成功之后,再改为 no
PermitEmptyPasswords no
# 在确定你的公钥加入到authorzed_keys后重启sshd服务
# /etc/init.d/sshd restart
|
权限是否是600
属主是否正确
选择创建密钥的时候选择的是否是openssh,如果不是需要转换格式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
##SSH服务器监听的选项
#修改监听的端口,可以增加入侵者探测系统是否运行了 sshd守护进程的难度。
Port 30003
#使用SSH V2协议
Protocol 2
#监听的地址为所有地址
#ListenAdderss 0.0.0.0
ListenAdderss 192.168.5.0
/24
#禁止DNS反向解析
UseDNS no
# 密钥长度
ServerKeyBits 2048
##用户登录控制选项
#是否允许root用户登录,如果允许用户使用root用户登录,那么黑客们可以针对root用户尝试暴力破解密码,给系统安全带来风险。
PermitRootLogin no
#是否允许空密码用户登录,允许使用空密码系统就像不设防的堡垒,任何安全措施都是一句空话。
PermitEmptyPasswords no
#登录验证时间(2分钟)
LoginGraceTime 2m
#最大重试次数
MaxAuthTries 6
#只允许user用户登录,与DenyUsers选项相反
AllowUsers user1 user2
# 禁止指定的 用户或组
DenyUsers apache
#注:Allow 和 Deny 可以组合使用,它们的处理顺序是:DenyUsers, AllowUsers, DenyGroups, AllowGroups
##登录验证方式
#关闭密码验证
PasswordAuthentication no
#以下三行没什么要改的,把默认的#注释去掉就行了
RSAAuthentication
yes
#启用秘钥验证
PubkeyAuthentication
yes
#指定公钥数据库文件
AuthorsizedKeysFile .
ssh
/authorized_keys
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Port 30003
ListenAddress 192.168.1.5
Protocol 2
#白名单
AllowUsers user1 user2
#黑名单
denyUser user3 user4
PasswordAuthentication no
PermitEmptyPasswords no
PermitRootLogin no
UseDNS no
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/bin/bash
# 配置sshd的参数
/bin/cp
/etc/ssh/sshd_config
/etc/ssh/sshd_config
.bak.$(
date
+
"%Y-%m-%d-%H-%m-%S"
)
/bin/sed
-i
's/#Port 22/Port 30003/g'
/etc/ssh/sshd_config
/bin/sed
-i
's/#LogLevel INFO/LogLevel VERBOSE/g'
/etc/ssh/sshd_config
/bin/sed
-i
"s/PasswordAuthentication yes/PasswordAuthentication no/g"
/etc/ssh/sshd_config
/bin/sed
-i
"s/#PermitEmptyPasswords no/PermitEmptyPasswords no/g"
/etc/ssh/sshd_config
/bin/sed
-i
"s/#PermitRootLogin yes/PermitRootLogin no/g"
/etc/ssh/sshd_config
/bin/sed
-i
"s/#UseDNS yes/UseDNS no/"
/etc/ssh/sshd_config
/sbin/service
sshd reload
|