openssh
1. openssh ####
当主机中开启openssh服务,那么就对外开放了远程连接的接口
#openssh服务的服务端:sshd
#openssh服务的客户端:ssh
2 2.1在客户端连接sshd的方式###
ssh 服务端用户@服务端ip地址
首先打开server和desktop,一个作为客户端一个作为服务端:
2.2 ip地址的查看和修改:
ip地址的查看:ifconfig inet后面的内容即为ip地址
ip地址的修改指令:nm-connection-editor 选择以太网,将Device MAC address设置为eth0
IPV4设置为Manual,在address输入新设置的ip地址,Network设置为24,回车保存
2.3 连接:
[[email protected] ~]# ssh [email protected]
The authenticity of host '172.25.254.200 (172.25.254.200)' can't beestablished.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? ##当当前主机第一次连接陌生主机时会自动建 立.ssh/know_hosts 这个文中记录的是连接过的主机信息
[email protected]'s password: #输入密码连接成功
Last login: Fri Mar 30 02:05:52 2018 from 172.25.254.100
[[email protected] ~]# exit #表示退出当前连接
logout
Connection to 172.25.254.200 closed.
从245连接145,输入密码连接后,可在145桌面上建立文件file1:
"注意:以上连接方式是不能打开远程主机的图形功能的如果需要打开远程主机图形功能需要输入 -X":ssh -X [email protected]
3. 给ssh服务添加新的认证方式 KEY认证####
3.1.生成锁和钥匙
[[email protected] ~]# ssh-****** ##生成**的命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #保存加密字符的文件用默认
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #可以为空,如果想为空必须大于4位
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:
00:4f:b0:72:40:9f:a6:fb:91:86:d2:69:bc:7c:75:5f [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| .o o.. |
| o * |
| . * o |
| = . |
| . S |
| o + .. . E |
|. B +. . . . |
| + +.. . |
| o.. |
+-----------------+
如图:
3.2 加密ssh用户的认证
#在服务端:ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] ##将锁给服务端
ssh-copy-id ##加密命令
-i ##指定**
/root/.ssh/id_rsa.pub ##**
root ##加密用户
172.25.254.200 ##主机ip
在客户端:
解密文件传输到客户端: scp /root/.ssh/id_rsa [email protected]:/root/.ssh/
之后在客户端的/root/.ssh目录中可以查看到id_rsa这个文件,再次连接服务端时不需要密码,连接服务端后可在服务端桌 面建立文件file2表示连接成功:
* 想要客户端在有**的情况下仍无法连接服务端,可删除服务端的authorized_keys文件:
rm -fr /root/.ssh/authorized_keys ##当此文件被删除,客户端解密文件失效,但仍可以通过输入服务端的密码进行连接:
authorized_keys文件的恢复:
cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys ##从新生成锁文件,解密文件功能恢复
* 想要客户端在有**和服务端密码的情况下仍无法连接服务端,可以编辑/etc/ssh/sshd_config文件,使客户端在连接时不进行输入密码的提示。将78行的yes改为no
设置完成后保存退出,需要使用指令systemctl restart sshd重启服务。再次连接是会显示请求被拒绝,即客户端连接失败。
4. sshd的安全配置 ####
禁止原始认证方式
PasswordAuthentication no|yes ##开启或关闭ssh的默认认证方式
PermitRootLogin no|yes ##开启或关闭root用户的登陆权限
AllowUsers westos ##用户白名单,当前设定是只允许westos登陆
DenyUsers linux ##用户黑名单,当前设定是只不允许linux登陆
同样是编辑文件/etc/ssh/sshd_config,在空白行写入AllowUsers westos,DenyUsers linux,表示westos用户可以接入而 linux用户不可以。
编辑文件之后保存退出,使用指令systemctl restart sshd重启服务,切换到用户连接时,白名单连接成功而黑名单连接失败
5. linux中服务的管理 #####
systemctl 动作 服务
systemctl start sshd #开启服务
systemctl stop sshd #停止服务
systemctl status sshd #查看服务状态
systemctl restart sshd #重启服务
systemctl reload sshd #让服务从新加载配置
systemctl enable sshd #设定服务开启启动
systemctl disable sshd #设定服务开机不启动