在Linux VPS(虚拟专用服务器)的使用中,SSH密钥登录和修改端口是一种常见的安全和便捷操作,本文将详细介绍如何在Linux VPS上使用SSH密钥登录以及如何修改SSH端口。
使用SSH密钥登录
1、生成SSH密钥对
我们需要在本地计算机上生成一对SSH密钥,在终端中输入以下命令:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
这将生成一个RSA密钥对,其中-t
参数表示密钥类型,-b
参数表示密钥位数,-C
参数表示注释信息,运行此命令后,系统会提示您输入保存密钥的路径和设置密码,按照提示操作即可。
2、将公钥复制到VPS
接下来,我们需要将生成的公钥复制到VPS上,在本地计算机上打开公钥文件(默认为~/.ssh/id_rsa.pub
),然后将其内容复制到VPS上的~/.ssh/authorized_keys
文件中,可以使用以下命令:
scp ~/.ssh/id_rsa.pub user@your_vps_ip:~/.ssh/authorized_keys
请将user
替换为您的VPS用户名,将your_vps_ip
替换为您的VPS IP地址。
3、测试SSH连接
现在,我们可以使用SSH密钥尝试连接到VPS,在终端中输入以下命令:
ssh user@your_vps_ip -i ~/.ssh/id_rsa -p your_custom_port
请将user
替换为您的VPS用户名,将your_vps_ip
替换为您的VPS IP地址,将your_custom_port
替换为您自定义的SSH端口,如果一切正常,您应该能够成功连接到VPS。
修改SSH端口
1、修改SSH配置文件
要修改SSH端口,我们需要编辑VPS上的SSH配置文件,使用以下命令打开配置文件:
sudo nano /etc/ssh/sshd_config
2、修改端口号
在配置文件中找到以下行:
Port 22
取消注释(删除行首的符号),并将端口号更改为您想要使用的端口,如果您想将端口更改为2222,则应将其更改为:
Port 2222
3、保存并退出编辑器
按Ctrl + X
,然后按Y
,最后按Enter
键保存更改并退出编辑器。
4、重启SSH服务以应用更改
要使更改生效,我们需要重启SSH服务,使用以下命令重启SSH服务:
sudo systemctl restart sshd
5、测试新的SSH端口连接
现在,您可以使用新的SSH端口尝试连接到VPS,在终端中输入以下命令:
ssh user@your_vps_ip -p your_new_port -i ~/.ssh/id_rsa -o ServerAliveInterval=60 -o ServerAliveCountMax=300 -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvv vv v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v v -o GSSAPIAuthentication=no -o GSSAPICleanupCredentials=no -o PasswordAuthentication=no -o HostbasedAuthentication=no -o PubkeyAuthentication=yes -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=600 -o ControlPath=/root/.ssh/sockets/%r@%h-%p -o TCPKeepAlive=no -o Compression=no -o ClientAliveInterval=60 -o ClientAliveCountMax=300 -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -l user -g users -G users -E utf-8 -e none -F /dev/null -N -T /dev/null -S none -q -O check -f /dev/null -M none -C none -I none -W none -R none -h /root/.ssh/sockets/%r@%h-%p -B /root/.ssh/sockets/%r@%h-%p.sock -%h %p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh/authorized_keys %r@%h:%p ~/.ssh|grep authok |cut -d':' -f2 |sort |uniq |wc -l) "Connecting to ${host}:${port}" &> /dev/null && echo "Connection established." || echo "Connection failed." && exit 1; fi; done < /root/.ssh/*authorized_keys; while true; do sleep 60; kill $MAINPID; done & wait" '"${args[@]}"' > /dev/null 2>&1 & disown && wait $! && [ $? = 0 ] && touch /var/lock/subsys/$progname start && [ $? = 0 ] && subsystem sftp /usr/libexec/openssh/sftp-server && subsystem scp /usr/libexec/openssh/scp-server && [ $? = 0 ] && [pidof $progname
!= '' ] && [ps ax | grep $progname | grep \-\-background | grep -\-nofork | wc -l
= '0' ] && pkill $progname && wait $! && exit; fi' >> /etc/rc.local' 'ExecStartPost=/bin//systemctl restart sshd' EOF >> /etc/rc.local' 'ExecStartPost=systemctl restart nginx' EOF >> /etc/rc.local' 'RestartSec=1min' 'Restart=always' '[Install]' 'WantedBy=multi-user.target' 'User=root' 'Group=root' 'X-Confined-User=root' 'Type=notify' 'Categories=xsession;screensaver;rdp;protocol;remoteaccess;connectionmanager;seamlessrdp;remote-display;pulseaudio;sound;printer;powermanagement;usb;bluetooth;wirelessdevice;inputmethod;networkmanager;x11;desktop
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/260909.html