服务器端和客户端的配置ssh连接
在服务器端开启ssh服务,并配置客户端到服务器端的ssh连接
服务器端
检查ssh服务状态
如果ssh服务正在运行,则输出
> systemctl status ssh
Active: active (running) since ...
如果ssh服务器没有安装或未运行,则输出
> systemctl status ssh
Unit ssh.service could not be found.
检查ssh服务是否安装
如果已安装则会输出路径
> which sshd
/usr/sbin/sshd
安装ssh服务并设置开机自启动
> sudo apt install openssh-server # 安装ssh服务
> sudo systemclt start ssh # 启动ssh服务
> sudo systemclt enable ssh # 设置ssh服务开机自启动
> systemclt status ssh # 验证ssh服务状态
> netstat -tpln # 查看ssh服务端口
配置ssh服务
在/etc/ssh/sshd_condig添加
ClientAliveInterval 60
然后重启ssh服务
> sudo systemclt restart ssh # 重启ssh服务
客户端
完整的ssh连接命令冗长
> ssh -L post:localhost:host_port username@server_ip -p ssh_port
> ssh username@server_ip -p ssh_port # 简化版
使用密码连接服务器,其中port为客户端本地端口,host_port为服务器端口,username为服务器用户名,server_ip为服务器ip地址,ssh_port为服务器ssh端口
> ssh -i ~/.ssh/id_rsa_xxx post:localhost:host_port username@server_ip -p ssh_port
> ssh -i ~/.ssh/id_rsa_xxx username@server_ip -p ssh_port # 简化版
使用私钥连接服务器,其中~/.ssh/id_rsa_xxx为服务器私钥路径(私钥可以来自客户端,或服务器端)
创建密钥对
> ssh-keygen -t rsa -C "email@xxx.com"
如果在客户端创建密钥对,需要将客户端公钥内容复制到服务器的~/.ssh/authorized_keys文件中,如果没有则新建。公钥内容应该是ssh-rsa开头,email@xxx.com结尾
如果在服务器端创建密钥对,需要将服务器端私钥内容复制到客户端,并对私钥进行适当的权限设置,通常是600或400
> chmod 600 ~/.ssh/id_rsa_xxx
推荐,别名登陆
在客户端获得用于ssh连接的私钥后,在客户端~/.ssh/config文件中添加如下内容,没有config文件则新建:
Host server_alias
HostName server_ip
LocalForward post localhost:host_port # 可以省略这一行
Port ssh_port
User username
IdentityFile ~/.ssh/id_rsa_xxx # 默认是`~/.ssh/id_rsa`,非指定可以省略这一行
其中server_alias是服务器别名,IdentityFile配置本地私钥路径,用于配置连接多个服务器的情况
配置完成后,可以在终端输入下面内容连接服务器
> ssh server_alias

浙公网安备 33010602011771号