一、修改ssh_d的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sudo vim /etc/ssh/sshd_config
# 以下为常用配置(顺序不一)
Port 22 # 监听端口为22(默认)
ListenAddress 0.0.0.0
# 监听地址为任意网段,也可以指定OpenSSH服务器的具体IP

PermitRootLogin yes # 允许root用户登录
PubkeyAuthentication yes # 使用公钥登录
AuthorizedKeysFile .ssh/authorized_keys # 设置公钥的位置
PasswordAuthentication no # 禁止使用密码登录
PermitEmptyPasswords no # 禁止空密码用户登录

#### 其他 ####
# 只允许zhangsan、lisi用户登录
# 且其中lisi用户仅能够从IP地址为61.23.24.25 的主机远程登录
AllowUsers zhangsan lisi@61.23.24.25 # 多个用户以空格分隔
# 禁止某些用户登录(wangwu),用法于AllowUsers 类似(注意不要同时使用)
DenyUsers wangwu

生成密钥对

若禁用公钥登录并启用密码登录,则可以省去这一步

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"


~/.ssh/id_rsa移到需要连接sshd的主机上
~/.ssh/id_rsa.pub移到被连接sshd的主机上并改名为authorized_keys

三、设置开机自启服务

1
2
3
4
# 设置开机自启动
sudo systemctl enable ssh
# 取消开机自启动
sudo systemctl disable ssh

四、ssh常用命令

1
2
3
4
5
6
7
8
9
10
# 连接IP的sshd
ssh ip
# 连接IP的user用户的sshd
ssh user@ip
# 打开sshd
sudo service sshd start
# 关闭sshd
sudo service sshd stop
# 查看sshd状态
sudo service sshd status