安装、使用ssh服务详解

安装、使用ssh服务详解

 

什么是ssh?

SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台。SSH在正确使用时可弥补网络中的漏洞。SSH客户端适用于多种平台。几乎所有UNIX平台—包括HP-UX、Linux、AIX、Solaris、Digital UNIX、Irix,以及其他平台,都可运行SSH。[来自百度百科]

一、先查看一下是否启动

ps -e | grep ssh

二、是否安装openssh-server

rpm -qa | grep ssh 

三、安装openssh-server

sudo apt-get  install openssh-server

四、启动ssh服务

sudo /etc/init.d/ssh start 
# 或 
sudo service ssh start

 

 

 

五、查看ssh服务的状态

sudo service ssh status

active(running)为开启状态,since后面的时间为开启的时间,以及持续时间。下面的列表是历史登录记录,显示了登录时间、用户名、登录IP、登录端口号等信息。

 

inactive(dead)为关闭状态,since后面的时间为开启的时间,以及持续时间。

六、关闭、重启ssh服务

sudo /etc/init.d/ssh stop 
# 或 
sudo service ssh stop 

sudo /etc/init.d/ssh restart 
# 或 
sudo service ssh restart

七、设置开机开启ssh服务

chkconfig sshd on

八、查看开启网络连接情况

netstat -ntlp

也可以直接使用命令过滤

netstat -a | grep ssh
# 或
ss | grep ssh

ssh端口号为TCP22

 

九、查看ssh配置文件

/etc/ssh/sshd_config

完整的配置文件,可以进行修改配置文件的键值对更改配置。关键字忽略大小写。

Host * 
选项“Host”只对能够匹配后面字串的计算机有效。“*”表示所有的计算机。
 
ForwardAgent no 
“ForwardAgent”设置连接是否经过验证代理(如果存在)转发给远程计算机。 

ForwardX11 no 
“ForwardX11”设置X11连接是否被自动重定向到安全的通道和显示集(DISPLAY set)。 

RhostsAuthentication no 
“RhostsAuthentication”设置是否使用基于rhosts的安全验证。 

RhostsRSAAuthentication no 
“RhostsRSAAuthentication”设置是否使用用RSA算法的基于rhosts的安全验证。 

RSAAuthentication yes 
“RSAAuthentication”设置是否使用RSA算法进行安全验证。 

PasswordAuthentication yes 
“PasswordAuthentication”设置是否使用口令验证。 

FallBackToRsh no 
“FallBackToRsh”设置如果用ssh连接出现错误是否自动使用rsh。 

UseRsh no 
“UseRsh”设置是否在这台计算机上使用“rlogin/rsh”。 

BatchMode no 
“BatchMode”如果设为“yes”,passphrase/password(交互式输入口令)的提示将被禁止。当不能交互式输入口令的时候,这个选项对脚本 文件和批处理任务十分有用。 

CheckHostIP yes 
“CheckHostIP”设置ssh是否查看连接到服务器的主机的IP地址以防止DNS欺骗。建议设置为“yes”。 

StrictHostKeyChecking no 
“StrictHostKeyChecking”如果设置成“yes”,ssh就不会自动把计算机的密匙加入“$HOME/.ssh/known_hosts”文件,并且一旦计算机的密 匙发生了变化,就拒绝连接。
 
IdentityFile ~/.ssh/identity 
“IdentityFile”设置从哪个文件读取用户的RSA安全验证标识。 

Port 22 
“Port”设置连接到远程主机的端口。 

Cipher blowfish 
“Cipher”设置加密用的密码。 

EscapeChar ~ 
“EscapeChar”设置escape字符。

 

下面是选项的可以配置项

# This is ssh server systemwide configuration file.
Port 22
ListenAddress 192.168.1.1
HostKey /etc/ssh/ssh_host_key
ServerKeyBits 1024
LoginGraceTime 600
KeyRegenerationInterval 3600
PermitRootLogin no
IgnoreRhosts yes
IgnoreUserKnownHosts yes
StrictModes yes
X11Forwarding no
PrintMotd yes
SyslogFacility AUTH
LogLevel INFO
RhostsAuthentication no
RhostsRSAAuthentication no
RSAAuthentication yes
PasswordAuthentication yes
PermitEmptyPasswords no
AllowUsers admin

下面逐行说明上面的选项设置:

Port 22
“Port”设置sshd监听的端口号。

ListenAddress 192.168.1.1
“ListenAddress”设置sshd服务器绑定的IP地址。

HostKey /etc/ssh/ssh_host_key
“HostKey”设置包含计算机私人密匙的文件。

ServerKeyBits 1024
“ServerKeyBits”定义服务器密匙的位数。

LoginGraceTime 600
“LoginGraceTime”设置如果用户不能成功登录,在切断连接之前服务器需要等待的时间(以秒为单位)。

KeyRegenerationInterval 3600
“KeyRegenerationInterval”设置在多少秒之后自动重新生成服务器的密匙(如果使用密匙)。重新生成密匙是为了防止用盗用的密匙解密被截获的信息。

PermitRootLogin no
“PermitRootLogin”设置root能不能用ssh登录。这个选项一定不要设成“yes”。

IgnoreRhosts yes
“IgnoreRhosts”设置验证的时候是否使用“rhosts”和“shosts”文件。

IgnoreUserKnownHosts yes
“IgnoreUserKnownHosts”设置ssh daemon是否在进行RhostsRSAAuthentication安全验证的时候忽略用户的“$HOME/.ssh/known_hosts”

StrictModes yes
“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任何人都有写权限。

X11Forwarding no
“X11Forwarding”设置是否允许X11转发。

PrintMotd yes
“PrintMotd”设置sshd是否在用户登录的时候显示“/etc/motd”中的信息。

SyslogFacility AUTH
“SyslogFacility”设置在记录来自sshd的消息的时候,是否给出“facility code”。

LogLevel INFO
“LogLevel”设置记录sshd日志消息的层次。INFO是一个好的选择。查看sshd的man帮助页,已获取更多的信息。

RhostsAuthentication no
“RhostsAuthentication”设置只用rhosts或“/etc/hosts.equiv”进行安全验证是否已经足够了。

RhostsRSAAuthentication no
“RhostsRSA”设置是否允许用rhosts或“/etc/hosts.equiv”加上RSA进行安全验证。

RSAAuthentication yes
“RSAAuthentication”设置是否允许只有RSA安全验证。

PasswordAuthentication yes
“PasswordAuthentication”设置是否允许口令验证。

PermitEmptyPasswords no
“PermitEmptyPasswords”设置是否允许用口令为空的帐号登录。

AllowUsers admin
“AllowUsers”的后面可以跟着任意的数量的用户名的匹配串(patterns)或user@host这样的匹配串,这些字符串用空格隔开。

完整的配置文件

$ 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/bin:/bin:/usr/sbin:/sbin

# 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.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

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

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

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

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

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# 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

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

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

# override default of no subsystems
Subsystem       sftp    /usr/lib/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server

十、ssh排查问题

1.判断物理链路是否通

ping 192.168.0.102  线路 | 防火墙 | 是否同一个网的 ping   本身是icmp协议

2.判断服务是否正常

telnet 192.168.0.102 22

3.Linux防火墙

service iptables status ==> /etc/init.d/iptables status 

4.打开ssh的调试进行观察

-v参数的个数是详细程度1-3等级

ssh -v lemon@192.168.0.102 
ssh -vvv lemon@192.168.0.102

十一、查看ssh的使用命令

1.ssh查看用法

usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
           [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
           [user@]hostname [command]
# 默认root账户登录
ssh 192.168.0.102

# 可以指定用户 -l
ssh -l lemon 192.168.0.102
ssh lemon@192.168.0.102

# 指定端口 -p 缺省为22
ssh -p 100 192.168.0.102
ssh -l lemon -p 100 192.168.0.102
ssh -p 100 lemon@192.168.0.102

2.使用man查看ssh详细用法(仅使用的话,用到进行查询即可)

SSH(1)                                   BSD General Commands Manual                                   SSH(1)

NAME
     ssh — OpenSSH SSH client (remote login program)

SYNOPSIS
     ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
         [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file]
         [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option]
         [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
         [user@]hostname [command]

DESCRIPTION
     ssh (SSH client) is a program for logging into a remote machine and for executing commands on a remote
     machine.  It is intended to provide secure encrypted communications between two untrusted hosts over an
     insecure network.  X11 connections, arbitrary TCP ports and UNIX-domain sockets can also be forwarded
     over the secure channel.

     ssh connects and logs into the specified hostname (with optional user name).  The user must prove
     his/her identity to the remote machine using one of several methods (see below).

     If command is specified, it is executed on the remote host instead of a login shell.

十二、scp的使用命令

usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
           [-l limit] [-o ssh_option] [-P port] [-S program]
           [[user@]host1:]file1 ... [[user@]host2:]file2

1、上传本地文件到服务器

把本机/home/目录下的hello文件上传到192.168.0.102这台服务器上的/opt/目录中

scp /home/hello lemon@192.168.0.102:/opt/

2、从服务器上下载文件

把192.168.0.102上的/opt/world 的文件下载到/home/(本地目录)

scp lemon@192.168.0.102:/opt/world /home/(本地目录)

3、从服务器下载整个目录

把远程服务器的/opt/目录拷贝到本机/home/下

scp -r lemon@192.168.0.102:/opt/(远程目录) /home/(本地目录)

4、上传目录到服务器

把当前目录下的source目录上传到服务器的/opt/ 目录

scp -r source/ lemon@192.168.0.102:/opt/

 

感谢开源,感谢文章的作者们,欢迎交流指正!

参考文章:

https://www.cnblogs.com/jiarenanhao/p/9938280.html

https://blog.csdn.net/liguangix/article/details/79262027

https://www.cnblogs.com/ftl1012/p/ssh.html

https://blog.csdn.net/u012778549/article/details/81112918

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

byzf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值