文章目录
一 、openSSH服务器远程访问
1、 ssh协议
为客户机提供安全的shell环境,用于远程管理
默认端口:TCP 22
openssh
服务名称 :sshd
服务端主程序: /usr/sbin/sshd
服务端配置文件: /etc/ssh/sshd_config
第二种远程访问:telnet 也是远程访问,是不会经过加密的明文传输,可以用抓包工具直接获取其中的消息 23 tcp 端口;而ssh 是典型的密文访问
因此tlelent 主要应用于局域网;ssh皆可
ssh_config 是针对客户端的
shhd_config是服务端
2、 服务监听选项
端口号、协议版本、监听IP地址
禁用反向解析
[root@localhost ~]# vim /etc/ssh/sshd_config
......
#Port 22 '端口号(可以修改)'
#ListenAddress 0.0.0.0 '监听地址'
Protocol 2 '版本'
#UseDNS no 'DNS反向解析 否'
控制
禁止ROOt用户、空密码用户 ————用户层面的控制
登录时间、重试次数 ————用户属性的控制
AllowUsers、DenyUsers ————使用白黑名单控制
AllowUsers 白名单 :仅允许登陆
DenyUsers 黑名单 :仅拒绝登陆
[root@localhost ~]# vim /etc/ssh/sshd_config
#LoginGraceTime 2m '登录时间2min 超过2min自动注销'
#PermitRootLogin yes '允许root登陆 是 前面有#符号注释则不可以使用'
#StrictModes yes '严格遵循标准模式 是'
#MaxAuthTries 6 '最大尝试登陆次数为 6'
#MaxSessions 10 '最大创建会话为 10'
PermitEmptPasswords no '允许空密码登陆 否'
······
AllowUsers jerry admin@61.23.24.25
'白名单,只允许以下用从指定终端登录,用户与用户之间用空格隔开'
3 登陆验证
密码验证: 核对用户名、密码是否匹配
密钥对验证: 核对客户的私钥、服务端公钥是否匹配
密钥对的方式需要自己去创建
密钥对里面包含公钥和私钥,合在一起叫密钥对
公钥给对方,私钥自己保留,这种方式叫做非对称密钥 rsa 相当于虎符
des 或aex或3des模式 是对称密钥,相当于门钥匙
[root@localhost ~]# vim /etc/ssh/sshd_config
······
#PubkeyAuthentication yes '密钥对验证开启 是'
#PasswordAuthentication yes '身份密码验证 是'
AuthorizedKeysFile .ssh/authorized_keys '密钥对公钥库文件路径'
二 、 使用SSH客户端程序
1、 ssh命令 ————远程安全登录
ssh user@host
ssh 被连接的主机的本地用户名@主机名
选项 -p 即 指定端口号
test01 的ip地址为192.168.139.128
test02的ip地址为192.168.139.129
[root@test01 ~]# cd /etc/ssh '切换到/etc/ssh目录下'
[root@test01 ssh]# ls
moduli ssh_host_ecdsa_key ssh_host_ed25519_key.pub
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key
sshd_config ssh_host_ed25519_key ssh_host_rsa_key.pub
[root@test01 ssh]# vim sshd_config '编辑服务端配置文件'
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
16 #
17 Port 22 '端口号22,去掉#启用'
18 #AddressFamily any
19 #ListenAddress 0.0.0.0
20 #ListenAddress ::
21
22 HostKey /etc/ssh/ssh_host_rsa_key
23 #HostKey /etc/ssh/ssh_host_dsa_key
24 HostKey /etc/ssh/ssh_host_ecdsa_key
25 HostKey /etc/ssh/ssh_host_ed25519_key
26
[root@test01 ssh]# systemctl restart sshd '重启sshd服务'
[root@test02 ~]# ssh root@192.168.139.128 '连接开启远程服务的主机,以root身份登陆'
The authenticity of host '192.168.139.128 (192.168.139.128)' can't be established.
ECDSA'指密钥对' key fingerprint is SHA256:dXWxtS2ShXQgfb7R672V7+l3i7rGqHBbIB5MTcFnAws.
ECDSA'指密钥对' key fingerprint is MD5:59:fb:20:f0:28:96:5e:14:90:82:63:c9:ae:67:d6:e9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.139.128' (ECDSA) to the list of known hosts.
root@192.168.139.128's password:
Last login: Wed Nov 20 17:13:57 2019
[root@test01 ~]# '注意主机名,此时已经远程登陆成功'
更改主配置文件,取消root用户,
[root@test01 ssh]# vim /etc/ssh/sshd_config '配置28的sshd的服务端配置文件'
38 PermitRootLogin no '第38行取消注释符,root登陆否'
[root@test01 ssh]# systemctl restart sshd '重启ssh服务以生效配置'
[root@test02 ~]# ssh root@192.168.139.128 '去连28,使用root身份,'
The authenticity of host '192.168.139.128 (192.168.139.128)' can't be established.
ECDSA key fingerprint is SHA256:dXWxtS2ShXQgfb7R672V7+l3i7rGqHBbIB5MTcFnAws.
ECDSA key fingerprint is MD5:59:fb:20:f0:28:96:5e:14:90:82:63:c9:ae:67:d6:e9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.139.128' (ECDSA) to the list of known hosts.
root@192.168.139.128's password:
Permission denied, please try again. '拒绝权限,请重试'
root@192.168.139.128's password:
[root@test02 ~]#
[root@test02 ~]# ssh zhuzhu@192.168.139.128 '使用zhuzhu身份去连接,没问题'
zhuzhu@192.168.139.128's password:
Last login: Wed Nov 20 18:08:14 2019 from 192.168.139.129
[zhuzhu@test01 ~]$
[zhuzhu@test01 ~]$ su - root '然后su切换到root'
Password:
Last login: Wed Nov 20 18:30:29 CST 2019 on pts/5
Last failed login: Wed Nov 20 18:32:37 CST 2019 on pts/5
There was 1 failed login attempt since the last successful login.
[root@test01 ~]# '成功'
好像这个改的没什么用,不允许以root什么登录进来,可以用其他人的身份登录进来,再su 切换,貌似有点脱裤子放屁的感觉~
为了防止上述情况产生,就运用到之前整理的知识,加入wheel组启动pam认证模块,需要深入了解的可以参考小编之前的博文
[root@test01 ssh]# vim /etc/pam.d/su '编辑对应的配置文件'
6 auth required pam_wheel.so use_uid '取消注释,启用pam.d的su功能'
[root@test01 ssh]# gpasswd -a zhuzhu '将猪猪用户加入到wheel组中,只有猪猪可以切换root'
[root@test01 ssh]# useradd lisi '新创建用户lisi,该用户不在wheel组内'
[root@test01 ssh]# passwd lisi
Changing password for user lisi.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@test02 ~]# ssh lisi@192.168.139.128 '用29去链接28'
lisi@192.168.139.128's password:
[lisi@test02 ~]$
[lisi@test02 ~]$ 'lisi登陆成功'
[lisi@test02 ~]$ su - root 'su 切换root'
密码:
su: 拒绝权限 '失败'
白名单
[root@test01 ssh]# vim /etc/ssh/sshd_config '给28配置sshd_config'
21 AllowUsers zhuzhu '手动添加白名单,即只允许zhuzhu登陆'
[root@test01 ssh]# systemctl restart sshd '重启sshd服务'
[root@test02 ~]# ssh zhuzhu@192.168.139.128 '以zhuzhu身份,29远程28'
zhuzhu@192.168.139.128's password:
Last failed login: Wed Nov 20 18:51:43 CST 2019 from 192.168.139.129 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Nov 20 18:48:54 2019 from 192.168.139.128
[zhuzhu@test01 ~]$ logout '登陆成功,然后退出'
Connection to 192.168.139.128 closed.
[root@test02 ~]# ssh root@192.168.139.128 '以root身份29去远程28'
root@192.168.139.128's password: '输入密码'
Permission denied, please try again. '权限拒绝,请重试'
root@192.168.139.128's password:
Permission denied, please try again.
2 、scp命令 ———— 远程安全复制
scp user@host:file 1 file2
复制目标主机下面的file文件到自己的file2下
scp file1 user@host:file2
复制自己的file1下的文件到目标主机的file1下
具体操作如下
[root@test01 ssh]# scp -r /etc/hosts root@192.168.139.129:etc/hosts
'此时在28上,scp复制 本地 /etc/hosts文件 到29:/etc/hosts'
The authenticity of host '192.168.139.129 (192.168.139.129)' can't be established.
ECDSA key fingerprint is SHA256:+uy+1TNy69jB97B7+AoYqhNEaBi42DuOYb0oE4pJ8s0.
ECDSA key fingerprint is MD5:00:78:0c:c1:c2:7b:01:45:7c:31:c2:3b:53:4d:5c:10.
Are you sure you want to continue connecting (yes/no)? yes '询问是否连接,选择是'
Warning: Permanently added '192.168.139.129' (ECDSA) to the list of known hosts.
root@192.168.139.129's password:
hosts 100% 204 87.9KB/s 00:00 '显示进度'
[root@test01 ssh]#
[root@test01 ssh]# ssh root@192.168.139.129 '以root身份28远程29'
root@192.168.139.129's password:
Last login: Wed Nov 20 19:18:41 2019 from 192.168.139.129
[root@test02 ~]# cat /etc/hosts '查看29下的/etc/hosts文件'
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
test01 192.168.139.128
test02 192.168.139.129
[root@test02 ~]# exit '注销登陆'
Connection to 192.168.139.129 closed.
[root@test01 ssh]# cd /opt/
[root@test01 opt]# ls
rh
[root@test01 opt]# touch abc.txt '在本地创建空文件'
[root@test01 opt]# scp /opt/abc.txt root@192.168.139.129:/home/
'把本地的/opt/abc.txt文件 以root身份 复制到192.168.139.129主机的/home/目录下'
root@192.168.139.129's password: '密码确认'
abc.txt 100% 0 0.0KB/s 00:00
[root@test01 opt]#
3 、sftp命令 ————安全FTP上下载
sftp user@host
进入到目标主机的sftp模式
具体操作如下
[root@test01 ~]# sftp root@192.168.139.129 'ftp上下载方式连接29'
root@192.168.139.129's password:
Connected to 192.168.139.129.
sftp>
sftp> ls -a '进入到对方home目录下'
. .. .ICEauthority
.Xauthority .bash_history .bash_logout
.bash_profile .bashrc .cache
.config .cshrc .dbus
.esd_auth .local .mozilla
.ssh .tcshrc .viminfo
anaconda-ks.cfg initial-setup-ks.cfg 下载
公共 图片 文档
桌面 模板 视频
音乐
sftp> cd /opt '可以随意切换目录'
sftp> ls
rh
sftp> touch aaa '在opt目录下创建 aaa'
sftp> ls
aaa rh
sftp> rm -f aaa '也可以删除'
对于生产环境下,这样的登录方式是非常不安全的,客户机可以远程对服务器的文件进行更改删除是很危险的,下面对这个问题进行进一步解决插入一个秘钥知识!
三 : 构建密钥对验证的SSH体系
1. 在客户端生成密钥对
公钥文件:id_rsa
公钥文件:id_rsa.pub
第一步 在客户端重新建立一个用户zhuzhu
在客户机中创建密钥对
ssh-keygen命令
可用的加密算法:RSA或DSA
[root@test02 ~]# ssh-keygen -t ecdsa '生成公共/私有ecdsa密钥对'
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa): '输入保存秘钥的文件'
Enter passphrase (empty for no passphrase): '输入密码'
Enter same passphrase again:
Your identification has been saved in .2
Your public key has been saved in .pub.
The key fingerprint is:
SHA256:R/ubZAgOklPma+jgqYqQdeIsz7xMnJyCMZZEqj2idu8 root@55
The key's randomart image is:
+---[ECDSA 256]---+
| . |
|o |
|.. o . |
|o.. = . . |
|=o= = o S o |
|+@ * + + o o |
|B.X.. o . . + |
|+X.+.. o o |
|=.O.oE o |
+----[SHA256]-----+
第二步:导入公钥信息
将公钥文本添加到目标用户的公钥库
[zhuzhu@1 .ssh]$ ssh-copy-id -i id_ecdsa.pub niuniu@192.168.139.128
第三步.进入到服务端的home下的niuniu用户
下面多了一个秘钥文件
[niuniu@server .ssh]$ ls
authorized_keys
第四步:
用客户端的猪猪登录服务端的牛牛
会发现每次登录都要输入密码
会很麻烦
第五步 设置免密登录
[zhuzhu@1 .ssh]$ ssh-agent bash
[zhuzhu@1 .ssh]$ ssh-add