Ubuntu 初始设置:启用 root 账户、启用密码登录、启用Key登录、ssh超时、修改主机名
新购买的 Ubuntu 服务器,默认禁用了 root 账户,如果想启用 root 账户的公钥登录,请使用以下方法:
1、打开 ssh 配置文件
vi /etc/ssh/sshd_config
2、修改ssh配置文件
PermitRootLogin yes # 把PermitRootLogin prohibit-password 改为 PermitRootLogin yes
PubkeyAuthentication yes # 开启公钥登录
3、生成公钥和私钥
ssh-keygen # 连续输入3次回车键,完成创建
4、进入 /root/.ssh
cd /root/.ssh
5、把公钥写入 authorized_keys 文件(公钥放在服务端,私钥在客户端)
cat id_rsa.pub >> authorized_keys
6、重启 ssh 服务
systemctl restart ssh # 重启ssh服务
7、把私钥 private_key(id_rsa) 下载到客户端本地(私钥放在客户端),并修改私钥的权限
chmod 600 ./private_key # 修改私钥的权限:仅当前用户可读写
8、客户端通过下面的 ssh 命令登录服务器
ssh -i path/to/private_key root@xxx.xxx.xxx.xxx
9、(如有需要)开启密码登录:
vi /etc/ssh/sshd_config # 编辑ssh配置文件
PasswordAuthentication yes # 开启密码登录
sudo passwd root # 设置root密码, 根据提示,输入2次密码
systemctl restart ssh # 重启ssh服务
10、如果 ssh 连接超时断开(尽量在客户端配置)
# 编辑
vi ~/.ssh/config
# 全局配置
Host *
ServerAliveInterval 60
ServerAliveCountMax 5
11、修改主机名,也就是 bash/zsh 中看到的:root@xxx$:
hostnamectl # 查看主机名
hostnamectl set-hostname xxx # 设置主机名

浙公网安备 33010602011771号