ssh使用

ssh登录

ssh <user_name>@<remote_ip> -p <remote_port> -i <your_key>

ssh端口映射

可以用于不保留端口的情况下,远程连接数据库等。

ssh -N -L <local_port>:localhost:<remote_port> <user_name>@<remote_ip> -p <remote_port> -i <your_key> 

脚本批量映射

需要注意,Nacos有gRPC,除了8848端口外,9848端口也要一起开放。

# Port Mapping
PORTS=(
"ulocalport:localhost:uremoteport"
# MySQL
"53306:localhost:3306"
# Nacos
"58848:localhost:8848"
"59848:localhost:9848"
# Redis
"56379:localhost:6379"
# RocketMQ namesrv
"59876:localhost:9876"
# RocketMQ broker
"510911:localhost:10911"
)

ARGS=()
for port in "${PORTS[@]}"; do
ARGS+=(-L "$port")
done

ssh -o ServerAliveInterval=60 -N "${ARGS[@]}" <username>@<remote_ip>

密钥登录

  1. 首先在本地生成一份密钥,然后将公钥上传到remote的~/.ssh/authorized_keys
  2. 修改remote/etc/ssh/sshd_config
# 新端口
Port 22
# 启用密钥认证
PubkeyAuthentication yes
# 禁用密码登录
PasswordAuthentication no
# 允许Root登录但禁止密码验证
PermitRootLogin prohibit-password
  1. 重启ssh
# Ubuntu/Debian
sudo systemctl restart ssh

# CentOS/RHEL
sudo systemctl restart sshd