ssh config & X11 & jump-machine

ssh config常见设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Host node5
HostName node5.xydustc.me #或者ip
IdentityFile ~/.ssh/id_rsa #windows: "C:\Users\Administrator\.ssh\id_rsa"
IdentitiesOnly yes #IdentitiesOnly指定ssh只能使用配置文件指定的identity和certificate文件或通过ssh命令行通过身份验证,即使ssh-agent或PKCS11Provider提供了多个identities。
User shaojiemike
Port 22
ProxyCommand E:\\commonSoftware\\Git\\mingw64\\bin\\connect.exe -S 127.0.0.1:7890 -a none %h %p
# 注意ProxyCommand不能写全局,会代理其他ssh。出现ctrl-c会中断ssh连接之类的错误

Host *
ForwardAgent yes #是否将本地SSH代理转发到远程主机。如果设置为“yes”,则可以在远程主机上使用本地SSH代理,而无需在远程主机上设置新的SSH连接。
AddKeysToAgent yes #是否将私钥添加到ssh-agent中。如果设置为“yes”,则在使用ssh连接时,ssh客户端会自动将私钥添加到ssh-agent中。
ForwardX11 yes
ForwardX11Trusted yes
Compression yes
TCPKeepAlive=yes
ServerAliveInterval 60 # Client每隔 60 秒发送一次请求给 Server,然后 Server响应,从而保持连接
ServerAliveCountMax 3 # Client发出请求后,服务器端没有响应得次数达到3,就自动断开连接,正常情况下,Server 不会不响应

更安全但是更简单的加密 ed25519

Use new way

The error message “userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]” indicates that the SSH server is configured to accept specific public key algorithms, and the client attempted to use the “ssh-rsa” algorithm, which is not included in the accepted algorithms list.

To resolve this issue, you have a few options:

  1. Update SSH Key Algorithm:
    If you are generating a new key pair, consider using a more secure algorithm such as Ed25519 instead of the older RSA algorithm.

    1
    ssh-keygen -t ed25519 -f /path/to/output/keyfile -C "Your Comment Here"
  2. Update Server Configuration:
    If you don’t have control over the client’s key type, you may need to update the server’s SSH configuration to include support for the “ssh-rsa” algorithm. Open the SSH server configuration file (usually located at /etc/ssh/sshd_config), and add or modify the following line:

    1
    PubkeyAcceptedAlgorithms +ssh-rsa

    After making the change, restart the SSH server.

    1
    sudo service ssh restart

    Note: Adding “ssh-rsa” might reduce the security of your SSH server, as RSA is considered less secure than some newer algorithms.

  3. Check Key Types:
    Ensure that you are using the correct key type when attempting to authenticate. If you are using an existing key, make sure it’s the right type (e.g., Ed25519) and not RSA.

Choose the option that best fits your security requirements and constraints. If possible, it’s generally recommended to use more modern and secure key algorithms like Ed25519 over older ones like RSA.

查看 ssh 日志

1
sudo journalctl -u ssh --since "yesterday" |less

X11 forward GUI

windows use mobaxterm, mac use xquartz + iterms

1
ssh -Y [email protected]

跳板机

目的

在管理外网服务器时,出于安全等因素的考虑,我们一般不会把所有服务器都设置成可ssh直连,而是会从中挑选出一台机器作为跳板机,当我们想要连接外网服务器时,我们要先通过ssh登录到跳板机,再从跳板机登录到目标服务器。

密钥认证

  1. 开启ssh-agent,然后将我们的private key添加到ssh-agent中。
1
2
3
4
5
$ eval $(ssh-agent)
Agent pid 8350
$ ssh-add
Identity added: /home/yt/.ssh/id_rsa (yt@arch)
Identity added: /home/yt/.ssh/id_ed25519 (yt@arch)
  1. ssh登录到跳板机(不过此次加上了-A参数,表示开启agent forwarding)。
1
ssh -A [email protected]

或者直接

1
ssh -J [email protected] [email protected]

这条命令将会首先连接到 [email protected] 的跳板机,然后再通过跳板机连接到 [email protected] 的目标服务器。

scp传递数据

1
scp -J [email protected] [email protected]:/path/to/source/file /path/to/destination/file

这个命令将会通过 [email protected] 的跳板机从源文件 /path/to/source/file 复制数据到 [email protected] 的目标文件 /path/to/destination/file

config配置

1
2
3
4
5
6
7
8
9
10
11
Host <name>
HostName 127.0.0.1 #是不是写错了??不是目标ip吗?
User <user>
Port <port>
ProxyCommand ssh <cloud-user>@<cloud-host> -W %h:%p

//example
Host xunfei-V100
HostName 172.31.97.164
User root
ProxyCommand C:\Windows\System32\OpenSSH\ssh.exe node1 netcat -w 120 %h %p

如何判断当前ssh没用跳板机

check is ssh use direct connect not use jump host

基于跳板机的x11转发

google check ssh gui x11 use jump host

https://www.ibm.com/support/pages/how-forward-x11-client-through-jump-host-back-pc-x-emulator

  1. server side
    1. /etc/ssh/sshd_config
      1. X11Forwarding yes
      2. X11UseForwarding yes
    2. install xauth
      1. environment variables on the server. DISPLAY and XAUTHORITY will automatically be set to their proper values.
      2. if DISPLAY is not set, it means ssh is not forwarding the X11 connection.
  2. client side
    1. ForwardX11 yes in ~/.ssh/config
      1. X11UseLocalhost yes
    2. ssh -v -X name@ip # -v for debug

使用跳板机转发 vscode

vlab 能正常登录的情况下ssh -i D:\\PowerShell\\vlab-vm7096.pem [email protected]

有两种设置ssh config设置方法

1
2
3
4
5
6
7
8
9
10
11
Host jumpSnode6Ipv4W
Hostname 202.38.72.23
User shaojiemike
Port 22
ProxyCommand C:\\Windows\\System32\\OpenSSH\\ssh.exe -W %h:%p -i D:\\PowerShell\\vlab-vm7096.pem ubuntu@vlab.ustc.edu.cn

Host jumpSnode6Ipv4
Hostname 202.38.72.23
User shaojiemike
Port 22
ProxyCommand C:\\Windows\\System32\\OpenSSH\\ssh.exe -i D:\\PowerShell\\vlab-vm7096.pem ubuntu@vlab.ustc.edu.cn netcat -w 120 %h %p

参考文献

https://cloud.tencent.com/developer/article/1501977

Homepage great template

个人网站的需求分析

功能需求

  1. 博客的标签和分类是必须的
  2. 搜索功能、评论功能(Mkdocs 内置搜索)
  3. icon support and usage

个人需求

  1. 少图片的冷淡风
  2. 但是要有彩色或者加粗等功能来体现文中的重要程度。(Mkdocs 的提示框真好用)

个人主页

  1. 基于Mkdocs
  2. Hexo icarus
  3. Hugo PaperMod
  4. Hexo matery

部署位置

cloudflare pages

  1. 不同于之前的教程, 创建位置有所调整。
    1. Workers & Pagescreate an application, choose the Pages tab and follow the git connection tutorial.
    2. 最大的优点是集成了github和mkdocs,可以直接运行mkdocs build 实现部署

GitHub pages

https://kirrito-k423.github.io/

使用自己的域名

替换shaojiemike.pages.dev 变成自己的域名 shaojiemike.top: cloudflare pages 下直接提供Custom domains 的解析 CNAME www shaojiemike.pages.dev

SSHForward

导言

服务器没网,姜师兄说可以ssh转发网络请求到本地windows

四类ssh转发

SSH 端口转发自然需要 SSH 连接,而SSH 连接是有方向的,从 SSH Client 到 SSH Server 。

而我们所要访问的应用也是有方向的,应用连接的方向也是从应用的 Client 端连接到应用的 Server 端。比如需要我们要访问Internet上的Web站点时,Http应用的方向就是从我们自己这台主机(Client)到远处的Web Server。

本地转发Local Forward

如果SSH连接和应用的连接这两个连接的方向一致,那我们就说它是本地转发。

1
2
3
ssh -L [bind_address:]port:host:hostport <SSH hostname>
ssh -L 3333:127.0.0.1:2333 -vN -f -l shaojiemike 222.195.72.218
debug1: Local connections to LOCALHOST:3333 forwarded to remote address(222.195.72.218) 127.0.0.1:2333


本地转发在本地这台机器上监听一个端口,然后所有访问这个端口的数据都会通过ssh 隧道传输到远端的对应端口上。命令中的 host 和 <SSH hostname> 可以是不同的主机。

远程转发Remote Forward

如果SSH连接和应用的连接这两个连接的方向不同,那我们就说它是远程转发。

1
ssh -R [bind_address:]port:host:hostport <SSH hostname>

远程转发与本地转发正好相反,打开ssh隧道以后,在远端服务器监听一个端口,所有访问远端服务器指定端口都会通过隧道传输到本地的对应端口上,下面是例子。

动态转发

  • TODO

X转发

  • TODO

实用参数

1
2
3
4
-C:压缩数据传输。
-f :后台认证用户/密码,通常和-N连用,不用登录到远程主机。
-N :不执行脚本或命令,通常与-f连用。
-g :在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。
1
2
3
4
5
6
-f      Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask for passwords or
passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 programs at a remote
site is with something like ssh -f host xterm.

If the ExitOnForwardFailure configuration option is set to "yes", then a client started with -f will wait for all remote port
forwards to be successfully established before placing itself in the background.

常见例子

将发往本机的80端口访问转发到174.139.9.66的8080端口

1
ssh -C -f -N -g -L 80:174.139.9.66:8080 [email protected]

将发往174.139.9.66的8080访问转发到本机的80端口

1
ssh -C -f -N -g -R 80:174.139.9.66:8080 [email protected]

使用远程管理服务器上的MySQL

1
ssh -C -f -N -g -L 80:174.139.9.66:8080 [email protected]

一次同时映射多个端口

1
ssh -L 8888:www.host.com:80 -L 110:mail.host.com:110 -L    25:mail.host.com:25 user@host -N  

反向隧道技术

  1. 先在公司机器(LAN_ip)上执行
    1
    ssh -NfR 2222:localhost:22 home_ip
    -R : 建立反向连接 将 home_ip port转发
  2. 然后到home_ip上面
    ssh localhost -p 2222

SOCKS代理

1
ssh -NfD 8888 user@remote_ip

现在在浏览器socks 5 proxy设置为localhost:8888,所有之前无法访问的网站现在都可以访问了。

假设本地主机A提供了HTTP服务,主机B无网络

1
ssh -fNgR 80:localhost:80 root@host-B

通过访问 http://host-B 来访问主机A上的HTTP服务了。

如果没有主机B的root账号,则只能远程转发到1024以后的端口号

1
ssh -fNgR 8080:localhost:80 lige@host-B

通过访问http://host-B:8080 来访问主机A上的HTTP服务

假设本地主机A无网络,主机B提供了HTTP服务

但是由于怕防火墙屏蔽,而不想直接访问

1
ssh -fNgL 80:localhost:80 root@host-B

ssh_config设置技巧

客户端

.ssh/config修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Host *
ControlPersist yes
ControlMaster auto
ControlPath /tmp/sshcontrol-%C
ControlPersist 1d
# 以上四条配合使用,实现多条ssh连接共享,而且保持1天内ssh存在。再次执行ssh命令几乎秒连
TCPKeepAlive=yes
# 发送空TCP包来保持连接,但是可能被防火墙过滤
ServerAliveInterval 30
# 表示每隔多少秒(30秒),从客户端向服务器发送一次心跳(alive检测)
# 心跳具体格式: debug1: client_input_global_request: rtype [email protected] want_reply 1
ServerAliveCountMax 240
# 表示服务端多少次(240次)心跳无响应后, 客户端才会认为与服务器到SSH链接已经断开,然后断开连接。

Port 443

Host *
ForwardAgent yes
# 可以讓本地的 SSH Key 在遠端 Server 上進行轉送,也就是经过跳板机Server1,使用本地key访问Server2,不用把key传到Server1上导致泄露
# 虽然Server1不会获得key,但是可以使用key。所以该选项不宜用于Host *,应该只添加您信任的服务器以及打算用于代理转发的服务器。
# 注意跳板机需要设置允许代理转发, /etc/ssh/sshd_config 将AllowAgentForwarding的值设置为yes, 并重启服务
AddKeysToAgent yes
ForwardX11 yes
ForwardX11Trusted yes
Compression yes
# 压缩,加快数据传输速度

服务器端

更改ssh服务器的配置文件/etc/ssh/sshd_config

1
2
3
ClientAliveInterval 60
# 默认是0,不发送
ClientAliveCountMax 3

原理同上
重启ssh服务以使配置生效

1
systemctl restart sshd

服务器端如何将端口绑定到外部地址上

我们可以把这个映射的端口绑定在0.0.0.0的接口上,方法是加上参数-b 0.0.0.0。

同时修改SSH服务器端 /etc/sshd_configGatewayPorts noGatewayPorts yes来打开它。

自动重连/保持长时间连接

1
2
Host *
ServerAliveInterval 60

检查隧道状态

  1. netstat
  2. ps
  3. autossh

实践

服务器网站端口转发到本地

1
ssh -L 6006:127.0.0.1:6006 -N -f -l acsacom snode6.swangeese.fun

给服务器提供网络代理

本地网络代理到服务器

1
2
ssh -fNgR 7333:127.0.0.1:7890 [email protected]
ssh -fNgR 7333:127.0.0.1:80 [email protected]
  • 7333数字不要太小,以免冲突。
  • 7890是本地clash端口,80也可以。
title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#YJH proxy
export proxy_addr=localhost
export proxy_http_port=7890
export proxy_socks_port=7890
function set_proxy() {
export http_proxy=http://$proxy_addr:$proxy_http_port #如果使用git 不行,这两个http和https改成socks5就行
export https_proxy=http://$proxy_addr:$proxy_http_port
git config --global https.proxy http://$proxy_addr:$proxy_http_port
git config --global https.proxy https://$proxy_addr:$proxy_http_port
export all_proxy=socks5://$proxy_addr:$proxy_socks_port
export no_proxy=127.0.0.1,.huawei.com,localhost,local,.local
}
function unset_proxy() {
git config --global --unset http.proxy
git config --global --unset https.proxy
unset http_proxy
unset https_proxy
unset all_proxy
}
function test_proxy() {
curl -v -x http://$proxy_addr:$proxy_http_port https://www.google.com | egrep 'HTTP/(2|1.1) 200'
# socks5h://$proxy_addr:$proxy_socks_port
}
# set_proxy # 如果要登陆时默认启用代理则取消注释这句

服务器git下载设置代理端口

1
2
3
4
5
6
export http_proxy=http://127.0.0.1:7333
# wget 正常使用
export all_proxy=socks5://127.0.0.1:7333
# git 正常使用
unset http_proxy
unset all_proxy

特定软件也需要设置代理

1
2
3
4
5
6
7
git config --global https.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

git config --global --unset http.proxy
git config --global --unset https.proxy

mac上转发失败

首先看看Windows上的输出


Mac的错误

在完成windows所有输出后,

  1. 首先调用了shell
  2. 错误
1
2
3
debug1: Remote: Forwarding listen address "localhost" overridden by server GatewayPorts
debug1: remote forward failure for: listen 7890, connect 127.0.0.1:7890
Warning: remote port forwarding failed for listen port 7890

猜测应该是已经端口占用了

1
netstat -nat |grep -i '7233'

解决办法一:换端口

首先,可以选择换端口,换成 7233

1
sudo lsof -i TCP:7233
  1. 进程验证
  2. wget验证
  3. curl验证(原本会走WLT)
1
redirecting to http://wlt.ustc.edu.cn


解决办法二:Kill掉相关进程

1
2
3
4
sudo lsof -i TCP:7890
sudo kill -9 process_id_1 process_id_2 process_id_3
sudo ps -ef | grep 'nc -X' | grep -v grep | awk '{print $2}' | sudo xargs -r kill -9
sudo lsof -i TCP:7233 |grep shaojiemike| awk '{print $2}'|sudo xargs -r kill -9

  • FD: File Descriptor number of
  • 不敢kill师兄的,然后发现并没有用,寄。kill掉师兄的之后就行了,嘻嘻~~ 师兄不要怪我

参考文献

http://blog.sina.com.cn/s/blog_704836f40100lwxh.html

https://blog.csdn.net/xyyangkun/article/details/7025854?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.no_search_link