Linux之远程登录服务
Linux下配置virt虚拟机网卡(主机网桥搭建)
主机本地
1.进入network-scripts目录
[root@localhost mnt]# cd /etc/sysconfig/network-scripts/
有ifcfg-enp4s0f1文件,作备份,没有就说明你没有以太网卡,去搞一块。
[root@localhost network-scripts]# ll
total 12
-rw-r--r--. 1 root root 402 Jan 22 09:53 ifcfg-ChinaNet-2lou
-rw-r--r--. 1 root root 55 Jan 26 06:14 ifcfg-enp4s0f1
-rw-------. 1 root root 22 Jan 22 11:07 keys-ChinaNet-2lou
[root@localhost network-scripts]#
[root@localhost network-scripts]# vim ifcfg-enp4s0f1
vim ifcfg-enp4s0f1文件,写如下内容
1 BRIDGE=br0
2 DEVICE=enp4s0f1
3 ONBOOT=yes
4 BOOTPROTO=none
~
ifconfig查看本机ip,找一个正常一点的地址:
查看ifcfg-br0,有就备份一下,没有就再touch一个ifcfg-br0,
[root@localhost network-scripts]# vim ifcfg-br0
vim ifcfg-br0如下
DEVICE=br0
BOOTPROTO=none
IPADDR=172.25.254.26 ##根据自己实际enp0s25情况写
PREFIX=24
ONBOOT=yes
TYPE=Bridge
~
reboot重启 注:系统重启,保存一下重要文件。
[root@localhost network-scripts]# reboot
重启之后,
[root@localhost network-scripts]# bridge link
[root@localhost network-scripts]# ifconfig
打开virt-manager ##没有的话,需要自己安装一个哈
[root@westos_student26 network-scripts]# virt-manager
打开之后,弹出窗口
双击之后,
然后
本地主机我们已经配置好啦。接下来我们需要在虚拟机里配置。开机之后
然后
虚拟机里打开终端,
按文中操作:
『实用』Linux之图形方式设定IP
到这里,实验环境配置完成。
一、Openssh的功能
1.sshd服务的用途
作用: 可以实现通过网络在远程主机中开启安全shell的操作
Secure SHell 客户端ssh
Secure SHell daemon 服务端sshd
2.安装包
openssh-server
3.主配置文件
/etc/ssh/sshd_conf
4.默认端口:22
ssh -l [-l 远程主机用户] <ip | hostname>
ssh远程连接
[root@westos_student26 network-scripts]# ssh -l root 172.25.254.126
##通过ssh命令在126主机以root身份开启远程shell连接
[root@westoslinux ssh_config.d]# cd /etc/ssh
[root@westoslinux ssh]# cat ssh_host_ecdsa_key.pub
【本地root】在本地重新打开一个终端
[root@westos_student26 Desktop]# cd ~/.ssh/
[root@westos_student26 .ssh]# ll
[root@westos_student26 .ssh]# cat known_hosts
【远程root】
[root@westoslinux ssh]# rm *key* -rf ##注意:在/etc/ssh目录下
##删除所有带key文件。
[root@westoslinux ssh]# systemctl restart sshd
【本地root】
[root@westos_student26 .ssh]# ssh -l root 172.25.254.126
报错
【本地root】
[root@westos_student26 .ssh]# vim /root/.ssh/known_hosts
【本地root】
[root@westos_student26 .ssh]# ssh -l root 172.25.254.126
输入yes
成功登陆。
ssh -X
开启图形
ssh -i
指定私钥
ssh -l
指定登陆用户
ssh -f
后台运行
ssh -o
指定参数连接
ssh -l root 172.25.254.126 -o "StrictHostKeyChecking=no" ##首次连接不需要输入yes
ssh -t
指定连接跳板
ssh -l root 172.25.254.126 -t ssh -l root 172.25.254.126
失败
【远程root】删除相关失败文件内容
【本地root】
ssh -l root 172.25.254.126 -t ssh -l root 172.25.254.126
跳板登陆成功。
ssh -o
指定参数连接
ssh -l root 172.25.254.126 -o "StrictHostKeyChecking=no" ##首次连接不需要输入yes
【远程root】重置服务端ssh的key文件,重启sshd服务
【本地root】删除相关失败文件内容
【本地root】
[root@westos_student26 .ssh]# ssh -l root 172.25.254.126 -o "StrictHostKeyChecking=no"
不输入yes 登陆成功。
三、sshd key 认证
1.认证类型
对称加密:
加密和解密是同一字符串
缺点:容易泄漏、可暴力破解、容易遗忘。
非对称加密:
加密用公钥,解密用私钥。
优点:不会被盗用,攻击者无法通过无密钥方式登陆服务器。
2.生成非对称加密密钥
方法一:ssh-keygen
『本地root』
[root@westos_student26 .ssh]# 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:
SHA256:Cy1dplX6adSMg/cz+8fA4tNpBbihcJPtXnV5u6xAq/4 root@westos_student26.westos.org
The key's randomart image is:
+---[RSA 3072]----+
| . |
| + + |
| *o=.o .|
| o.=++++..+|
| o So =+++o+|
| o .o.= +=.|
| . = +o=.|
| . = ++o|
| .o.E +. o|
+----[SHA256]-----+
[root@westos_student26 .ssh]#
进入~/.ssh文件夹
[root@westos_student26 .ssh]# cd ~/.ssh/
[root@westos_student26 .ssh]# ll
total 16
-rw-------. 1 root root 580 Dec 25 00:13 authorized_keys
-rw-------. 1 root root 2622 Jan 20 13:03 id_rsa ##私钥
-rw-r--r--. 1 root root 586 Jan 20 13:03 id_rsa.pub ##公钥
-rw-r--r--. 1 root root 351 Jan 19 16:58 known_hosts
[root@westos_student26 444]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.25.254.126
『远程主机root』
[root@westoslinux .ssh]# cd ~/.ssh/
[root@westoslinux .ssh]# ll
进入~/.ssh目录
看到authorized_keys文件,远程主机免密授权文件设置成功
『本地root』
进入~/.ssh目录
[root@westos_student26 444]# cd ~/.ssh/
[root@westos_student26 Desktop]# ssh -l root 172.25.254.126
免密登录成功
为什么还能免密登录??????
『远程root』
『本地root』
[root@westos_student26 Desktop]# ssh -l root 172.25.254.126
登录再次要求输入密码
『远程root』
『本地root』
[root@westos_student26 Desktop]# ssh -l root 172.25.254.126
免密登录成功
四、sshd安全优化参数详解
[root@node126 ~]# systemctl disable --now firewalld
关闭防火墙
port 2222
设定端口为2222
PermitRootLogin yes|no
对超级用户登陆是否禁止
『远程root』
[root@node126 ~]# vim /etc/ssh/sshd_config
PermitRootLogin改为no,
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』
『远程root』
[root@node126 ~]# vim /etc/ssh/sshd_config
PermitRootLogin改为yes,
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』
root登录成功
PasswordAuthentication yes|no
是否开启原始密码认证方式
『远程root』
[root@node126 ~]# vim /etc/ssh/sshd_config
PasswordAuthentication改为no,
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』
Permission denied
『远程root』
[root@node126 ~]# vim /etc/ssh/sshd_config
PasswordAuthentication改为yes,
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』
登陆成功。
AllowUsers lee
用户白名单
『远程root』
[root@node126 ~]# vim /etc/ssh/sshd_config
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』
DenyUsers lee
用户黑名单
『远程root』
AllowUsers westos
修改为DenyUsers westos
[root@node126 ~]# systemctl restart sshd ##重启sshd
然后
『本地root』