大家好,欢迎来到IT知识分享网。
一:配置服务端
1:服务监听选项
[root@localhost ~]# setenforce 0
注意:如果不关闭selinux,不能修改端口号
[root@localhost ~]# vi /etc/ssh/sshd_config
修改
Port 22 监听的
重启sshd服务
[root@localhost ~]# systemctl restart sshd
客户端登陆验证:
linux客户端
注意:服务端可以修改端口号,但在远程连接时,服务器要关防火墙
[root@localhost ~]# ssh -p 16666 root@192.168.10.10
2:用户登录控制
[root@localhost ~]# vi /etc/ssh/sshd_config
修改
LoginGraceTime 2m 允许一次登录花费登陆验证的时间
PermitRootLogin yes 为了后面的实验,不禁用root
MaxAuthTries 6 登陆次数(输错密码的次数)
PermitEmptyPasswords no 禁止空密码用户登录
43 #StrictModes yes 严格模式如果StrictModes为yes必需保证存放公钥的文件夹的与登陆用户名是相同的
AllowUsers zhangsan aaa lisi@192.168.10.20 只允许或拒绝时用,添加。lisi只能从10.20登陆
[root@localhost ~]# systemctl restart sshd
客户端登陆验证
注意:
MaxAuthTries 6 登陆次数(输错密码的次数)
客户端在ssh登录的时候,默认使用3次密码尝试,所以在测试这个MaxAuthTries参数时,可以让客户端多尝试几次密码,可以用以下语句让客户端登录时尝试指定次数的密码
[root@192 ~]# ssh -o NumberOfPasswordPrompts=8 192.168.10.101
二:使用ssh客户端程序
1):客户端的操作
root@localhost ~]# ssh aaa@192.168.10.10
aaa@192.168.10.10’s password:
[aaa@localhost ~]$ ifconfig
[aaa@localhost ~]$ exit
[root@localhost ~]# scp root@192.168.10.10:/etc/passwd /usr/ccc.txt 下载,在AllowUsers中添加root
root@192.168.10.10’s password:
passwd 100% 1668 1.6KB/s 00:00
[root@localhost ~]# scp -r /usr/src/text.html root@192.168.10.10:/usr 上传
root@192.168.10.10’s password:
text.html 100% 0 0.0KB/s 00:00
[root@localhost ~]# scp -o port=16666 root@192.168.10.10:/etc/passwd /opt/bbb.txt 下载
[root@localhost ~]# scp -P 16666 -r /opt/aaa.txt root@192.168.10.10:/opt/vvv.txt 上传
2.客户端的sftp操作
[root@localhost ~]# sftp aaa@192.168.10.10
Connecting to 192.168.10.10…
aaa@192.168.10.10’s password:
sftp> ls
sftp> put /etc/profile 上传到了aaa的宿主目录下,普通用户只能将数据上传到宿主目录,
root可以上传到其他地方直接用cd切换目录
Uploading /etc/profile to /home/aaa/profile
/etc/profile 100% 1796 1.8KB/s 00:00
sftp> ls
profile
sftp> get profile 下载,下载到当前位置
sftp> bye
注释:如果远程服务器修改了端口号,可用下列方法登录sftp
[root@localhost ~]# sftp -o port=8888 root@192.168.10.101
三:构建密钥对验证ssh体系(方法一)
1:服务器端设置登陆验证方式
[root@localhost ~]# vi /etc/ssh/sshd_config
修改(都是默认值,不用修改)
PasswordAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys 指定公钥数据库
[root@localhost ~]# systemctl restart sshd
2:在客户端创建密钥对
[root@localhost ~]# useradd bbb
[root@localhost ~]# passwd bbb
[root@localhost ~]# su bbb
[bbb@localhost root]$ ssh-keygen -t rsa 创建密钥
秘钥短语:用于第一次使用该秘钥时解锁秘钥用的
[bbb@localhost root]$ ls -lh ~/.ssh/id_rsa*
-rw——-. 1 bbb bbb 1.7K 12月 3 17:00 /home/bbb/.ssh/id_rsa
-rw-r–r–. 1 bbb bbb 407 12月 3 17:00 /home/bbb/.ssh/id_rsa.pub
3:将公钥文件上传到服务器
[bbb@localhost root]$ scp ~/.ssh/id_rsa.pub root@192.168.10.10:/tmp
4:在服务器中导入公钥文本
[root@localhost ~]# useradd aaa
[root@localhost ~]# passwd aaa
[root@localhost ~]# mkdir -p /home/aaa/.ssh/ 想登陆谁的账号,就导入到谁里面
[root@localhost ~]# cat /tmp/id_rsa.pub >> /home/aaa/.ssh/authorized_keys
[root@localhost ~]# tail -l /home/aaa/.ssh/authorized_keys
[root@localhost ~]# ls -l /home/aaa/.ssh/authorized_keys
5:在客户端使用密钥对验证
[bbb@localhost root]$ ssh aaa@192.168.10.10 在服务器上allowusers中要添加aaa账号
[aaa@localhost ~]$ ifconfig
[aaa@localhost ~]$ ls -l /home/aaa/.ssh/authorized_keys
[aaa@localhost ~]$ tail -l /home/aaa/.ssh/authorized_keys
[aaa@localhost ~]$ exit
四:免密登录(方法二)
(1)以下命令在客户端操作
[root@localhost ~]# ssh-keygen -t rsa
[root@localhost ~]# yum -y install openssh-clients
[root@localhost ~]# ssh-copy-id root@192.168.10.101
(2)登录服务器
五:TCP Wrappers访问控制 (TCP封装)
[root@localhost ~]# ldd /usr/sbin/sshd | grep “libwrap”
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f7f1d)
[root@localhost ~]# vi /etc/hosts.allow
添加
sshd:61.63.65.67 192.168.10.*
注意:如果不想要192.168.10.0的主机远程,就不要添加
[root@localhost ~]# vi /etc/hosts.deny
添加(大小写不区分)
sshd:ALL
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/113430.html