使用密钥方式登录更安全
自用一键设置脚本:(基于windows 10 安装了git)
#!/bin/bash
# 创建SSH目录
mkdir -p ~/.ssh
cd ~/.ssh
# 生成SSH密钥
# 生成SSH密钥
echo -e "\e[32m开始愉快之旅吧\e[0m"
echo -e "\e[32m系统将提示您指定密钥对名称: \e[33m一路回车\e[32m 请按Enter继续\e[0m"
echo
ssh-keygen -t ed25519 -C "注释随意"
# 复制公钥到远程服务器
read -p "请输入SSH端口号(默认为22):" ssh_port
ssh_port=${ssh_port:-22}
read -p "请输入服务器IP地址:" server_ip
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p $ssh_port root@$server_ip
# 修改远程服务器配置
ssh -p $ssh_port root@$server_ip << 'EOF'
if grep -q "^#*PubkeyAuthentication\s*no" /etc/ssh/sshd_config; then
sudo sed -i 's/^#*PubkeyAuthentication\s*no/ PubkeyAuthentication yes/' /etc/ssh/sshd_config
elif grep -q "^#*PubkeyAuthentication\s*yes" /etc/ssh/sshd_config; then
sudo sed -i 's/^#*PubkeyAuthentication\s*yes/ PubkeyAuthentication yes/' /etc/ssh/sshd_config
else
echo "PubkeyAuthentication yes" | sudo tee -a /etc/ssh/sshd_config
fi
sudo service ssh restart
exit
EOF
# 提示用户输入别名和ip
read -p "请输入别名:" alias_name
# 检查~/.ssh/config文件是否存在,如果不存在则创建并添加配置
if [ ! -f ~/.ssh/config ]; then
touch ~/.ssh/config
fi
# 添加别名和IP到~/.ssh/config文件中
if ! grep -q "Host $alias_name" ~/.ssh/config; then
echo "Host $alias_name" >> ~/.ssh/config
echo " Hostname $server_ip" >> ~/.ssh/config
echo " IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config
echo " User root" >> ~/.ssh/config
fi
# 使用SSH密钥登录
echo -e "\e[33m使用 ssh -p $ssh_port $alias_name 愉快登录吧\e[0m"
ssh -p $ssh_port -i ~/.ssh/id_ed25519 root@$server_ip
双击ovh.bat文件一键登录服务器,还是密钥登录,里面就几个字符
教程开始
本地配置密钥对
以windows为例
为了方便代码运行,我安装了Git,因为以下代码在CMD不通用
Git 下载: https://git-scm.com/
安装好后,桌面右键-Git Bash Here
开始整密钥对
mkdir -p ~/.ssh
cd ~/.ssh
ssh-keygen -t ed25519 -C "注释随意"
系统将提示您指定密钥对名称: 建议输入名称 请按Enter
继续
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_ed25519)
接下来,系统会要求您输入安全密码 请按Enter
继续
Enter passphrase (empty for no passphrase)
如果不设置名称,下次使用时会覆盖掉原有的, 如果设置密码短语,则每次使用该密钥登录服务器时,系统都会提示您输入密码短语。(更安全)
我的方式是: 设置名称 不设密码
查看密钥对是否创建:
ls -l | grep "密钥名称"
完成后在C:\Users\Administrator\ .ssh目录下有两个文件,其中
xx.pub
就是公钥文件
将公钥复制到远程服务器
也就是复制公钥内容到 ~/.ssh/authorized_keys 文件
假设你的公钥文件是xx.pub
ip是8.8.8.8
注意: 如果你的服务器默认SSH端口不是22,请使用第二条命令,还要注意如果用户不是root,也要改
ssh-copy-id -i ~/.ssh/xx.pub root@8.8.8.8
如果SSH端口是33
ssh-copy-id -i ~/.ssh/xx.pub -p 33 root@8.8.8.8
提示输入 yes
然后输入你的服务器密码验证, 最后提示下面信息则成功!!
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'username@server_ip_address'"
and check to make sure that only the key(s) you wanted were added.
登录验证
注意: 这里的
xx
是密钥,不是xx.pub
公钥
ssh -i ~/.ssh/xx root@8.8.8.8
如果SSH端口是33
ssh -p 33 -i ~/.ssh/xx root@8.8.8.8
禁用密码登录
sudo vim /etc/ssh/sshd_config #在你的服务器上操作
修改: 按键盘i
进入编辑模式,修改完成按esc键
退出模式,最后输入 :wq
保存退出
主要是第一和第二个
PubkeyAuthentication yes #允许密钥对连接
PasswordAuthentication no #禁止密码登录
ChallengeResponseAuthentication no
UsePAM no
PermitRootLogin yes #默认允许root登录
注意: /etc/ssh/sshd_config.d 目录下是否有其他文件,同样有修改,没有就不管
重启ssh生效
sudo service ssh restart
验证密码登录是否禁止
ssh root@8.8.8.8
如果SSH端口是33
ssh -p 33 root@8.8.8.8
出现: Permission denied (publickey).提示需要密钥对方式登录,则成功
最后大招
回到你的本地电脑,打开git bash
vim ~/.ssh/config
如果没有config文件可以创建,注意没有任何后缀
文件在 C:\Users\Administrator\ .ssh 目录
写入以下代码: 删掉注释,不要有多余空格
Host bb #bb为Host名称 随便一个名称
Hostname 8.8.8.8 #你的IP地址
IdentityFile ~/.ssh/xx #xx私钥路径
User root #用户名
登录验证:
在cmd中或者git bash 输入
ssh bb
##bb为设置的Host名称,即可登录成功 如果之前你设置了安全密码,则需要输入安全密码
ssh bb
如果SSH端口是33
ssh -p 33 bb
桌面新建一个bat文件,写入:
@echo off
ssh bb
双击就登录你的服务器了!!!
其他:
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh