Static IP on raspberry and Networking - networking

I'm quite new to this world and I do not have a lot of experience with raspberry PI nor with networking, and I have a doubt:
I am supposed to set a static IP to a raspberry device: ok, easy, lots of tutorials out there. The purpose is: Access a site that is on the rasp, via a network (no internet needed), like you do with your router when you configure it.
HOWEVER, this raspberry will be sent to many different places (imagine it's like a group of friends, each week it will be at one's house. They do not know how to program or set things up or even access the rasp). Therefore.. different networks.
The doubt is: Would a static IP still work in that case? Or is it restrained to one's network DHCP?
If it does not work, any ideas on how can I make it work? I'd like to access a html file hosted on the raspberry, but as its IP changes and suppose I don't know what it is, how can I access this html from my computer, both being connected on the same network?

A static IP is restricted to one network, which means you can just simply set a static IP on each network via the Router-Settings. Or configure it your self with a Bash Script.
A more dynamic way would be to just use the hostname, which leads to an access like this http://yourHostname/yourFile.html than even with a different IP everything is working. This assumes that you're webserver is right configured.
For example an Apache2-Conf with VirtualHost could look like this:
<VirtualHost *:80>
ServerName yourHostname
<VirtualHost>

I would go for a more dynamic solution and avoid using a static IP address, because you don't know which subnets are used in different places. For example a wifi might use 192.168.0.xxx, another one might use 192.168.1.xxx and so on...
Solution: try to use a hostname for your Rpi. You will then access it by using its name and not its IP.
step 1: try to find how to setup your hostname
step 2: you will have to somehow "populate" this hostname to the network. Some use the avahi-daemon for this.
step 3: you might need to install Bonjour to your laptop
More info:
about avahi here: Raspberry Pi not reachable via its hostname in LAN
about Bonjour here:
Accessing Raspberry Web server with a ".local" address?

Related

How do I assign my server IP to a local device?

I have a raspberry pi connected to my router with a local IP. I want to access the raspberry pi from outside (from another country) the local network via web browser. So, I have purchased a VPS which has its public IP.
Now how can I assign this VPS IP to the raspberry pi or any other local devices?
how can I assign this VPS IP to the raspberry pi or any other local devices?
That's not how the internet works.
In short, the VPS provider has a pool of IPs and it or its ISP is responsible for publishing routes to the rest of the internet, informing the rest of the internet how to route to those IPs.
You can't just "move" the public IP to your local network; the rest of the internet won't route traffic to it. It would be kind of like writing "1600 Pennsylvania avenue" on the front of your house - you wouldn't cause you to get White House mail, right? Because regardless of what you write there, the post office doesn't use it as a source of truth for mail routing.
There's a few of the options you have:
use ngrok to expose an endpoint to your local service. Ngrok's servers can proxy connections back to your local environment without it having to be online. This will be accessible by anyone on the internet, but is more obscure than an ipv4 address which can and will be easily found (see below). This is a quick and easy solution, and what I'd recommend in your case.
you already have the VPS. Run the code there instead of on the raspberry pi. It will almost certainly be more reliable and higher speed than your home internet.
you also already have a public IP, assuming your pi's local router has a route to the internet. the router is performing NAT so that devices "behind" it with only internal addresses can still communicate with the world. You can almost certainly configure your router to forward a certain port to your pi's internal IP address.
set up a VPN between the private networks, or between the individual hosts on the private networks. This is a lot of work and will require more knowledge of IP networking. If this interested you, I'd recommend looking at Wireguard (recommended) or OpenVPN.
Finally, keep in mind that unless you do something to make it not the case, your service, once on a public IP, will be accessible from the wide world. Since IPv4 space is pretty small by 2022 standards, and there are only 65000 some IP ports per address, it won't take nefarious parts of the internet long to find your service. You can't hide in the obscurity of an IPv4 network.
If your public address is ipv6 instead, those are essentially unguessable and much more likely to go unnoticed. Same thing is true of ngrok - since it has very large set of potential URLs, it's hard to guess. Still, I'd set up some sort of authentication in front of the service. It's just good practice, like locking your house.
Run this on vps:
echo "GatewayPorts yes" >> /etc/ssh/sshd_config
And this on your raspberry pi:
ssh -L remote-port:localhost:your-local-port your-vps.com
You will access server using: your-vps.com:remote-port

VM on GCP Lost network after set static ip in ifcfg-eth0

CentOs 7 with whm
Compute Engine VM Instance was working fine and GCP given external static ip xx.135 and internal 10.xx.x.2
Upon checking it is found that network settings was DHCP hence I
modified /etc/sysconfig/network-scripts/ifcfg-eth0 with BOOTPROTO=static with static ip given by GCP and restart network service. After that I lost the
control of VM. What is wrong? How to resolve the issues and get the control?
I do not think you needed to modify the DHCP configuration. You could follow the link here for Reserving a Static External IP Address. Also, this is the documentation if you would like to Reserve a Static Internal IP Address.
The way to fix a messed up config like this is to use the console, where the user can revert that config. Just to note here that you might have to have set the password. This is, in fact, one way.
Another way is if the disk attached is a Persistent Disk, you could attach it somewhere else and replace the config. Here is the documentation for that. There is a caution, some types of VMs that won't allow for this. It won't work if it's a local SSD.

How to change a port on a server into a different IP address in centos 7

I've got a few web servers running on my local network and, I wanted to change a specific port on a web server into IP address so that I can easily proxy them over nginx and also have access to them locally via Bind. I've got a server [HTTPD] with a few WordPress sites running on different ports [i.e 80, 8080 ETC] and I would like to change those into a private IP address locally. This is a complicated problem of the fact that I cannot specify port numbers on my local DNS, and I also don't want to install another nginx server on the local sites. Thanks guys
Search the web for "Centos 7 IP Aliasing" and set up a new IP address that connects to your machine. Then configure a new virtual host to listen on that new IP address.
I would give you more info, except A) IP aliasing on Centos 7 is more involved than I like (much easier on Solaris), and B) I'm not familiar at all with configuring Nginx (very easy on Apache).
#Tarun Lalwani asks a good question regarding whether this is a home or local network vs. a public one. You have to pay $$ for a public IP address as they are a scarce, managed resource, whereas your local network can accommodate almost as many IP's as you can think of. Anyone on your local network can access your service on your machine using those extra IP's. However, accessing those local IP's from the Public Internet is a separate topic altogether, involving router configurations and NAT addressing.
It may not be the precise answer you're looking for, but at least it should give you a direction to continue looking.

How do I create/manage the stuff on private IP address of a device connected to LAN?

Okay. I really didn't know how to explain the question in title. What I actually want to do is this:
Suppose a device is connected to WiFi, i.e. it's on LAN. It would have a private IP address like 192.168.xx.xx like that. What I want is my (android app/or technically general way) program to have access to that IP and display some info on it, like a webpage or maybe live screen of the device itself?
For ex: http://192.168.0.105:5901/thisScreen would stream live screen of a mobile device. This URL can opened by any device connected to LAN. How do I achieve that streaming access on that particular IP address assigned to the device? (Assume I've control to everything).
How do I even begin for this? Networking? How do I implement it? Any references? I tried to Google my problem but didn't find any help.
Since you ask in general, what you want is to serve sth on a lan ip ex: on 192.168.1.50:80 and that is available to everyone on the same lan to be precise in 192.168.1.0/24 right?
Well if you haven't done any configuration such as subnets smaller than /24 that is what happening by default.
To confirm that run a web server an nginx for example on a local machine and visit the ip of this machine from another device on the same lan, you will see the default page of nginx.
So the only thing you have to configure is a service to run on a local machine and serves sth.

IP Changing Program

So, my work has several networks and small business locations all with an internet blocking server. The managers all rotate and want freedom from the server, AKA static IP addresses outside the range of DHCP.
my problem is that they all need access to each others network, so setting up an alternate configuration is not that useful. How would I create an application that will change my users IP Address so that it will match their static IP at the location they changed it too?
This is just their internal IP address on their local machine, and each location's network has a static ip waiting for them. Just have to change their IP Address.
Is there a batch file command that can help achieve this? if not maybe writing a script or going into a larger application?
Doing this on the machine itself will be difficult. It needs to be able to recognise each network, and what will it do when connecting to an unknown network like a home network or a hotspot?
I think it would be better to let the DHCP server hand out static addresses from outside the normal dynamic address pool. That way everything keeps working automatically while also giving static addresses to the machines.
I know this is not a real answer to your question, but I hope this gives you an alternative.

Resources