Linux --ssh远程访问及控制
ssh远程管理
1.SSH(Secure Shell)时一种安全通道协议,主要用来实现字符界面的远程登录,复制等功能
2.SSH(Secure Shell)协议对通信双方的数据传输进行了加密处理,安全性更好
配置Openssh服务端
ssh服务及配置文件
服务名称:sshd
服务端主程序目录:/usr/sbin/sshd
服务端配置文件目录:/etc/ssh/sshd_config
客户端配置文件目录:/etc/ssh/ssh_config
// [root@fw ~]# cd /etc/ssh
[root@fw ssh]# ls
moduli ssh_host_ecdsa_key ssh_host_rsa_key
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key.pub
sshd_config ssh_host_ed25519_key
sshd_config.bak ssh_host_ed25519_key.pub
ssh_config ##客户端配置文件
sshd_config ##服务端配置文件;
***vim /etc/ssh/sshd_config***进配置文件
//
37 #LoginGraceTime 2m ##登陆验证时间2分钟
38 #PermitRootLogin yes ##默认允许root登录
40 #MaxAuthTries 6 ##密码输入最大6次,默认3次
41 #MaxSessions 10 ##最大允许10人访问我
65 PasswordAuthentication no ##不允许空密码登录
115 UseDNS no ##no 禁掉反向解析
AllowUsers tom ##白名单:指定用户tom登录,其他人全拒绝,安全性高
DenyUsers tom ##黑名单:指定用户tom不能登录,其他人都允许,安全性低
AllowUsers tom zhangsan@192.168.142.129 允许tom在任何主机登录,zhangsan只能在指定主机登录
;
使用客户端
远程登录
ssh远程登录:通过ssh命令远程登录sshd服务,使用时指定登录用户,目标主机地址作为参数
//
[root@kh ~]# ssh root@192.168.142.129
;
如果端口号改变,则需在ssh后加"-p 端口号"
远程复制
scp远程复制:scp命令可以远程相互复制文件,把服务端文件复制到客户端
//
[root@localhost BT]# scp root@192.168.142.129:/root/test /root/BT/ ##指定服务端地址,文件,复制到自己的地址
root@192.168.142.129's password:
test 100% 0 0.0KB/s 00:00
[root@localhost BT]# ls
test
;
把客户端文件复制到服务端
//
[root@localhost ~]# scp /root/BT/test root@192.168.142.129:/root/bt/ ## 把主机的某个文件传到服务端某个地址;
sftp安全FTP:可以利用ssh安全连接与远程主机上传,下载文件,交互式。
//
sftp tom@192.168.142.129 ## 以FTP模式远程连接
put /etc/passwd ##上传文件
get 1.txt ##下载文件
bye ## 退出登录;
登录验证方式
1.密码验证:用服务器中本地系统用户的用户名和密码进行验证
2.秘钥对验证:要求提供相匹配的秘钥信息才能通过验证,通常是在客户机上创建一对秘钥文件(公钥,私钥),然后将公钥文件放到服务器指定位置。远程登录时,系统将使用公钥,私钥进行加密解密关联验证。
注:对称秘钥:加密解密用同一个秘钥。 如:AES,DES 加密速度快,但是不安全
非对称秘钥:加密解密不同秘钥。分为公钥和私钥 ,不可从一个秘钥推出另一个 。速度慢但是安全
私钥:验证身份,保密的
公钥:所有人都知道,公开的
服务端开启密钥对验证
客户机
//
[root@localhost ~]# 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:sSqSz3I3MGg6P+nsws+UVI6435wbn1A4GyPBGEVItPU root@kh
The key's randomart image is:
+---[RSA 2048]----+
|o+++ |
| .* . |
| o o E . |
| . = . o |
| . = * .S |
| =.= *. | ##生成的密钥
|.+o+.*. |
|+oB++.B . |
| **Bo*.+ |
+----[SHA256]-----+
[root@localhost ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .cache .cshrc .esd_auth initial-setup-ks.cfg .ssh .viminfo 公共 视频 文档 音乐
.. .bash_history .bash_profile BT .config .dbus .ICEauthority .local .tcshrc .Xauthority 模板 图片 下载 桌面
[root@localhost ~]# cd .ssh
[root@localhost .ssh]# ls
id_rsa ##私钥 id_rsa.pub ##公钥 known_hosts
;
//
[root@localhost .ssh]# ssh-copy-id root@192.168.142.129 ###推给服务端公钥
/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.142.129's password:
Number of key(s) added: 1 ##添加了一个
Now try logging into the machine, with: "ssh 'root@192.168.142.129'"
and check to make sure that only the key(s) you wanted were added.
;
公钥推送在了服务端的authorized_keys里
验证客户机能否直接登录服务端
//
[root@kh ~]# ssh root@192.168.142.129
Last login: Wed May 26 16:48:38 2021 ##免密直接登陆成功
;
TCP Wrappers访问控制
TCP Wrappers概述
TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接的特定服务进行安全检测并实现访问控制,凡是包含有libwrap.so库文件的的程序就可以受TCP_Wrappers的安全控制。它的主要功能就是控制谁可以访问。
配置文件
/etc/hosts.allow
/etc/hosts.deny
设置访问控制策略
策略格式:服务程序列表:客户端地址列表
服务程序列表
多个服务以逗号分隔,ALL 表示所有服务
客户端地址列表
多个地址以逗号分隔,ALL表示所有地址
允许使用通配符?和
网段地址,如192.168.4.或者192. 168.4.0/255.255.255.0
区域地址,如.benet.com
策略的应用程序
先检查hosts.allow,找到匹配则允许访问
否则再检查hosts.deny,找到则拒绝访问
若两个文件中均无匹配策略,则默认允许访问
若两个文件中均有匹配策略,则默认允许访问
举例:
// [root@fw .ssh]# vim /etc/hosts.deny
sshd:192.168.142.131
[root@localhost ~]# ssh root@192.168.142.129
ssh_exchange_identification: read: Connection reset by peer 被限制,所以登不上去;