openssh基本配置

蒜香大龙虾 2024-08-08 00:58:51
Categories: Tags:

openssh-server基本配置

安装

1
yum install openssh-server -y

启动&防火墙

1
2
3
systemctl enable --now sshd
firewall-cmd --add-port=22/tcp
firewall-cmd --add-port=22/tcp --permanent

常用修改配置文件

配置文件路径为/etc/ssh/sshd_config

允许root登录

修改为yes并取消注释

1
#PermitRootLogin prohibit-password

变成:

1
PermitRootLogin yes

同理,禁止root登录则修改为no

1
PermitRootLogin no

修改完要重启服务

修改端口

原配置为

1
#Port 22

取消注释,然后修改

1
Port 1145

连接时要使用-p指定端口

1
ssh root@172.20.20.11 -p 1145

修改完要重启服务

配置免密

查看家目录下有无秘钥

1
ls ~/.ssh/

无输出则先生成秘钥

1
ssh-keygen -f ~/.ssh/id_rsa -N "" -t rsa

拷贝公钥到目标主机

1
ssh-copy-id root@172.20.20.12

以下地方需要交互

1
2
3
4
5
# 这里要输入yes
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

# 这里输入密码,没有回显
root@172.20.20.12's password:

完成后即可ssh免密

1
ssh root@172.20.20.12

远程执行命令

1
ssh root@172.20.20.12 hostname

如果命令执行失败,提示没有终端,则需要添加-tt参数伪终端连接

1
ssh root@172.20.20.12 -tt hostname