目录
Linux服务-openssh
1. 使用 SSH 访问远程命令行
1.1 OpenSSH 简介
OpenSSH这一术语指系统中使用的Secure Shell软件的软件实施。用于在远程系统上安全运行shell。如果您在可提供ssh服务的远程Linux系统中拥有用户帐户,则ssh是通常用来远程登录到该系统的命令。ssh命令也可用于在远程系统中运行命令。
常见的远程登录工具有:
- telnet
- ssh
- dropbear
telnet //远程登录协议,23/TCP
//一般用于测试端口或者接口是否开启
认证明文
数据传输明文
ssh //Secure SHell,应用层协议,22/TCP
通信过程及认证过程是加密的,主机认证
用户认证过程加密
数据传输过程加密
dropbear //嵌入式系统专用的SSH服务器端和客户端工具,一般应用于手机上
1.2 SSH 版本
openssh有两个版本,分别为v1和v2,其特点如下:
v1:基于CRC-32做MAC,无法防范中间人攻击
v2:双方主机协议选择安全的MAC方式。基于DH算法做密钥交换,基于RSA或DSA算法实现身份认证
关于密钥交换
协商生成密码的过程叫做密钥交换(Internet Key Exchange,IKE)
使用的是DH协议(Diffie-Hellman):
A(主机) --> B(主机)
p,g(大素数,生成数),在网络中传输的,公开的
A:自己取一个随机数x
B:自己取一个随机数y
A:g^x%p --> B
B:g^y%p --> A
A:(g^y%p)^x=g^yx%p
B:(g^x%p)^y=g^xy%p
这最后得出的g^xy%p就是最终的密钥
1.3 SSH 认证方式
openssh有两种认证方式,分别是:
- 基于口令认证(即密码认证)
- 基于密钥认证(非对称加密。有一对密钥,公钥(P)和私钥(S))
1.4 openSSH 的工作模式
openSSH是基于C/S架构工作的
服务器端 //sshd,配置文件在/etc/ssh/sshd_config
[root@CTL .ssh]# vim /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
.
.
.
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
客户端
//ssh,配置文件在/etc/ssh/ssh_config
ssh-keygen //密钥生成器
ssh-copy-id //将公钥传输至远程服务器
scp //跨主机安全复制工具
$OpenBSD: ssh_config,v 1.30 2016/02/20 23:06:23 sobrado Exp $
# This is the ssh client system-wide configuration file. See
# ssh_config(5) for more information. This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.
# Configuration data is parsed as follows:
# 1. command line options
# 2. user-specific file
# 3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.
# Site-wide defaults for some commonly used options. For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.
# Host *
# ForwardAgent no
# ForwardX11 no
# RhostsRSAAuthentication no
# RSAAuthentication yes
# PasswordAuthentication yes
.
.
.
# Send locale-related environment variables
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
1.5 Secure Shell 示例
//以当前用户身份创建远程交互式shell,然后在结束时使用exit命令返回到之前的shell
[root@CTL .ssh]# ssh 192.1