ssh 101
一、概述
是Linux的登陆工具,VPS管理利器
二、客户端
登陆方式:ssh user@ip,user是用户名,ip是远端主机的公网ip
端口:默认是22,可以在/etc/ssh/sshd_config中修改端口,修改后记得查看防火墙是否允许;
参数:
-p:指定连接的端口,ssh -p port
-v:登陆调试信息,如果登陆不成功,此参数很有用
三、配置文件
id_rsa:私钥
id_ras.pub:公钥
known_hosts:SSH 服务器的公钥指纹
config:个人配置,通过在这里配置,可以通过别名的方式,快速登陆
# 主机别名 Host serverName # 主机公网ip HostName ip # 主机端口 Port 22 #指定各种登录方法的优先级:publickey,hostbased,password PreferredAuthentications publickey # 私钥 IdentityFile ~/.ssh/id_rsa # 登陆主机用户名 User user # 指定动态转发端口,本机与 SSH 服务器之间创建了一个加密连接 # 然后本机内部针对某个端口的通信,都通过这个加密连接转发 DynamicForward tunnel-host:local-port # 指定本地端口转发,创建一个本地端口,将发往该端口的所有通信都通过 SSH 服务器, # 转发到指定的远程服务器的端口 LocalForward client-IP:client-port server-IP:server-port # 指定远程端口转发,通过远程 SSH 服务器访问本地计算机 RemoteForward remote-port target-host:target-port
四、密钥登陆
生成密钥:
ssh-keygen
-t:指定加密算法,默认为rsa
上传公钥:
scp ~/.ssh/id_rsa.pub <用户名>@<ip地址>:/home/id_rsa.pub
scp -P <端口号> ~/.ssh/id_rsa.pub <用户名>@<ip地址>:/home/id_rsa.pub
scp:用于两台主机之间加密传送文件,等同于cp+ssh
scp source destination:source:当前文件,destination:目的地
scp user@host:file file:主机与文件之间要使用冒号(:)分隔
-r:复制目录,
scp -r directory directory
-p:指定端口
y追加到authorized_keys文件中
cat /home/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_rsa.pub | ssh user@host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys
通过ssh-copy-id上传
ssh-copy-id -i key_file user@host
五、服务端
配置文件:/etc/ssh/sshd_config
# 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:/bin:/usr/games
# 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.
Include /etc/ssh/sshd_config.d/*.conf
# 端口
#Port 22
#指定 sshd 监听的本机 IP 地址
#ListenAddress 0.0.0.0
#ListenAddress ::
# 指定Syslog如何处理 sshd 的日志
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
# 指定允许客户端登录时发呆的最长时间
#LoginGraceTime 2m
# 指定是否允许根用户登录
PermitRootLogin yes
# 指定 sshd 是否检查用户的一些重要文件和目录的权限
#StrictModes yes
# 指定允许 SSH 登录的最大尝试次数
#MaxAuthTries 6
# 指定是否允许公钥登录
#PubkeyAuthentication yes
# 指定储存用户公钥的目录
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
六、指令
启动:
service sshd start
停止:
service sshd stop
重启:
service sshd restart
七、工具推荐
如果管理的vps可以用一只手可以数的过来,item2+ssh+config就可以了
如果很多,还是需要趁手的工具
Termius(推荐)
electerm
Xterminal