SSH Server Project

August 11, 2024, 23:16

sparkeras

okay guys i’m pretty new to the pi and to linux so bear with me as i have an idea. i’d like to leave my pi on at university 24/7 connected to an external hard drive and to the campus wifi. the idea being that when im working on my laptop i can ssh to my pi to send, receive and backup files to its external hard drive rather than storing everything i ever do on my laptop’s linux partition. obviously i cannot modify the router settings at all so how can i set it up so that i can ssh into my pi on any network? i guess i need to forward a port? i have only managed to ssh through ethernet so far and i should be able to figure it out on a LAN but this is a little different.

illegitimate_egg

You've invented a NAS

sparkeras

yeah but

sparkeras

not on the same network

illegitimate_egg

Accessing outside the network is a little harder

illegitimate_egg

as you said you can't forward the port

sparkeras

okay in theory it is but i have no idea how they set up their wifi across a whole campus

illegitimate_egg

so either run a VPN somewhere you can and then bridge them

illegitimate_egg

or use a service like ngrok to do that for you

illegitimate_egg

then you can do normal ftp/sftp file transfers all day long

sparkeras

i want to do it in the most complicated and manual and involved way possible

sparkeras

so that i can learn more about it

illegitimate_egg

ah

sparkeras

whats bridging

sparkeras

and wouldnt i have to choose an ip

illegitimate_egg

here it's just connecting the pi to a vpn

sparkeras

is that a vpn on the server or laptop side

illegitimate_egg

but you must run it yourself

illegitimate_egg

you need a second server

illegitimate_egg

that's the trick

illegitimate_egg

somewhere where you can port forward

sparkeras

i have four raspberry pis

illegitimate_egg

more than I have

sparkeras

but i only have access to the one network

sparkeras

for internet

illegitimate_egg

not even your home?

sparkeras

okay would that work

illegitimate_egg

you could run the nas there

sparkeras

if i left one running at home 24/7 and the actual hardware server at uni

sparkeras

okay

sparkeras

other way round

illegitimate_egg

then you only need one

illegitimate_egg

just forward it at home and then connect as normal

sparkeras

is that like no1 way to get attacked

sparkeras

you have to do a lot of privacy stuff i think

illegitimate_egg

be careful

illegitimate_egg

only forward the ports you need

illegitimate_egg

update the software regularly

illegitimate_egg

and use software that gets updated obviously

illegitimate_egg

if you do that you should be safe

illegitimate_egg

do not expose port 22

illegitimate_egg

expose port 21 for ftp

sparkeras

so im guessing theres a port for ftp

sparkeras

yep

sparkeras

and if i forward that via my router at home

illegitimate_egg

port 22 would probably be safe to forward

sparkeras

i can access my pi at home from my laptop on any network

illegitimate_egg

that would give you ssh and sftp too

illegitimate_egg

yeah

sparkeras

okay so forwarding and exposing two different things

illegitimate_egg

and the trick with sftp and ssh is that first s

illegitimate_egg

secure

illegitimate_egg

if you forward 21 and 22 and keep software up to date it should be fine

sparkeras

and i can update my pi via ssh anyway?

illegitimate_egg

yeah

illegitimate_egg

or schedule it to do it automatically

sparkeras

so im guessing theres a bunch of commands in the cli i can use to transfer all the files on my laptop like music and photos etc

illegitimate_egg

any sftp or ftp client will do

sparkeras

without a gui drag and drop

illegitimate_egg

no

illegitimate_egg

you can have a gui

illegitimate_egg

easy

illegitimate_egg

for windows there's WinSCP or filezilla

illegitimate_egg

linux also supports filezilla

illegitimate_egg

and winscp

illegitimate_egg

just pick one you like

illegitimate_egg

most file managers can do it too

sparkeras

ohhh i see

sparkeras

so i just tell it where to go (my raspberry pi port)

sparkeras

and drag and drop

illegitimate_egg

not even port

illegitimate_egg

it's smart enough to now

illegitimate_egg

just needs the forwarded IP

illegitimate_egg

which is your home IP address btw

illegitimate_egg

I don't want to see that

illegitimate_egg

some people do

sparkeras

do i NEED a ftp client like in theory could i do this all through ssh and port forwarding and cli and stuff

illegitimate_egg

It would be hard to move files otherwise but technically no

illegitimate_egg

sftp is pretty good

illegitimate_egg

doesn't even require any extra software

illegitimate_egg

does it all over ssh

sparkeras

i see okay

sparkeras

since i have four pis

sparkeras

what else can i do

sparkeras

ive got one built up, one needs a case and two spares

sparkeras

one of em ill use as my NAS which is like a free icloud or whatever

sparkeras

and then i need to figure out what to use the rest for

illegitimate_egg

idk

illegitimate_egg

mine sit around doing nothing

illegitimate_egg

until I write something for them to do

sparkeras

ill just package the other three up and bring them with me then until someone gives me an idea or a project

sparkeras

i want one that runs windows 95

sparkeras

maybe hang them on a keychain or something to show off

illegitimate_egg

that's going to be a major challenge

sparkeras

its been done

illegitimate_egg

Different architectures

illegitimate_egg

that'll be a pain to get working

illegitimate_egg

if you don't follow a guide

sparkeras

hopefully theres a decent one

sparkeras

more learning

illegitimate_egg

Doing it yourself is pretty cool

illegitimate_egg

but emulating x86 is a pain in the ass

illegitimate_egg

emulating the intel 8080 is a pain in the ass

illegitimate_egg

x86 is infinitely worse

illegitimate_egg

QEMU will do it though

sparkeras

can you run the 1990s macOS on a pi?

sparkeras

because the specs were so low back then obviously

illegitimate_egg

that's not what makes it hard

illegitimate_egg

they're completely incompatible

illegitimate_egg

ik QEMU does x86 32

illegitimate_egg

but I don't know about power pc

illegitimate_egg

qemu does power pc

illegitimate_egg

I have now learnt

illegitimate_egg

you could do it

sparkeras

emulation man

sparkeras

crazy

illegitimate_egg

yeah

illegitimate_egg

qemu is the tool

illegitimate_egg

want to write an OS? QEMU

illegitimate_egg

want to run an OS? QEMU

sparkeras

i had a problem with wine where it wouldnt recognise my usb device is that normal

illegitimate_egg

what kind of USB device

sparkeras

speaking of emulators and also not emulators

sparkeras

uhh a usb recording device

illegitimate_egg

wine is not an emulator of course

illegitimate_egg

interesting

illegitimate_egg

I couldn't get my midi controller working

sparkeras

all fine on lsusb it actually worked through audacity

sparkeras

but the app i was running through wine wouldnt recognise there was anything there

illegitimate_egg

hmm

illegitimate_egg

not the best person to ask

illegitimate_egg

I like to roll my own crazy stuff

illegitimate_egg

not use other peoples

sparkeras

why does my pi only have dhcp

sparkeras

and not dhcpcd

sparkeras

which i just installed

sparkeras

observer

illegitimate_egg

I didn't build the network stack

sparkeras


illegitimate_egg

I also don't have a reference so I was guessing

sparkeras

so if i wanna set up a static ip now what is the absolute best way

illegitimate_egg

it depends

illegitimate_egg

do you have network manager

illegitimate_egg

or wpa_supplicant

sparkeras

on my laptop

illegitimate_egg

is your network configured using ifconfig or network manager

sparkeras

ifconfig on the pi i guess

illegitimate_egg

do you have them all but only a certain set of services running

illegitimate_egg

Pi OS lite comes with dhcpcd

illegitimate_egg

that was my reference

sparkeras


illegitimate_egg

the dhcp folder is from network manager

illegitimate_egg

it's its internal one

illegitimate_egg

there you go

sparkeras

ah i have this

illegitimate_egg

don't go installing stuff

illegitimate_egg

it was rhetorical

illegitimate_egg

I was complaining about how many variables there are for this simple question

illegitimate_egg

anyway

illegitimate_egg

it was network manager

illegitimate_egg

dhcpcd is a little better anyway

illegitimate_egg

so it couldn't hurt

sparkeras

yeah i got network manager up to date

sparkeras

okay set a static ip

sparkeras

what am i doin

illegitimate_egg

with network manager?

sparkeras

not following various internet gudies for a start

sparkeras

uhhh

sparkeras

whichever way is best

illegitimate_egg

if you don't want to read a guide

illegitimate_egg

then consult the man page

illegitimate_egg

it'll tell you

illegitimate_egg

man nmcli

sparkeras

i say this bc the one i showed you might not be the best way

sparkeras

writing a dhcpcd file

illegitimate_egg

I like dhcpcd

illegitimate_egg

but

illegitimate_egg

it's about convinience

oops.se

There is an alternative to using port forwarding and that is a proxy. An example is https://www.zerotier.com/ But remember this is also a security threat as the function is a "Man in the middle" (https://en.wikipedia.org/wiki/Man-in-the-middle_attack). So you need to make a risk assessment and also make a risk assessment for your neighbouring devices on the same network as they can be exposed.

sparkeras

my home ip is dynamic what’s the best way to deal with that for a vpn? wireguard seems like a good one btw unless you know of any other

oops.se

There are a lot of free Dynamic DNS (DynDNS) providers. So instead of using a IP that will change you use a DNS name, example: myfunkyip.dyndns.org. Then you have a DynDNS client running on your Pi that tells the DynDNS provider what IP you have and they link that ip to your name, ex. myfunkyip.dyndns.org

jannik44

there are several bash scripts on github that let you set your domain and then they check for ip changes every x minutes and update the dns record on cloudflare, no ddns domain needed

sparkeras

i have done this now

sparkeras

im moving forwards

sparkeras

so

sparkeras

i now have a working wireguard vpn server on the pi

sparkeras

my laptop is a client

sparkeras

with appropriate keys

sparkeras

and i can get the connection up and down

sparkeras

i can ping the pi and get a response but there’s one problem

sparkeras

well a lot of problems

sparkeras

it won’t let me do anything

sparkeras

when i try to ssh it says connection refused

sparkeras

and when i try to ftp it tells me refused

sparkeras

<@685869137939267603> any ideas sensei?

illegitimate_egg

err

illegitimate_egg

gotta be the firewall

illegitimate_egg

iptables

illegitimate_egg

sudo iptables-save

sparkeras

i don’t think i set one up

sparkeras

is there one that’s preinstalled on the pi?

illegitimate_egg

idk

illegitimate_egg

I don't know what the stock software is

illegitimate_egg

I also can't check

sparkeras

i’ll do iptables tomorrow and see what it says

sparkeras

it makes sense

sparkeras

hey look at this weird device from a weird network trying to gain remote access

illegitimate_egg

yeah

sparkeras

is there a way to get wireguard working twice

sparkeras

hang on let me explain

sparkeras

this is my webserver which i need access to so i can swap out my html files and maybe edit my server stack

sparkeras

i also want to build my nas as a cloud backup

sparkeras

obviously they will both be running separate wireguard servers but routing to the same ip (my home router) with the same client (my laptop)

sparkeras

will that work? do i just need to do what i did again and name everything wgs1 instead of wgs0?

illegitimate_egg

you don't need two wireguard servers

illegitimate_egg

that's the whole point of the VPN

sparkeras

oh yeah cause they’re on the same network

sparkeras

facepalm

sparkeras

but hang on

sparkeras

once im connected in this scenario to wgs0

sparkeras

how do i connect to the other pi not running the server

sparkeras

will it be there as if im on the home network

illegitimate_egg

yeah

illegitimate_egg

just use it's ip

illegitimate_egg

like for the other pi

sparkeras


sparkeras


sparkeras

when im connected to wg0 i cant ping my router or any other device on the network

sparkeras

but isnt it supposed to act as if im in the network

sparkeras

or do i have to be ssh'd into the pi to do anything

sparkeras

like i have to ssh to another pi via being ssh'd into my wireguard pi

thunder07337

Does wireguard run as a Docker container?

sparkeras

i don’t think so

sparkeras

but i used pi-vpn to set it up

sparkeras

so it may well do

sparkeras

i should’ve done docker ps but i have to go to work

sparkeras

either way shouldn’t i still be able to ping other devices on the home network?

thunder07337

You don't know how you installed it? If it runs in a Docker container it is isolated from the host! Docker uses its own firewall!

thunder07337

And it ignores the host firewall.

sparkeras

i installed it using pi-vpn

thunder07337

Yes, but did you install the pi-vpn yourself or was it already installed?

sparkeras

yes i installed it myself

sparkeras

and went through the options etc to set up wireguard

thunder07337

Then you should know whether you have installed it as a Docker container or not...

sparkeras

no it isnt

sparkeras

i didnt think it was but since you said it and some of the process was automated i thought i should make sue

thunder07337

Ok, I don't understand this text. Anyway... Does an SSH connection work without VPN?

sparkeras

only when im actually on my home network legitimately

thunder07337

If you are connected to the VPN, what does the command for SSH look like?

sparkeras

ssh pi@10.x.x.x

sparkeras

which is the ip address for the vpn

thunder07337

This is the public IP?

sparkeras


thunder07337

This is the public IP? Or does your internal network also have 10.159.42.x?

sparkeras

this is the internal ip for the wireguard connection

thunder07337

Forget it, you will connect to your PC from which you connected to the VPN...

thunder07337

I think the only thing that helps is this https://superuser.com/questions/347534/ssh-server-cant-be-connected-to-when-vpn-is-turned-on/347543#347543 Or you have to search the internet yourself. I think the VPN connection is blocking the SSH...

oops.se

Create a "Virtual network interface" and use that for either VPN or SSH. Example nmcli connection add con-name <connection-name> ifname <device-name> type ethernet

oops.se

Then you need to add an IP to that virtual interface. Example for DHCP nmcli connection modify <connection-name> ipv4.method auto

sparkeras

FINISHED

sparkeras

aside from any problems that might arise with DDNS here's how i did it without port forwarding

sparkeras

1. Set up your Pi with a static IP using nmtui 2. Make sure SSH is enabled 3. Register for no-ip to create a dynamic DNS for your home router (example.ddns.net) 4. Use PiVPN to install Wireguard on your server and follow the default setup making sure to include your new DDNS entry and of course your static IP 5. Configure your client through the PiVPN setup while the client and server are both connected on your home network - copy the client .conf file to your client and change the allowed ip value to the internal Wireguard IP of your server 6. In your server .conf change the allowed ip to the internal Wireguard IP of your client 7. In the same file, set MTU to 1200. This is just a precaution, you can leave it at default 8. Use wg-quick up wg(number) to set up your connection on both devices 9. Connect to any other network on your client and connect to your new VPN 10. You can now test by pinging the Wireguard IP of the server. Using SSH to connect will allow you to ping your home network devices and the internet. In your FTP client of choice choose the Wireguard IP as the hostname, rather than the static device IP. And do whatever else you want!

thunder07337

What exactly is used to establish the VPN connection, the domain or the IP address?