ssh操作指南

ssh 操作指南

检查ssh服务是否启动

SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议。

ps -e | grep ssh

"""
7529 ? 00:00:00 sshd
7852 pts/1 00:00:00 ssh
"""

若输入指令后显示类似于上图所示,则说明SSH服务已启动
其中sshd表示ssh-server已启动,ssh表示ssh-client已启动

安装ssh服务

sudo apt-get install openssh-client
sudo apt-get install openssh-server

启动ssh服务

sudo /etc/init.d/ssh start
ps -e | grep ssh

修改ssh端口号

sudo vim /etc/ssh/sshd_config

"""
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin prohibit-password
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
"""

修改端口号port后,重启ssh服务即可生效

sudo /etc/init.d/ssh restart

ssh远程登录

ssh user@X.X.X.X
# X.X.X.X 为ip地址

数据传输

完成SSH服务配置之后即可实现基于SSH的数据传输,最常用方便的指令便是scp,以下是常用scp指令:

scp SOURCE DESTINATION
# -r : 递归复制目录

ssh无密码自动登录

需要两步

  1. 在本地主机上生成ssh密钥
  2. 将生成的公钥传给远程主机
# 在本地主机上生成密钥
ssh-keygen -t rsa

# 将公钥传给远程主机
ssh-copy-id USER@REMOTE_HOST

ssh实现端口转发

SSH的端口转发包括,本地端口转发和远程端口转发两部分

  1. 本地端口转发
# 在本地(运行命令的机器上)起一个监听端口,把所有对该本地端口的访问转发到服务器
ssh -L <local-port-to-listen>:<remote-host>:<remote-port> <sshserver>
# <local-port-to-listen> 本地端口,也就是命令运行的机器的端口
# <remote-host>:<remote-port> 目标机器和端口,是真正提供服务的端口
# <sshserver> ssh服务器,是提供端口转发功能的服务器

这个命令的功能就是把”本地机器:“映射到”:“,这个功能通过sshserver实现

本地端口转发通常执行在防火墙外的机器上,用来给防火墙外的其他机器访问防火墙内的资源

img

# 在proxyserver机器上执行下面的命令
ssh -L 30000:webserver:8080 user@SSHserver

  1. 远程端口转发

和本地端口转发的区别是:

本地端口转发,起一个监听端口在本地(执行命令的机器);然后转发所有向这个新起本地端口的请求, 到 sshserver,再到目标机器。

远程端口转发,起一个监听端口在远程(sshserver机器);然后转发所有向这个新起远程端口的请求,到本地机器(执行命令的机器),再到目标机器。

ssh -R <local-port-to-listen>:<remote-host>:<remote-port> <sshserver>
# <local-port-to-listen> 本地端口,也就是命令运行的机器的端口
# <remote-host>:<remote-port> 目标机器和端口,是真正提供服务的端口
# <sshserver> ssh服务器,是提供端口转发功能的服务器

所不同的是端口是起在sshserver上,而不是在运行命令的proxyserver机器上。然后把所有对:的请求都转发到:上去
远程端口转发通常执行在防火墙内的机器上,用来给防火墙外的其他机器访问防火墙内的资源

img

# 在proxyserveer机器上执行下面的命令
ssh -R 30000:webserver:8080 user@sshserver

借助服务器访问内网主机

img

sshfs实现本地挂载点挂载远程驱动器

# 首先安装sshfs
sudo apt-get install sshfs

# 必须sudo执行命令,否则权限出错
sudo sshf -o allow_other ltb@X.X.X.X:/home/ltb ~/AMAX

# 使用下面命令卸载
umount ~/AMAX
Author: pangzibo243
Link: https://litianbo243.github.io/2019/08/06/ssh 操作指南/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
支付宝打赏
微信打赏