ssh-server 安装
SSH分客户端
openssh-client
服务端
openssh-server
如果你只是想登陆别的机器的SSH只需要安装openssh-client__(Ubutntu有默认安装,如果没有则 sudo apt-get install openssh-client)__,如果要使本机开放SSH服务就需要安装openssh-server。
安装
我这里使用命令安装服务端SSH。
sudo apt-get install openssh-server
检查
然后使用命令确认sshserver是否启动了:
ps -e |grep ssh
如果看到sshd那说明ssh-server已经启动了。
alex@ubuntu:~$ ps -e | grep ssh
2284 ? 00:00:00 ssh-agent
8743 ? 00:00:00 sshd
如果没有则可以这样启动:
sudo /etc/init.d/ssh start
#或者
service ssh start
配置
配置文件位于**/etc/ssh/sshd_config**,在这里可以定义SSH的服务端口,默认端口是22。
然后重启SSH服务:
sudo /etc/init.d/ssh stop
sudo /etc/init.d/ssh start
开机自启动配置
设置开机自动启动ssh服务:
Ubuntu中配置openssh-server开机自动启动打开/etc/rc.local文件,添加如下语句:
/etc/init.d/ssh start
ssh 文件传输
上传
scp /path/file(这部分为本地的路径) user(远端目标用户名)@host(远端目标IP):/pathorfile(文件存储路径)下载
- 下载单独文件
scp user(远端用户名)@host(远端IP):/path/file(下载文件在远端的路径) localpathorfile(本地文件存放路径)
- 下载整个文件夹
scp -P 2233 -r root@xxx.xxx.xxx.xxx:/data/gagent/log_gagent/00BSJ26M_0403000A_20190610170053 .
- 查看目录或文件
ssh user@host command ls "/path/*.tgz"
反向代理

如上图,实现Company下的PC 通过ssh 访问到Home(A) 里的PC.实际上是在公网上一台主机Server(B)做SSH转发,在A上做SSH方向代理实现;
A服务器用户名: gagent
B服务器用户名: ubuntu
反向代理第一步:ssh 参数解析
-f 后台执行ssh指令
-C 允许压缩数据
-N 不执行远程指令
-R 将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口
-L 将本地机(客户机)的某个端口转发到远端指定机器的指定端口
-p 指定远程主机的端口
反向代理
ssh -fCNR正向代理
ssh -fCNL
反向代理第二步:服务配置
首先,在A上面搭建A到B的反向代理配置 指令为:
ssh -fCNR [B机器IP或省略]:[B机器端口]:[A机器的IP]:[A机器端口] [登陆B机器的用户名@服务器IP]在这里我使用了B机器的7280端口,用来月A通讯。以及A机器的22端口,用来与B通讯,( [B机器端口]<<====>>[A机器端口] )按照上面的指令就是这样子的操作
实例ssh -fCNR 7280:localhost:22 ubuntu@123.123.123.123在A检验是否已经启动了可以使用ps aux | grep ssh指令来查看:

在B上检测反向代理是否生效,看到7280端口启动则表明生效ubuntu@VM-133-112-ubuntu:~$ netstat -tnl | grep 127.0.0.1 tcp 0 0 127.0.0.1:7280 0.0.0.0:* LISTEN以上都成功则可以可从公网服务器B反向登录回家庭服务器A了
ssh gagent@127.0.0.1 -p 7280第二,在B机器上搭建正向代理到A的配置,用来在B上转发数据到A 指令为:
ssh -fCNL [A机器IP或省略]:[A机器端口]:[B机器的IP]:[B机器端口] [登陆B机器的用户名@B机器的IP]按照第一步那里输入的指令,这里的B机器的端口和上面的B机器的端口是一致的,端口1234的也是B机器的。
实例ssh -fCNL *:1234:localhost:7280 localhost*解释:**在此1234端口为本地转发端口,负责和外网进行通信,并将数据转发的7280这个端口,实现了可以从其他机器访问的功能。同时,号表示可以接受任何IP的访问。
第三,在C机器上ssh到A机器
ssh -p1234 gagent@123.123.123.123**解释:**在此-p参数为指定登陆的IP,我们在上面指定了__1234__端口为转发端口,故用__1234__端口登陆,然后__gagent__是内网A机器的用户名,__123.123.123.123__为外网B机器的IP地址。
反向代理第三步:稳定性加强
ssh每次重连都需要键入密码,故在此首先设置免密码登陆到内网A
在内网的机器A上面执行:- 1 生产 ssh-keygen(如果已经有了key 可以跳过此步骤)
gagent@ubuntu:~$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/gagent/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/gagent/.ssh/id_rsa. Your public key has been saved in /home/gagent/.ssh/id_rsa.pub. The key fingerprint is: **:**:**:**:**:**:**:**:**:**:**:**:**:**:**:** gagent@ubuntu The key's randomart image is: +--[ RSA 2048]----+ | . ... | | + . . . .. | | o o . | | . . | | .. a | | . .. d | | o.+o = d | | ..+ . | | o. | +-----------------+- 2 复制ssh-keygen到 B
# ssh-copy-id 外网用户名@外网IP ssh-copy-id ubuntu@123.123.123.123然后提醒你可以直接登录服务器B了,不用输入密码,效果如下:
gagent@ubuntu:~$ ssh-copy-id ubuntu@123.123.123.123 /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 ubuntu@123.123.123.123's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'ubuntu@123.123.123.123'" and check to make sure that only the key(s) you wanted were added. gagent@ubuntu:~$ ssh ubuntu@123.123.123.123 Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-91-generic x86_64)用autossh建立稳定隧道
由于A和B不稳定,容易发生断开事件,所以需要工具在断开之后自动ssh到B机器,于是就用到了autossh;1 工具安装说明
# 安装 sudo apt-get install autossh用法如下:
gagent@ubuntu:~$ autossh --help /usr/lib/autossh/autossh: invalid option -- '-' usage: autossh [-V] [-M monitor_port[:echo_port]] [-f] [SSH_OPTIONS] -M specifies monitor port. Overrides the environment variable AUTOSSH_PORT. 0 turns monitoring loop off. Alternatively, a port for an echo service on the remote machine may be specified. (Normally port 7.) -f run in background (autossh handles this, and does not pass it to ssh.) -V print autossh version and exit. Environment variables are: AUTOSSH_GATETIME - how long must an ssh session be established before we decide it really was established (in seconds). Default is 30 seconds; use of -f flag sets this to 0. AUTOSSH_LOGFILE - file to log to (default is to use the syslog facility) AUTOSSH_LOGLEVEL - level of log verbosity AUTOSSH_MAXLIFETIME - set the maximum time to live (seconds) AUTOSSH_MAXSTART - max times to restart (default is no limit) AUTOSSH_MESSAGE - message to append to echo string (max 64 bytes) AUTOSSH_PATH - path to ssh if not default AUTOSSH_PIDFILE - write pid to this file AUTOSSH_POLL - how often to check the connection (seconds) AUTOSSH_FIRST_POLL - time before first connection check (seconds) AUTOSSH_PORT - port to use for monitor connection AUTOSSH_DEBUG - turn logging to maximum verbosity and log to stderr- 2 实际操作
解释:autossh -M 7281 -fCNR 7280:localhost:22 root@123.123.123.123_autossh_的参数与ssh的参数是一致的,但是不同的是,在隧道断开的时候,autossh会自动重新连接而ssh不会。另外不同的是我们需要指出的-M参数,这个参数指定一个端口,这个端口是外网的B机器用来接收内网A机器的信息,如果隧道不正常而返回给A机器让他实现重新连接
- 2 实际操作
设置A机器启动的时候自动ssh到B
修改启动文件
vi /etc/rc.d/rc.local
添加内容:
autossh -M 7281 -fCNR 7280:localhost:22 root@123.123.123.123