OpenSSH 管理远程主机

SSH 为 Secure Shell 的缩写,是一种以安全的方式提供远程登陆的协议,也是目前远程管理Linux系统的首选方式,SSH由 IETF 的网络小组(Network Working Group)所制定,SSH为建立在应用层基础上的安全协议,SSH是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用SSH协议可以有效防止远程管理过程中的信息泄露问题.

SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台,SSH在正确使用时可弥补网络中的漏洞,SSH客户端适用于多种平台,几乎所有UNIX平台—包括HP-UX、Linux、AIX、Solaris、Digital、UNIX、Irix以及其他平台,都可运行SSH.

常用sshd配置项

sshd是一款基于SSH协议开发的一款远程管理服务程序,此工具不仅使用起来方便快捷,而且能够提供两种安全验证方法,一种是:基于口令的验证,另一种是:基于密钥的验证,两种方法相比较推荐大家使用密钥对验证方法,其安全性最高.

sshd服务的配置文件默认保存在/etc/ssh/sshd_config文件中,下面我们将介绍sshd_config配置文件中的参数的意思吧.

vim /etc/ssh/sshd_config

port 22                                     #监听端口
addressFamily any                           #允许所有人链接
listenAddress 0.0.0.0                       #IPV4监听IP   0.0.0.0表示监听所有
listenAddress : :                           #IPV6监听IP
protocol 2                                  #使用二代协议
syslogFacility AUTHPRIV                     #日志认证等级
permitRootLogin yes                         #是否允许root登陆
passwordAuthentication yes                  #是否使用密码认证
permitEmptyPasswords no                     #是否允许空密码
loginGraceTime 2m                           #2分钟不输入后自动断开连接
printMotd yes                               #登陆后根据/etc/motd内容打印信息
printLastLog yes                            #输出最后一次登录信息
useDNS yes                                  #反查主机名,关闭后可提升登陆速度
gSSAPIAuthentication yes                    #GSS认证,关闭后可提升登陆速度
pidFile /var/run/sshd.pid                   #存放sshPID的地方
usePrivilegeSeparation sandbox/yes/no       #是否允许以较低权限运行
pubkeyAuthentication yes                    #使用公钥认证机制
authorizedKeysFile .ssh/auth                #公钥的存放位置
MaxAuthTries 5                              #密码最大尝试次数
MaxSessions 10                              #最大允许终端数

看了上面的配置参数,接下来我们来继续看一下ssh命令的常用参数吧

usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]

        -q      #静默模式
        -i      #指定身份文件
        -o      #指定配置选项
        -X      #开启X11转发功能
        -x      #关闭X11转发功能
        -y      #开启信任X11转发功能
        -1      #强制使用ssh协议版本1
        -2      #强制使用ssh协议版本2
        -4      #强制使用IPv4地址
        -6      #强制使用IPv6地址
        -C      #请求压缩所有数据
        -f      #后台执行ssh指令
        -N      #不执行远程指令
        -F      #指定ssh指令的配置文件
        -A      #开启认证代理连接转发功能
        -a      #关闭认证代理连接转发功能
        -l      #指定连接远程服务器登录用户名
        -g      #允许远程主机连接主机的转发端口
        -p      #指定远程服务器上的端口
        -b      #使用本机指定地址作为对应连接的源ip地址


基于密钥对验证

首先,我们通过配置ssh密钥对,可以实现免密钥登陆指定主机

1.通过命令生成密钥对,此时系统会在/root/.ssh/id_rsa下面生成两个文件一个公钥一个私钥

[root@localhost ~]# ssh-keygen
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:eVPH36wIC0vuOEdNs19vQPHSJ/lAp53BuVSwNso4Sb8 [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|              o.+|
|             .o*.|
|          . ..BO+|
|         oo* +B**|
|        SoBo+. ==|
|       o.+o= oo..|
|       .o ..E..o |
|      .o.   .   o|
|      .o.      . |
+----[SHA256]-----+

2.通过一个命令将生成的公钥自动的拷贝到对方主机上,此时系统会将id_rsa.pub拷贝到对方/root目录下,并会自动命名为authorized_keys.

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.22 (192.168.1.22)' can't be established.
ECDSA key fingerprint is SHA256:2kWFaV72YVvAl2EU2Zop4uAjP3Gy2jW92d0Va/HrSMM.
ECDSA key fingerprint is MD5:fc:6c:91:b0:02:e6:7e:98:52:af:0d:b3:47:d4:69:ef.
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
[email protected]'s password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.

3.下次使用ssh登陆对方主机,无需密码即可登陆上了

[root@localhost ~]# ssh [email protected]

Last login: Mon Nov  5 09:59:45 2018 from 192.168.1.8


通过scp传输文件

scp(secure copy)是一个基于SSH协议在网络之间进行安全传输的命令,scp不仅可以传输数据,而且在传输过程中都是加密的,安全性方面更高

首先我们先看一下,scp命令的常用参数

usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]

        -1      #强制scp命令使用协议ssh1
        -2      #强制scp命令使用协议ssh2
        -4      #强制scp命令只使用IPv4寻址
        -6      #强制scp命令只使用IPv6寻址
        -B      #使用批处理模式,过程中不询问
        -C      #允许压缩
        -p      #留原文件的修改时间,访问时间和访问权限
        -q      #不显示传输进度条
        -r      #传送文件夹
        -v      #详细方式显示输出
        -c      #以cipher加密传输
        -F      #指定一个替代的ssh配置文件
        -i      #传输时指定密钥文件
        -l      #限制传输带宽,以Kbit/s为单位
        -P      #指定传输端口
        -S      #指定加密传输时所使用的程序

下面我们举几个小例子来说明scp的使用方法

1.将 /etc/passwd 拷贝到远程的 /tmp 目录下

[root@localhost ~]# scp /etc/passwd [email protected]:/tmp/

[email protected]'s password:
passwd                                                               100%  898   876.6KB/s   00:00

2.将远程的 /etc/shadow 拷贝到本地的 /tmp 目录下

[root@localhost ~]# scp [email protected]:/etc/shadow /tmp/

[email protected]'s password:
shadow                                                               100%  714   741.2KB/s   00:00

3.把远程的 /etc 目录拷贝到本机的 /tmp 目录下

[root@localhost ~]# scp -r [email protected]:/etc/ /tmp/

[email protected]'s password:
fstab                                                                100%  465   188.1KB/s   00:00
crypttab                                                             100%    0     0.0KB/s   00:00
mtab                                                                 100%    0     0.0KB/s   00:00
resolv.conf                                                          100%    1     1.2KB/s   00:00
00_header                                                            100% 8702   696.5KB/s   00:00
01_users                                                             100%  232   135.8KB/s   00:00
.........


猜你喜欢

转载自www.cnblogs.com/LyShark/p/9951137.html