服务器上装Ubuntu24.04装ssh进行远程控制
笔者自己搭建一台网络私有服务器,记录使用Xshell进行远程访问控制服务器。记录下完整安装ssh历程。
1、在装有Ubuntu24.04系统中,能够访问外网,并且服务器能够在公网上被访问到。
1、安装 SSH 服务器
sudo apt update && sudo apt upgrade
sudo apt install openssh-server
2. 配置 SSH 服务器
- 打开 SSH 服务器配置文件:
sudo vim /etc/ssh/sshd_config
- 找到以下行并取消注释:
Include /etc/ssh/sshd_config.d/*.conf
#Port 22
#AddressFamily an
#允许所有IP访问
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
禁止root权限
PermitRootLogin no
开启密钥配置
PubkeyAuthentication yes
旧版本的ssh加密兼容
PubkeyAcceptedKeyTypes +ssh-rsa
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
启用基于RSA密钥的认证方式
RSAAuthentication 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)
KbdInteractiveAuthentication no
# and KbdInteractiveAuthentication to 'no'.
UsePAM yes
X11Forwarding yes
PrintMotd no
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
UseDNS no
# override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server
将上述参数配置完后,保存并关闭配置文件。
重启 SSH 服务器:
sudo systemctl restart ssh
需要注意:如果你启用了防火墙,请确保防火墙打开了 SSH 端口,命令如下:
sudo ufw allow ssh
查看防火墙状态:
sudo ufw status
3、测试 SSH 连接
1、在您的任意一台电脑上,打开xshell软件( 或者SSH 客户机)并连接到您的服务器:笔者是用的xshell软件,首先配置好服务器的加密协议,访问服务器IP,端口号等参数,点击连接。
身份验证这里可以选择密码和密钥,笔者两个都做了,先说密码部分,密码这里需要勾选上,然后点击上图中的连接后,机会提示你输入密码,密码正确,就可以进入你想访问的目标服务器中,
2、输入您服务器的用户名和(sudo)密码时,您应该能够成功连接到您的 SSH 服务器。
4.密钥配置:
通过xshell配置密钥进行连接,通过密钥连接,更安全。
1、通过xshell生产配置为密钥对,操作如下图:
在工具菜单里面有个用户密钥管理,点击进去,
初始是没有的,点击生成,
根据提示,点击下一步
上图中的密码可以有,也可以不设置,看读者自己。
把这全部内容复制,全部内容!!!!!(笔者这里就是没有复制到ssh-rsa,这个开头,倒是配置好久都没有成功)。
2)xshell通过前面密码连接到服务器后,打开authorized_keys密钥配置文件。
sudo vim ~/.ssh/authorized_keys
将您的公钥粘贴到该文件末尾。也就是上图中复制的公钥内容,然后保存关闭(:wq),,,
然后,再打开xshell软件,选择你的服务器,点击连接,出现下面的身份验证。
然后,密码输对,就直接访问成功,
注意:前提是其它客户端(电脑),能够访问到该服务器!!!!