Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Last active October 17, 2022 12:25
Show Gist options
  • Save MarshalW/c18c6756ae7cbbfbb814050e5f6b89ee to your computer and use it in GitHub Desktop.
Save MarshalW/c18c6756ae7cbbfbb814050e5f6b89ee to your computer and use it in GitHub Desktop.
树莓派4 headless 安装 autossh 全过程

树莓派4 headless 安装 autossh 全过程

通过 rpi 网站 下载 rpi imager,并安装。

通过 rpi imager 下载最新版本 ubuntu server 64bit 版本(22.04),设置正确的参数。

image

设置 public key 登录方式。

安装后,找到 ip 地址,可以通过本地路由器查找。

设置 apt mirror ( 参考 清华大学镜像 - 树莓派):

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ jammy-proposed main restricted universe multiverse

更新和升级系统:

sudo apt update && sudo apt upgrade -y

安装 autossh:

sudo apt-get install autossh -y

不交互生成当前用户 ssh key:

< /dev/zero ssh-keygen -q -N ""
# 或者
ssh-keygen -q -t rsa -b 4096 -N '' -f ~/.ssh/id_rsa

将当前用户 public key 在跳板机设置 /home/$jumper-user/.ssh/authorized_keys

创建 autossh service:

check_port=22001
tunnel_port=10140
jumper_host=jumper-server

cat >/tmp/autossh.service <<EOF
[Unit]
Description=AutoSSH service for a reverse tunnel from some.example.com to localhost
After=network-online.target

[Service]
User=pi
Group=adm
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M $check_port -N -T -q -o ServerAliveInterval=30 -o ServerAliveCountMax=10 -R $tunnel_port:localhost:22 $jumper_host
Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target
EOF

sudo cp /tmp/autossh.service /etc/systemd/system
sudo systemctl enable autossh.service
sudo systemctl start autossh.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment