一、ssh远程管理
1. ssh概述
ssh(secure shell)协议
- ssh是一种安全通道协议,主要用来实现字符界面的远程登录,远程复制等功能。
- ssh协议对通信双方的数据传输进行了加密处理,其中包括用户登陆时输入的口令。
openssh
openssh是实现ssh协议的开源软件项目,适用于各种unix、linux操作系统。
- 服务名称:sshd
- 默认端口号:22
- 服务端主程序:/usr/sbin/sshd
- 服务端配置文件:/etc/ssh/sshd_config
- 客户端配置文件:/etc/ssh/ssh_config
2.ssh的优点
- ssh是较可靠,专为远程登录会话和其他网络服务提供安全性的协议
- 利用ssh协议可以有效防止远程管理过程中的信息泄露问题
- ssh最初是unix系统中的一个程序,后来又迅速扩展到其他操作平台
- ssh在正确使用时可弥补网络中的漏洞
- 数据传输的过程是加密的,具有安全稳定的特点
- 数据传输的方式是压缩的,可以提高传输速度
3.配置openssh客户端
- shd_config配置文件的常用选项设置
vi /etc/ssh/sshd_config ...... port 22 #监听端口为 22 listenaddress 0.0.0.0 #监听地址为任意网段,也可以指定openssh服务器的具体ip logingracetime 2m #登录验证时间为 2 分钟 permitrootlogin no #禁止 root 用户登录 maxauthtries 6 #最大重试次数为 6 permitemptypasswords no #禁止空密码用户登录 usedns no #禁用 dns 反向解析,以提高服务器的响应速度 allowusers zhangsan lisi wangwu@61.23.24.25 #只允许zhangsan、lisi、wangwu用户登录,且其中wangwu用户仅能够从ip地址为61.23.24.25的主机远程登录 denyusers zhangsan #禁止某些用户登录,用法于allowusers类似(注意不要同时使用)
4.sshd服务支持的两种验证方式
密码验证
- 对服务器中本地系统用户的登录名称、密码进行验证。
- 简便,但可能会被暴力/破解
密钥对验证
- 有球提供相匹配的密钥信息才能通过验证。
- 通常先在客户端中创建一对密钥文件(公钥、私钥),然后将公钥文件放到服务器中的指定位置。
- 远程登录时,系统将使用公钥、私钥进行加密\解密关联验证。
- 能够增强安全性,且可以免交互登录
5. 使用ssh客户端程序
5.1 ssh - 远程登录
ssh "选项" "用户名"@"ip\名称" 例: ssh -p 1022 zhangsan@20.0.0.13 # "-p"指定端口号
5.2 scp - 远程复制
- 下行复制:
scp "用户名"@"ip/名称":"被复制的远程主机文件路径" "复制到的本机文件路径" 例:scp root@20.0.0.13:/etc/passwd /root/passwd10.txt
- 上行复制:
scp "被复制的本机文件路径" "用户名"@"ip\名称":"复制到的远程主机文件路径" 例:scp /root/passwd root@20.0.0.13:/etc/passwd10.txt
- "-r"选项:递归复制目录
- "-p"选项:指定端口号
6.配置密钥对验证
1.在客户端使用ssh-keygen工具创建密钥对文件(可用的加密算法为rsa、ecdsa、dsa等)
ssh-kegen -t rsa #"-t"选项:指定加密算法类型
[root@wzx ssh]# ssh-keygen -t rsa 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): #设置私钥密码(回车默认) 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:gqxmgbqbombyseldedinramgvv17fjrxb7fq0nhgl5o root@wzx the key's randomart image is: +---[rsa 2048]----+ |b .. .o+| |=o.+. . o=| |o.o... . +..| | o. +.o e . | |*.o =.s . | |=. . oo . . | |. .. o .. . o| | . +. . o | | o. o. | +----[sha256]-----+
- 生成的私钥、公钥文件默认存放在宿主目录中的隐藏目录.ssh/下
2.将公钥文件上传至服务器
ssh-copy-id root@20.0.0.12 "-i"选项:指定密钥文件
[root@wzx ssh]# ssh-copy-id root@20.0.0.12 /usr/bin/ssh-copy-id: info: source of key(s) to be installed: "/root/.ssh/id_rsa.pub" the authenticity of host '20.0.0.12 (20.0.0.12)' can't be established. ecdsa key fingerprint is sha256:1r40ytt7dynxgxab8w4tabvlpcu9038llvjdrqqnpna. ecdsa key fingerprint is md5:03:ae:25:33:8f:08:48:80:85:d4:3a:73:f2:49:83:a4. 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@20.0.0.12's password: number of key(s) added: 1 now try logging into the machine, with: "ssh 'root@20.0.0.12'" and check to make sure that only the key(s) you wanted were added.
3.使用密钥对验证登录
ssh root@20.0.0.12
[root@wzx ssh]# ssh root@20.0.0.12 last login: mon aug 19 23:47:41 2024 from 20.0.0.1
二、tcp wrappers访问控制
1.tcp wrappers 概述
- tcp wrapper 机制的保护对象为各种网络服务程序,针对访问服务的客户端地址进行访问控制。
- 对应的两个策略文件为:/etc/hosts.allow 和 /etc/hosts.deny ,分别用来设置允许和拒绝的策略。
格式:
"服务程序列表":"客户端地址列表"
(1) 服务程序列表:
- all:代表所有的服务
- 单个服务程序:如"vsftpd"
- 多个服务程序组成的列表:如"vsftpd.sshd"
(2) 客户端地址列表:
- all:代表任何客户端地址
- local:代表本机地址
- 多个地址以逗号分隔
- 允许使用通配符"*"、"?",
- 网段地址,如 192.168.20. 或 192.168.20.0/255.255.255.0
- 区域地址,如".benet.com"
2. tcp wrappers 机制的基本原则
首先检查/etc/hosts.allow(白名单)文件,如果找到相匹配的策略,则允许访问;
否则继续检查/etc/hosts.deny(黑名单)文件,如果找到相匹配的策略,则拒绝访问;
如果检查上述两个文件都找不到相匹配的策略,则运行访问。
- 允许所有,拒绝个别:
只需在/etc/hosts.deny文件中添加相应的拒绝策略
- 允许个别,拒绝所有:
除了在/etc/hosts.allow中添加允许策略之外,还需要再/etc/hosts.deny文件中设置"all:all"的拒绝策略。
三、对称密钥与非对称密钥
1.对称密钥(symmetric key)
对称密钥加密,又称为私钥加密或会话密钥加密算法,是一种加密和解密使用相同密钥或可以相互推算的密钥对的加密方式。
优点:
- 加解密速度快,适合处理大量数据
缺点:
- 密钥管理困难,泄露风险大
2.非对称密钥(asymmetric key)
非对称密钥加密,又称为公钥密钥加密,它需要使用一对密钥来分别完成加密和解密操作。这对密钥包括一个公钥和一个私钥,公钥可以公开发布,而私钥则由用户自己秘密保存。
优点:
- 泄露风险低
缺点:
- 加解密速度慢
四、在终端中使用私钥文件登录服务器
1.在服务端中生成密钥
[root@wzx ~]# ssh-keygen -t rsa 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): 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:xfbf6zbu+bsvx7kpn4pyu//ug5kvoizi2m6bndv77qg root@wzx the key's randomart image is: +---[rsa 2048]----+ | . | | . . o| | + . o.| | o . +..| | o s ...o.| | . = ..=o=| | + =e *++=| | . = o.+oo+=+| | o. .=+**==*| +----[sha256]-----+
2.将id_rsa私钥文件导出到客户端(可使用ftp工具等)
3.找到公钥文件并追加到authorized_keys文件内
[root@wzx ~]# cd .ssh [root@wzx .ssh]# ls id_rsa id_rsa.pub [root@wzx .ssh]# cat id_rsa.pub >> authorized_keys [root@wzx .ssh]# ls authorized_keys id_rsa id_rsa.pub [root@wzx .ssh]# cat authorized_keys ssh-rsa aaaab3nzac1yc2eaaaadaqabaaabaqc6bcv0ehdsxsd83cg4inl81d1vdliuywssbj+3anqok4qrwnpohfoibxganzqst+33crjy4zvfnvciewps+/fyyfntekhpzuorozczyfkbxm1sb7ym5plnhr8ydyucoayuhlybubixm80l1qvvopnnrkkgzcosoqspoihgxgaa2d7lbonr8uoo/aj/6fq2xxccecp9sg3h/uvci7bp430xqpmfvksrm/tr+8spnjnzioxgioh5sc6rqp2c1ic3krx3um4fcyqjjzj6i7uy74g+l/nt7it3qtr13zvvjeaktnv0zmvdxrtnnizewtrhwedaszjs3ewdjv1fesgerhth root@wzx
4.配置sshd_config文件
ot@wzx .ssh]# vim /etc/ssh/sshd_config ...... pubkeyauthentication yes #启用公钥验证 ...... [root@wzx .ssh]# systemctl restart sshd #重启刷新配置
5.在终端中设置私钥登录
6.登录
成功登录且不用输入密码
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论