脚本操作指南参见:Ubuntu Shell 脚本操作
本脚本自动执行:
生成
ed25519密钥对配置
config文件配置
known_hosts文件输出公钥
gitee_keys.sh
#!/bin/bash
# 生成SSH密钥对
ssh-keygen -t ed25519 -C "Gitee SSH Key" -f ~/.ssh/key-ubuntu-gitee -P ""
# 定义要添加的内容
new_config="Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/key-ubuntu-gitee"
new_config_host="gitee.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEKxHSJ7084RmkJ4YdEi5tngynE8aZe2uEoVVsB/OvYN gitee.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMuEoYdx6to5oxR60IWj8uoe1aI0XfKOHWOtLqTg1tsLT1iFwXV5JmFjU46EzeMBV/6EmI1uaRI6HiEPtPtJHE= gitee.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMzG3r+88lWSDK9fyjcZmYsWGDBDmGoAasKMAmjoFloGt9HRQX2Qp4f9FY2XK/hsHYinvoh5Xytl9iaUNUWMfYR8q6VEMtOO87DgoAFcfKZHt0/nbAg9RoNTKYt6v8tPwYpr7N0JP/01nE4LFsNDnstr6H0bXSAzbKWCETLZfdPV4l2uSpRn3bU0ugoZ0aSKz5Dc/IloBfGCTvkSsxUydMRd/Chpjt6VxncDbp+Fa6pzsseK8OQzrg6Fgc5783EN3EQqZ2skqyCwExtx95BJlfx1B3luZnWfpkwNDnrZRT/Qx0OrWqyf0q6f9uQr+UG1S8qDcUn3e/9onq3rwBri8/"
# 检查文件是否存在,如果不存在则创建
config_file="/root/.ssh/config"
if [ ! -f $config_file ]; then
touch $config_file
fi
config_file_host="/root/.ssh/known_hosts"
if [ ! -f $config_file_host ]; then
touch $config_file_host
fi
# 检查是否已经存在相同配置,如果不存在则追加到文件末尾
if ! grep -q "Host gitee.com" $config_file; then
echo "$new_config" >> $config_file
fi
if ! grep -q "gitee.com ssh-ed25519" $config_file_host; then
echo "$new_config_host" >> $config_file_host
fi
# 显示公钥内容
cat ~/.ssh/key-ubuntu-gitee.pub