侧边栏壁纸
博主头像
晓然博主等级

走着、走着、花开了

  • 累计撰写 37 篇文章
  • 累计创建 15 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

关于ssh登陆服务器这件事

晓然
2022-09-01 / 0 评论 / 0 点赞 / 175 阅读 / 974 字
温馨提示:
本文最后更新于 2023-02-21,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

关于ssh登陆服务器这件事

前言

当仅有一台服务器,通过ssh登陆是简单的,但当有多台服务器时,或许有更好的方法来管理。本文是我的笔记,简单的总结了通过终端登陆服务器的几种方法,希望对你有用。

实验环境

  • OS: Debian 5.10.120
  • GNOME 桌面的终端模拟器
  • ssh服务器: ssh.test1.comssh.test2.com
  • 用户名:user
  • pem文件:~/.ssh.test.pem

登陆服务器

最基本的用法

以用户名登陆远程主机,一条简单的命令就可以了。默认端口为22

ssh user@ssh.test1.com

如果以指定端口访问,使用参数-p

ssh -p user@ssh.test1.com

公钥登陆

通过user@host这种方式登陆,每次都要输入密码,就很麻烦。使用公钥登陆可以省去输入密码的步骤。

登陆过程演示

  1. 生成密钥

由于在Openssh7.0及以上版本中,由于DSA的脆弱性,已经默认不支持DSA算法了(https://www.openssh.com/legacy.html),这里使用rsa算法

xc@x:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xc/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/xc/.ssh/id_rsa
Your public key has been saved in /home/xc/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:ixS255ohi0BjADwxBga0y3aZnGCKl5T1nbymZ4i/6tQ xc@x
The key's randomart image is:
+---[RSA 3072]----+
|O*. .            |
|+ooo . o .       |
|.++   + +        |
|*.+.+. o .       |
|oBo*  o S        |
|+.o  + B .       |
|.   + E =        |
| . o + *         |
|  ..+.=.         |
+----[SHA256]-----+    
  1. 上传到服务器,使用ssh-copy-id 命令。参数i指定文件位置,使用绝对路径
xc@x:~$ ssh-copy-id  -i /home/xc/.ssh/id_rsa.pub  root@133.33.333.423
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/xc/.ssh/id_rsa.pub"
The authenticity of host '133.33.333.423 (133.33.333.423)' can't be established.
ECDSA key fingerprint is SHA256:e1Ld0rwMrVig.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/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
root@1133.33.333.423's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@133.33.333.423'"
and check to make sure that only the key(s) you wanted were added.
  1. 尝试连接,成功后,修改sshd_config配置文件
# 修改配置文件
vim /etc/ssh/sshd_config

PasswordAuthentication no     //yes改为no,禁用密码登陆,确保不需要密码登陆再修改
ChallengeResponseAuthentication no  //yes改为no
PubkeyAuthentication yes  //去掉前面的注释允许密钥
AuthorizedKeysFile .ssh/authorized_keys  //去掉前面的注释 密钥文件位置
  1. 为了安全起见,在客户端本地设置以下文件权限
chmod -R  700 ~/.ssh
chmmd 600 ~/.ssh/authorized_keys

通过pem文件登陆

有些情况下,比如亚马逊EC2服务器,默认只能通过创建密钥对登陆,密钥文件为pem格式的,这时候可以使用i参数来登陆,前提要保证pem文件的权限为600甚至为400,否则可能Permission denied报错

示例:

ssh -i ~/.ssh/test.pem user@ip

通过别名登陆

虽然通过密钥对登陆,可以省去输入密码的步骤,但还要输入ip地址或者主机名,还是有点麻烦,通过配置sshconfig文件,可以达到通过别名登陆的效果即ssh 别名,这样会更方便些。编辑配置文件:vim ~/.ssh/config

配置文件如下:

Host test1 #别名
	HostName ssh.test1.com #主机名
	User root #登陆用户
	Port 22 #端口
	
Host test2
	HostName ssh.test1.com
	User root
	Port 22
	IdentityFile ~/.ssh/test.pem #密钥文件路径

使用别名登陆

ssh test1 #登陆ssh.test1.com 远程主机
ssh test2 #登陆ssh.test2.com 远程主机

参考链接

  1. SSH原理与运用(一):远程登录
  2. 使用 SSH config 文件
0

评论区