linux-9 openssh

来源:互联网 发布:网络制作软件扑克 编辑:程序博客网 时间:2024/05/27 20:52
OpenSSH
介绍:
可以让远程主机可以通过网络访问sshd服务,开始一个安全shell。OpenSSH 是 SSH (Secure SHell) 协议的免费开源实现。SSH协议族可以用来进行远程控制, 或在计算机之间传送文件。而实现此功能的传统方式,如telnet(终端仿真协议)、 rcp ftp、 rlogin、rsh都是极为不安全的,并且会使用明文传送密码。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控件和文件传输过程中的数据,并由此来代替原来的类似服务。
OpenSSH是使用SSH透过计算机网络加密通讯的实现。它是取代由SSH Communications Security所提供的商用版本的开放源代码方案。目前OpenSSH是OpenBSD的子计划。
一:连接远程主机
登陆主机的文本界面(常用)
命令:
ssh 远程主机用户@远程主机ip 

[root@foundation16 dhcp]# ssh root@172.25.254.116                  #输入命令The authenticity of host '172.25.254.116 (172.25.254.116)' can't be established.ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.Are you sure you want to continue connecting (yes/no)? yes         #确认Warning: Permanently added '172.25.254.116' (ECDSA) to the list of known hosts.root@172.25.254.116's password:                                    #输入远程主机的用户密码Last login: Tue Jul 25 21:22:46 2017                               #登陆成功[root@localhost ~]#                                 

登陆主机的图形界面(在主机中需有图形界面)
命令:
ssh 远程主机用户@远程主机ip -X

直接在远程主机运行某条命令:
ssh 远程主机用户@远程主机ip command
[root@foundation16 dhcp]# ssh root@172.25.254.116 touch /root/ssh_file_testroot@172.25.254.116's password: [root@foundation16 dhcp]# 


二:sshkey 加密

       如果想让别人用ssh远程连接自己的主机,但是又不想给别人自己的用户密码,这就可以用sshkey生成给钥匙和锁,把锁加到自己的主机上,再给别人锁的钥匙,这样别人就可以免密码登陆自己的主机了。
步骤:
(1)生成公钥和私钥
[root@foundation16 dhcp]# 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:0b:cc:d3:68:19:c1:99:b9:3e:01:8f:04:5a:ea:a9:e9 root@foundation16.ilt.example.comThe key's randomart image is:+--[ RSA 2048]----+|  o. ..+         || +  o =.         ||o  . +..         ||. . .o+=         || o   .O.S        ||..   .oo .       ||o      ..        ||.                || E               |+-----------------+[root@foundation16 dhcp]# 

[root@foundation16 dhcp]# ls /root/.ssh/id_rsa  id_rsa.pub  known_hosts
(2) 加密key认证方式
命令:
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.216  #-i 指定加密key文件                                                          #/root/id_rsa.pub 加密key的位置                                                          #root@172.25.254.216 被加密主机用户及ip
加密key如图:


(3) 分发密钥
命令:
scp /root/.ssh/id_rsa 用户名@主机地址:/root/.ssh/


(4) 测试连接
在有密钥的主机里用ssh连接加上公钥的主机,测试是否需要输入用户密码
如图测试成功:



三 提升openssh的安全级别
在被加有公钥的主机中通过设置,可以控制访问本主机的人,实现让只有自己指定的人可以连接主机

(1) 修改ssh-server 的配置文件
文件的地址:/etc/ssh/sshd_config
文件如图:

PasswordAuthentication yes|no        #是否开启用户密码认证,yes为支持no为关闭48 PermitRootLogin yes|no#是否允许超级用户登陆49 AllowUsers student root#用户白名单,只有在名单中出现的用户可以使用sshd建立shell50 DenyUsers student#用户黑名单
如图:禁止student登陆后的效果,student 需要输入密码才能的登陆


(2) 控制ssh客户端的访问
方法:修改文件
文件地址:/etc/hosts.deny
                /etc/hosts.deny

## hosts.deny    This file contains access rules which are used to#               deny connections to network services that either use#               the tcp_wrappers library or that have been#               started through a tcp_wrappers-enabled xinetd.##               The rules in this file can also be set up in#               /etc/hosts.allow with a 'deny' option instead.##               See 'man 5 hosts_options' and 'man 5 hosts_access'#               for information on rule syntax.#               See 'man tcpd' for information on tcp_wrappers#ssd:ALL                                           #决绝所有人连接#ssd:ALL EXCEPT 172.25.254.22                     #只允许172.25.254.22 连接

## hosts.allow   This file contains access rules which are used to#               allow or deny connections to network services that#               either use the tcp_wrappers library or that have been#               started through a tcp_wrappers-enabled xinetd.##               See 'man 5 hosts_options' and 'man 5 hosts_access'#               for information on rule syntax.#               See 'man tcpd' for information on tcp_wrappers#sshd:172.25.254.116, 172.168.254.16      #允许谁连接#sshd:ALL EXCEPT 172.25.254.25           #只不允许谁连接                                

(3) 修改登陆提示文本
为了方便登陆后一眼了解登陆的是什么用途的服务器,可以在被登陆的服务器中自定义设置登陆提示文本
方法:修改文件
文件地址:/etc/motd
用vim 在文件中输入自己想要的文本即可
如图:


原创粉丝点击