openssh

1. 使用 SSH 访问远程命令行

1.1 OpenSSH

简介OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。​
常见的远程登录工具有:
telnetsshdropbear

telnet      //远程登录协议,23/TCP    认证明文    数据传输明文​
ssh         //Secure SHell,应用层协议,22/TCP    
通信过程及认证过程是加密的,主机认证    
用户认证过程加密    
数据传输过程加密    
dropbear    //嵌入式系统专用的SSH服务器端和客户端工具

1.2 SSH 版本

openssh有两个版本,分别为v1和v2,其特点如下:
​v1:基于CRC-32做MAC,无法防范中间人(man-in-middle)攻击(类似ARP欺骗)
v2:双方主机协议选择安全的MAC方式。基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证

​1.3 SSH 认证方式

openssh有两种认证方式,分别是:​

  1. 基于口令认证
  2. 基于密钥认证

​1.4 openSSH 的工作模式

openSSH是基于C/S架构工作的。

 服务器端    //sshd,配置文件在/etc/ssh/sshd_config
 客户端     //ssh,配置文件在/etc/ssh/ssh_config    
 ssh-keygen      //密钥生成器    
 ssh-copy-id     //将公钥传输至远程服务器    
 scp             //跨主机安全复制工具

1.4 Secure Shell 示例

  1. 以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@mlj123 .ssh]# ssh 192.168.204.129
The authenticity of host '192.168.204.129 (192.168.204.129)' can't be established.
ECDSA key fingerprint is SHA256:9O36r9YzimHLxh8FrdlwdrShNS99tKyPg1G+cBIy81I.
ECDSA key fingerprint is MD5:66:70:6e:90:cd:56:de:21:e6:6a:5b:53:dc:70:6b:9a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.204.129' (ECDSA) to the list of known hosts.
root@192.168.204.129's password: 
Last login: Sun Apr 26 09:54:58 2020 from 192.168.204.1 
[root@mlj ~]# exit 
logout
Connection to 192.168.204.129 closed.
[root@mlj123 .ssh]#
  1. 以其他用户身份(remoteuser)在选定主机(remotehost)上连接到远程shell
[root@mlj .ssh]# ssh tom@192.168.204.150 //tom用户在192.168.204.150主机存在
tom@192.168.204.150's password: 
Last login: Sun Apr 26 18:53:34 2020 from 192.168.204.129
[tom@mlj123 ~]$ exit 
logout
Connection to 192.168.204.150 closed.
You have new mail in /var/spool/mail/root
[root@mlj .ssh]#
  1. 以远程用户身份(remoteuser)在远程主机(remotehost)上通过将输出返回到本地显示器的方式来执行单一命令
[root@mlj ~]# ssh tom@192.168.204.150 'touch abc'
tom@192.168.204.150's password: 
[root@mlj ~]# ssh tom@192.168.204.150 'echo "hello world">/tmp/hehe'
tom@192.168.204.150's password: 
[root@mlj ~]# 
[root@mlj123 ~]# su - tom
Last login: Sun Apr 26 19:02:23 CST 2020 from 192.168.204.129 on pts/2
[tom@mlj123 ~]$ ls
abc
[tom@mlj123 ~]$ cat /tmp/hehe 
hello world
[tom@mlj123 ~]$
  1. w命令可以显示当前登录到计算机的用户列表。这对于显示哪些用户使用ssh从哪些远程位置进行了登录以及执行了何种操作等内容特别有用
[root@localhost ~]# w
 07:49:18 up 18 min,  2 users,  load average: 0.02, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    172.16.12.1      07:31    6.00s  0.02s  0.00s w
user1    pts/1    172.16.12.136    07:49    5.00s  0.00s  0.00s -bash

1.5 SSH 主机密钥

ssh通过公钥加密的方式保持通信安全。当某一ssh客户端连接到ssh服务器时,在该客户端登录之前,服务器会向其发送公钥副本。这可用于为通信渠道设置安全加密,并可验证客户端的服务器。

当用户第一次使用ssh连接到特定服务器时,ssh命令可在用户的/.ssh/known_hosts文件中存储该服务器的公钥。在此之后每当用户进行连接时,客户端都会通过对比/.ssh/known_hosts文件中的服务器条目和服务器发送的公钥,确保从服务器获得相同的公钥。如果公钥不匹配,客户端会假定网络通信已遭劫持或服务器已被入侵,并且中断连接。

这意味着,如果服务器的公钥发生更改(由于硬盘出现故障导致公钥丢失,或者出于某些正当理由替换公钥),用户则需要更新其~/.ssh/known_hosts文件并删除旧的条目才能够进行登录。

  1. 主机ID存储在本地客户端系统上的 ~/.ssh/known_hosts 中
[root@mlj ~]# cat .ssh/known_hosts 
192.168.204.150 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAA
ABBBBeFvXvbVFnsZ9/H55E/2vwAXVGcfl1sP4liDnubMSZJDs15
WCV9PTNe2puxQsKyCfMaSZUEHy2y4bw+OscAIGU=
[root@mlj ~]#
  1. 主机密钥存储在SSH服务器上的 /etc/ssh/ssh_host_key* 中
[root@mlj ~]# ls /etc/ssh/*key*
/etc/ssh/ssh_host_ecdsa_key      /etc/ssh/ssh_host_ed25519_key.pub
/etc/ssh/ssh_host_ecdsa_key.pub  /etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_ed25519_key    /etc/ssh/ssh_host_rsa_key.pub
[root@mlj ~]#

2. 配置基于 SSH 密钥的身份验证(重点)

  1. 用户可通过使用公钥身份验证进行ssh登录身份验证。ssh允许用户使用私钥-公钥方案进行身份验证。这意味着将生成私钥和公钥这两个密钥。私钥文件用作身份验证凭据,像密码一样,必须妥善保管。公钥复制到用户希望登录的系统,用于验证私钥。公钥并不需要保密。拥有公钥的ssh服务器可以发布仅持有您私钥的系统才可解答的问题。因此,可以根据所持有的密钥进行验证。如此一来,就不必在每次访问系统时键入密码,但安全性仍能得到保证。
  2. 使用ssh-keygen命令生成密钥。将会生成私钥/.ssh/id_rsa和公钥/.ssh/id_rsa.pub。
[root@mlj ~]# ssh-keygen -t rsa
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:
SHA256:5PGcoDfeoBUrso5mj+PeFHvLOK15+9V4Tg6SpO9la68 root@mlj
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|        =        |
|       + B .     |
|    o o S +      |
|     = O = o     |
|    +.+ + B +    |
|  +*.=oo =.B     |
| ==oB++++.Eo+    |
+----[SHA256]-----+
[root@mlj ~]# ls .ssh
id_rsa  id_rsa.pub  known_hosts

注意:
生成密钥时,系统将提供指定密码的选项,在访问私钥时必须提供该密码。如果私钥被偷,除颁发者之外的其他任何人很难使用该私钥,因为已使用密码对其进行保护。这样,在攻击者破解并使用私钥前,会有足够的时间生成新的密钥对并删除所有涉及旧密钥的内容。

生成ssh密钥后,密钥将默认存储在家目录下的.ssh/目录中。私钥和公钥的权限就分别为600和644。.ssh目录权限必须是700。

[root@mlj ~]# cd .ssh
[root@mlj .ssh]# ll
total 12
-rw-------. 1 root root 1675 Apr 26 12:04 id_rsa
-rw-r--r--. 1 root root  390 Apr 26 12:04 id_rsa.pub
-rw-r--r--. 1 root root  177 Apr 26 10:41 known_hosts
[root@mlj .ssh]# ll -d
drwx------. 2 root root 57 Apr 26 12:04 .
[root@mlj .ssh]#
  1. 在可以使用基于密钥的身份验证前,需要将公钥复制到目标系统上。可以使用ssh-copy-id完成这一操作
[root@mlj .ssh]# ssh-copy-id root@192.168.204.150
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.204.150's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.204.150'"
and check to make sure that only the key(s) you wanted were added.

[root@mlj .ssh]#
  1. 通过ssh-copy-id将密钥复制到另一系统时,它默认复制~/.ssh/id_rsa.pub文件
    使用 ssh-copy-id 将公钥复制到远程系统上的正确位置
[root@localhost ~]# ls .ssh/
id_rsa  id_rsa.pub
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.12.138
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '172.16.12.138 (172.16.12.138)' can't be established.
ECDSA key fingerprint is SHA256:JK5WwrX8hynl3dyWO43e6+lcs6zn9oZn74z1H5X8F90.
ECDSA key fingerprint is MD5:01:4f:4f:4b:0e:45:a9:10:bb:d0:c0:dd:19:9a:9f:96.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.16.12.138's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.16.12.138'"
and check to make sure that only the key(s) you wanted were added.
  1. 使用 ssh 命令无命令登录远程主机
[root@mlj .ssh]# ssh root@192.168.204.150
Last login: Sun Apr 26 18:54:50 2020 from 192.168.204.129
[root@mlj123 ~]#
  1. 使用 scp 命令传送文件到远程主机
1.生成密钥
[root@mlj123 .ssh]# ssh-keygen -t rsa 
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:
SHA256:no/SxIuZfoOBertscip2ocYLWizxKZPuIUHG408KMX8 root@mlj123
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|.                |
|o=               |
|++.              |
|+...E. .S        |
|.*++. ..o.       |
|Oo*o.  Oo.       |
|*X+o+ * =o       |
|==+*++.o...      |
+----[SHA256]-----+
[root@mlj123 .ssh]# 
[root@mlj123 .ssh]# ll
total 16
-rw-------. 1 root root  390 Apr 26 22:52 authorized_keys
-rw-------. 1 root root 1679 Apr 26 23:10 id_rsa //私钥文件
-rw-r--r--. 1 root root  393 Apr 26 23:10 id_rsa.pub//公钥文件
-rw-r--r--. 1 root root  354 Apr 26 18:55 known_hosts
[root@mlj123 .ssh]#

2.把公钥文件传给192.168.204.129主机
[root@mlj123 .ssh]# scp id_rsa.pub root@192.168.204.129:/root/.ssh/authorized_keys / //把公钥放在.ssh/authorized_keys /
root@192.168.204.129's password: 
id_rsa.pub                         100%  393    93.4KB/s   00:00    
[root@mlj123 .ssh]#

3.查看公钥文件
[root@mlj123 .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDO7MgP2KGWllo
HW57r8kjeP0alVEAD7Hqh5zhBGj8cVu9P8caUN7E4nIfbeQOsGqvZ8/
HBKKk9GjLc2v9z4f4mHanuydgvcbWrDEbu4gl3CerfjtL/BMa7f7AxBVKq
OFDtDYYmUW75+lIP4Ns1WSfvhbNfKHy1noZ2YCESC0A/VjJJZxnrrml1
XDAikl0n/J2G7XYmuERFJ55BO2QjyWsYtUqDcpWA/V80lanmHLDbjnCQ
KImheHllfTpAJ2MfZ6c0jd1yRQWRMKYase4HTxBrwXZ2GTRiXmiv288QC
oYazfpYDYIq+gff+qLwodqHJlK9/UIKyVqzU1E0ttUCXl3Z root@mlj123
[root@mlj123 .ssh]#

[root@mlj .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@mlj .ssh]# cat authorized_keys //查看传过来的公钥文件内容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDO7MgP2KGWl
loHW57r8kjeP0alVEAD7Hqh5zhBGj8cVu9P8caUN7E4nIfbeQOsGqv
Z8/HBKKk9GjLc2v9z4f4mHanuydgvcbWrDEbu4gl3CerfjtL/BMa7f7Ax
BVKqOFDtDYYmUW75+lIP4Ns1WSfvhbNfKHy1noZ2YCESC0A/VjJJ
Zxnrrml1XDAikl0n/J2G7XYmuERFJ55BO2QjyWsYtUqDcpWA/V80lan
mHLDbjnCQKImheHllfTpAJ2MfZ6c0jd1yRQWRMKYase4HTxBrwXZ2
GTRiXmiv288QCoYazfpYDYIq+gff+qLwodqHJlK9/UIKyVqzU1E0ttUC
Xl3Z root@mlj123
[root@mlj .ssh]# ll
total 16
-rw-r--r--. 1 root root  393 Apr 26 15:12 authorized_keys

4.在192.169.204.129改authorized_keys权限改为600
[root@mlj .ssh]# chmod 600 authorized_keys 
[root@mlj .ssh]# ll
total 16
-rw-------. 1 root root  393 Apr 26 15:12 authorized_keys

验证配置,这时候不需要密码即可登录192.168.204.129主机

[root@mlj123 .ssh]# ssh root@192.168.204.129
Last login: Sun Apr 26 10:55:37 2020 from 192.168.204.150
[root@mlj ~]#

scp命令常用选项

    -r      //递归复制
    -P      //保持权限
    -p      //端口
    -q      //静默模式,所有操作不显示过程
    -a      //全部复制

把远程主机上的abc文件放到本地主机

[root@mlj123 ~]# cat abc
hello world
[root@mlj123 ~]# 
[root@mlj ~]# scp root@192.168.204.150:/root/abc . //.表示当前目录
abc                                100%   12     2.1KB/s   00:00    
[root@mlj ~]# cat abc
hello world
[root@mlj ~]#

3. 自定义 SSH 服务配置

虽然OpenSSH服务器通常无需修改,但会提供其他安全措施,可以在配置文件/etc/ssh/sshd_config中修改OpenSSH服务器的各个方面。

PermitRootLogin {yes|no}    //是否允许root用户远程登录系统
PermitRootLogin without-password    //仅允许root用户基于密钥方式远程登录
PasswordAuthentication {yes|no}     //是否启用密码身份验证,默认开启

4. SSH 安全注意事项

密码应该经常换且足够复杂
[root@localhost ~]#  tr -dc A-Za-z0-9_ < /dev/urandom | head -c 30 |xargs   //生成30位的密码
LYH9cbirdT6E_hbColMFjZNf9Kd6If

[root@localhost ~]# openssl rand 20 -base64
Di9ry+dyV40xVvBHirsc3XpBOzg=    //生成20位随机密码
  • 使用非默认端口
  • 限制登录客户端地址
  • 仅监听特定的IP地址
  • 禁止管理员直接登录
  • 仅允许有限制用户登录
    AllowUsers,AllowGroups
  • 使用基于密钥的认证
  • 禁止使用空密码
  • 禁止使用SSHv1版本
  • 设定空闲会话超时时长
  • 利用防火墙设置
  • ssh访问策略限制ssh的访问频度和并发在线数
  • 做好日志的备份,经常分析(集中于某台服务器)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值