ubuntu - 3.常用命令 - ssh

Publish Data: 2025-3-10

安装sshd服务

tianjh@ubtdlts2024:~$ sudo apt install openssh-server 

 

启用sshd服务

tianjh@ubtdlts2024:~$ sudo systemctl start ssh.service

 

从它机连接

PS C:\Users\tianjh> ssh tianjh@192.168.0.36
ssh: connect to host 192.168.0.36 port 22: Connection refused
PS C:\Users\tianjh> ssh tianjh@192.168.0.36
The authenticity of host '192.168.0.36 (192.168.0.36)' can't be established.
ED25519 key fingerprint is SHA256:+KjbYiHAKxrPnlgrjHHtMLUp+SgEqwcIYACATD6reZ4.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.36' (ED25519) to the list of known hosts.
tianjh@192.168.0.36's password:
Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.11.0-19-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

扩展安全维护(ESM)Applications 未启用。

38 更新可以立即应用。
要查看这些附加更新,请运行:apt list --upgradable

1 个额外的安全更新可以通过 ESM Apps 来获取安装。
可通过以下途径了解如何启用 ESM Apps:at https://ubuntu.com/esm


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

tianjh@ubtdlts2024:~$

 

允许root用户登录

#启用root用户
sudo passwd root

#启用sshd root用户访问
#在文件/etc/ssh/sshd_config.d/50-cloud-init-conf 末尾增加下面一行后,保存后退出:
PermitRootLogin yes

echo "PermitRootLogin yes" | sudo tee -a /etc/ssh/sshd_config.d/50-cloud-init.conf > /dev/null

 

使用证书登录ssh

ssh-keygen
ssh-keygen用来生成ssh公钥认证所需的公钥和私钥文件。
ssh秘钥登录特点:1.安全;2.免输密码。
对于安全级别较高的服务器,建议配好ssh登录后禁掉密码登录。
缺点:略繁琐。如果你的只是临时登录一次,那么还是密码吧。
一、生成秘钥
秘钥对需要在你自己的机器上生成,然后把公钥放到服务器相应用户的~/.ssh目录
执行下面命令,默认生成位置是~/.ssh
ssh-keygen

系统会询问你文件名和秘钥密码,可以一路回车过去,会生成两个文件:
id_rsa 私钥
id_rsa.pub 公钥
默认使用rsa算法,你也可以用比较详细的指令,如
ssh-keygen -t rsa -b 1024 -f yourkeyname -C "备注"

参数解释-b采用长度1024bit的密钥对,b=bits,最长4096,不过没啥必要-t rsa采用rsa加密方式,t=type-f生成文件名,f=output_keyfiles-C备注,C=comment
更多参数可运行 man ssh-keygen
二、在服务器上安装秘钥
把上一步生成的公钥发送到服务器(scp,FillZilla等)上,然后在服务器上执行下面命令
cat id_rsa.pub >> ~/.ssh/authorized_keys

如此便完成了公钥安装,有个小坑值得一提:authenrized_keys的权限必须是600或更小,否则会连接失败。
保险起见,执行下面命令
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh

另外,.ssh目录的owner必须是ssh登录用户,不能是root
服务器ssh配置
修改服务器上的ssh配置文件,位置:/etc/ssh/sshd_config
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

PermitRootLogin no //禁止root登录
PasswordAuthentication yes //允许密码登录,根据你的情况设置

然后重启ssh服务
service sshd restart
或
systemctl restart sshd

三、连接服务器
方法1: 直接ssh
ssh -i ~/.ssh/id_rsa -p 22 user@yourservername

方法2(推荐):修改~/.ssh/config
Host server_alias(你的服务器别名)
HostName test.com/192.168.1.1(域名或IP)
Port 22
User user
Identifier id_rsa

保存后,登录时只需执行
ssh server_alias

多个服务器另起一行续写就行了,就是这么简单!

证书登录示例

PS C:\Users\tianjh> ssh -i .\ubtdesktop  tianjh@192.168.0.36
Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.11.0-19-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

扩展安全维护(ESM)Applications 未启用。

38 更新可以立即应用。
要查看这些附加更新,请运行:apt list --upgradable

1 个额外的安全更新可以通过 ESM Apps 来获取安装。
可通过以下途径了解如何启用 ESM Apps:at https://ubuntu.com/esm

Last login: Mon Mar 10 10:30:49 2025 from 192.168.0.100

 

使用ssh连接建立隧道

#持久化 SSH 会话:
ssh -D 1080 -f -C -q -N user@remotehost
-f 让 SSH 在后台运行。
-C 启用压缩。
-q 减少日志输出。
-N 告诉 SSH 不要执行远程命令。

 

相关ssh连接:

https://zhuanlan.zhihu.com/p/759355798

https://www.cnblogs.com/spacelit/p/17936899

 

posted @ 2025-03-10 10:52  lightway  阅读(28)  评论(0)    收藏  举报