-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_setup.sh
51 lines (45 loc) · 1.32 KB
/
host_setup.sh
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#parameters
relay_name=<relay_ip_address>
relay_port=<relay_port>
reverse_port=<reverse_port>
local_port=<local_listening_port>
# create ssh keypair in /etc/sshtunnel
if [ ! -f /etc/sshtunnel/id_ssh.pub ]; then
sudo mkdir -p /etc/sshtunnel
sudo ssh-keygen -t ed25519 -qN "" -f /etc/sshtunnel/id_ssh
fi
# create sshtunnel.service file
sudo rm -rf /etc/systemd/system/sshtunnel.service
sudo cat << EOF >> sshtunnel.service
[Unit]
Description=Service to maintain an ssh reverse tunnel
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=0
[Service]
Type=simple
ExecStart=/usr/bin/autossh -M 0 -N \\
-R $reverse_port:localhost:$local_port \\
-i /etc/sshtunnel/id_ssh \\
-o ServerAliveInterval=30 \\
-o ServerAliveCountMax=3 \\
-o StrictHostKeyChecking=no \\
sshtunnel@$relay_name -p $relay_port
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
sudo chown root sshtunnel.service
sudo mv sshtunnel.service /etc/systemd/system/sshtunnel.service
# Start the service
sudo systemctl daemon-reload
sudo systemctl enable --now sshtunnel
sudo systemctl restart sshtunnel
# View the ssh public key generated earlier
echo
echo "Copy the following into the relay machine /home/sshtunnel/.ssh/authorized keys file"
echo
cat /etc/sshtunnel/id_ssh.pub
echo
echo