SSH协议(Secure Shell,安全外壳)是一种需要进行加密和认证的,用于远程访问及文件传输的网络安全协议(默认协议端口22)。
SSH功能类似于Telnet服务,但SSH基于加密和认证的特性可以为用户提供更强大的安全保障机制,在用户使用不安全的网络环境登录设备时,SSH能够有效保护设备不受IP地址欺诈、明文密码截取等攻击。
只要服务器开启了sshd服务,可以直接使用ssh命令来连接并登录服务器
ssh 用户名@主机地址
接下来输入服务器ssh登录密码即可。
如上所提,ssh依赖的sshd服务,事实上linux下很多指令都是加d后的服务来提供支持的,例如crontab对应的crond、sftp对应的vsftpd等等。
打开 /etc/ssh/sshd_config
即为ssh的配置文件。
# $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 ::
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
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#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 no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users 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'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
PermitRootLogin yes
PasswordAuthentication yes
UseDNS no
如前面所说,ssh默认的端口为22,如果你想通过别的端口登录,可以打开
Port 22
配置,修改22为其他你想要的端口,就可以不适用默认的22端口登录,不过在登录时记得要指定端口
~/.ssh
生成公钥和私钥对:使用命令ssh-keygen -t rsa来生成RSA算法的公钥和私钥文件。默认情况下会将这两个文件保存到当前用户目录下的.ssh文件夹中。
复制公钥到目标主机:使用命令ssh-copy-id username@hostname将本地计算机上的公钥复制到目标主机上。其中,username表示目标主机上的用户名,hostname表示目标主机的IP地址或者域名。该命令会自动将公钥添加到目标主机的~/.ssh/authorized_keys文件中。
配置SSH服务器:如果目标主机没有开启SSH服务器功能,则需要先安装并配置OpenSSH服务器。可以根据操作系统类型选择合适的包管理工具进行安装,然后编辑SSH服务器的配置文件/etc/ssh/sshd_config
,确保以下设置正确:
PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin without-password
最后重新启动SSH服务器以应用更改。
测试免密登录:完成上述步骤后,就可以使用ssh username@hostname命令直接无需输入密码即可登录到目标主机了。
scp是一个跨主机拷贝命令,也是基于ssh的,主要实现两台网络相同的主机之间快速的文件拷贝。
scp local_file remote_username@remote_ip:remote_dir
scp -r local_dir remote_username@remote_ip:remote_dir
scp remote_username@remote_ip:remote_file local_file
scp -r remote_username@remote_ip:remote_dir local_dir
sftp(secure file transfer protocol)是一个安全的ftp文件传输工具,相比于传统ftp,它默认是安全的,使用的ssh通道进行传输,因此默认端口是22,和ftp需要手动开启二进制传输不同,它默认使用binary模式。
sftp sftpuser@sftp_ip
ls # 查看远程sftp服务器下当前目录下的文件和目录
lls # 查看本机当前目录下的文件和目录
cd # 切换远程sftp服务器的路径
lcd # 切换本机路径
pwd # 查看远程sftp服务器的当前路径
lpwd # 查看本机的当前路径
put # 上传本机文件到远程sftp服务器
get # 下载远程sftp服务器中的文件到本
exit/quit # 退出
工具名称 | 官方网址 |
---|---|
mobaXterm | https://mobaxterm.mobatek.net/ |
secureCRT | https://www.vandyke.com/products/securecrt/ |
WindTerm | https://github.com/kingToolbox/WindTerm/releases |
Xshell | https://www.xshell.com/zh/xshell/ |
FinalShell | http://www.hostbuf.com/ |
putty | https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html |
tabby | https://github.com/Eugeny/tabby/releases |
xterminal | https://www.xterminal.cn/ |
从上到下我依次推荐,有一些可以玩一玩,但实用性真的不高