跳到主要内容

sshd

1、通过配置 ~/.ssh/config 文件

错误信息:bad permissions

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0664 for '/home/xxx/.ssh/xxx_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/xxx/.ssh/xxx_rsa": bad permissions

问题原因:秘钥的权限不对,应该改为 600,执行命令 chmod 600 xxx_rsa

2、修改 SSH 端口

vim /etc/ssh/sshd_config
# 把 Port 22 改成其他的端口

# 以后在连接的时候,通过 -p 来指定端口

3、使用 ssh 远程其他机器的时候,如果一段时间没有操作,就会假死,即不能操作了

vim /etc/ssh/sshd_config
# 插入
ClientAliveInterval 30
ClientAliveCountMax 6

# 修改之后需要重启 sshd 服务
systemctl restart sshd

4、生成 rsa

ssh-keygen -t rsa

生成的文件会存放到 /root/.ssh 中
私钥:idea_rea ,公钥:idea_rsa.pub

5、免密登录

cd ~/.ssh
cat id_rsa.pub >> authorized_keys
chmod 600 authorized_keys

生成 rsa 秘钥

ssh-keygen -m PEM -t rsa -b 4096 -f ~/.ssh/git_id_rsa

6、设置允许使用密码登录

# 设置为允许通过密码登录
vim /etc/ssh/sshd_config
> PasswordAuthentication Yes

# 重启 sshd 服务
systemctl restart sshd

7、删除 known_hosts 中的指定记录

ssh-keygen -R cc  

8、通过 ssh user@hostname 登陆时,默认使用 ~/.ssh/id_rsa 密钥。

9、通过 ssh 远程执行脚本

cd $(dirname $0)
# 带上 /bin/bash 可以避免每次执行的时候都会输出
# Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh cloud /bin/bash << EOF
# 注意,等号两边不能有空格
current_date=$(date +"%Y-%m-%d")
# 需要加上 \ 进行转义
echo "当前日期:"\$current_date
EOF

10、Permission denied (publickey).

chmod 700 /root/.ssh
chown root:root /root/.ssh

chmod 600 /root/.ssh/authorized_keys
chown root:root /root/.ssh/authorized_keys

11、执行 sudo 时不输入密码

visudo  /etc/sudoers
ubuntu  ALL=(ALL:ALL) NOPASSWD: ALL