linux配置ssh key有两个好处:
1、可以免密登录服务器
2、增加服务器安全性,避免黑客暴力破解密码。
原理:首先由用户生成一对密钥,然后将公钥保存在SSH服务器用户的目录下.ssh子目录中的authorized_key文件里(/root/.ssh/authorized_key).私钥保存在本地计算机.当用户登陆时,服务器检查authorized_key文件的公钥是否与用户的私钥对应,如果相符则允许登入,否则拒绝.由于私钥只有保存在用户的本地计算机中,因此入侵者就算得到用户口令,也不能登陆到服务器.
1、首先新增一个普通用户
为了服务器更加安全,一般都是禁止root账号直接登录,所以要创建一个普通账号用于登录服务器,然后再切换root账号管理服务器,即使authorized_key泄露,也能避免root权限被使用。1
2
3
4###创建tenke用户,并且创建/home/tenke目录
root@ubuntu:~/.ssh$ useradd tenke -d /home/tenke -m -s /bin/bash
###添加用户密码
root@ubuntu:~/.ssh$ passwd tenke
2、首先生成ssh密钥1
2
3
4
5
6
7
8
9
10
11
12root@ubuntu:~/.ssh$ ssh-keygen -t rsa
###直接回车3次
###key文件会保存在/root/.ssh目录下
###这时候.ssh目下会多出几个文件
###id_rsa 私钥文件,放在自己电脑
###id_rsa.pub 公钥文件,放在/home/tenke/.ssh下,并且修改名字为:authorized_keys
root@ubuntu:~/.ssh$ mkdir -p /home/tenke/.ssh
root@ubuntu:~/.ssh$ mv id_rsa.pub /home/tenke/.ssh/authorized_keys
###修改.ssh目录的权限 700
root@ubuntu:~/.ssh$ chmod 700 /home/tenke/.ssh
###修改authorized_keys文件权限 600
root@ubuntu:~/.ssh$ chmod 600 /home/tenke/.ssh/authorized_keys
3、修改服务器sshd配置1
2###打开sshd_config文件
root@ubuntu:~/.ssh$ vi /etc/ssh/sshd_config
修改ssh默认端口,禁止root账号登录,禁止密码登录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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88# Package generated configuration file
# See the sshd_config(5) manpage for details
# What ports, IPs and protocols we listen for
Port 20000 ###修改默认22端口
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024
# Logging
LogLevel INFO
# Authentication:
LoginGraceTime 120
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no ###禁止空密码登录
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PAM authentication via ChallengeResponseAuthentication may bypass
# If you just want the PAM account and session checks to run without
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
UseDNS no
AddressFamily inet
PermitRootLogin no ### 禁止root账号直接登录
SyslogFacility AUTHPRIV
PasswordAuthentication no ### 禁止密码直接登录
ClientAliveInterval = 120
重启sshd服务1
root@ubuntu:~/.ssh$ service sshd restart
4、测试使用ssh-key登录服务器
需要注意的问题:.ssh目录的权限,authorized_key文件的权限,指定的ssh端口是否已经开启