Raspberry pi has static IP address - linux

Hi my raspberry pi has got static ip address.Even when its connected to router instead of 192.168.0.* as it ip address,it is showing 10.21.113.60.
I have tried changing /etc/network/interfaces file
My /etc/network/interfaces file
auto lo
iface lo inet loopback
When I looked into raspberry pi login screen i'm able to see two ip address.But ssh only works with 10.21.113.60
Someone please help me with this.

We should get lots of responses (the laptop is responding the Raspberry Pi through the network cable)! Ctrl+C to stop that. (If you didn’t then double check your connections and the IP addresses you are using).
As mentioned before, the new IP address will be lost when we reboot, so we need to ensure it is set every time we boot (or at least every time we want it to be when we boot).
To do that we can edit the “cmdline.txt” file which is located on the boot partition of the SD Card.
Make a copy of the file, with the following command:
sudo cp /boot/cmdline.txt /boot/cmdline.normal
Next edit the original file using nano:
sudo nano /boot/cmdline.txt
At the end of the long line, add the following (you will need to add a space between the last item and “ip”:
ip=192.168.0.x, for x your number
Ctrl+x and y to save and exit.
Make a copy of this file too:
sudo cp /boot/cmdline.txt /boot/cmdline.direct
You can now reboot the Raspberry Pi (sudo reboot), and next time the IP address will be automatically set.
To change between configurations, simply use the following commands (just remember to edit /boot/cmdline.direct if you need to change the IP address in future).
sudo cp /boot/cmdline.normal /boot/cmdline.txt
sudo cp /boot/cmdline.direct /boot/cmdline.txt
go to this instructions:
https://pihw.wordpress.com/guides/direct-network-connection/

Related

run program when usb connected to raspberry pi

I'm trying to run a program when I plug USB to my Raspberry Pi 3 model B.
I referenced below two
https://hackaday.com/2009/09/18/how-to-write-udev-rules/
https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
write a *.rules file in /etc/udev/rules.d/ directory
sudo nano /etc/udev/rules.d/81-usb.rules
fill with
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
restart udev
sudo /etc/init.d/udev restart
I almost tried every variation, rebooted a lot, just in case I'm missing something. But nothing works.
Thanks for reading.
Thanks stark.
Anyway I'm answering my own Question.
I changed it from
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
to
KERNEL=="sda1", RUN+="/home/pi/test.sh"
also did
chmod +x /home/pi/test.sh

Linux: execute a command when network connection is restored

I have a linux box connected to a router via an ethernet cable.
The cable is never unplugged and the linux box is always on.
My goal is to have a command executed on the linux box every time the router is rebooted.
This question is quite similar to this question but the suggested solution doesn't seem to work.
More specifically:
the command
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected' does never detect a change (even in the case the cable is unplugged from the pc), because according to this answer the command inotifywait does not work in /sys
the command (suggested in a comment) inotifywait -e modify /etc/network/if-up.d/avahi-daemon does not detect any change when the router is rebooted
You can start a script after the linux box gets connected using up
(requires ifplugd to be installed )
#/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
up /etc/network/yourscript.sh
However, there keep in mind that if you disconnect the cable (and plug in in after a while), the script also starts even though the router might not have been restarted.
--edit--
alternatively , place your script in
/etc/network/if-up.d/
(make sure it is executable and restart networking after changes.)
Depending on the Linux distribution on that router the correct way of running a command at startup/reboot is to create a startup script, or add the command to /etc/rc.local

Linux send URL my IP address on startup

So, I'm trying to write a simple bash script to send my internal IP address to a website of mine on startup. I am on a network with DHCP, so I don't always know what the IP address of my Raspberry Pi will be after I do a reboot over ssh. I figured I could fix this by sending my website the current IP on startup. I haven't written many bash scripts, and I'm not really sure how to send data to my website. Right now I was just trying in the terminal this:
wget -qO- http://http://mywebsite.com/private/CurrentIP.php?send=$(/sbin/ifconfig eth0|grep 'inet addr:')
But I'm not having any luck. I don't actually know much about linux, and I'm trying to learn. That's why I got the raspberry pi actually. Anyway, can someone head me in the right direction?
I already know I need to put it in /etc/init.d/.
You could do this:
IP_ADDR=$(ifconfig eth0 | sed -rn 's/^.*inet addr:(([0-9]+\.){3}[0-9]+).*$/\1/p')
wget -q -O /dev/null http://mywebsite.com/private/CurrentIP.php?send=${IP_ADDR}
...but if your machine is stuck behind NAT, $IP_ADDR won't be your externally-visible address. Might want to use $_SERVER['REMOTE_ADDR'] in your PHP instead of/in addition to this to get the address for your client that your server sees.
Edit: Sounds like you want to be able to find your Raspberry Pi on your local (DHCP-managed) network after reboots. Have you considered using Multicast DNS instead?
How it works in practice: Let's say you've set the hostname of your RasPi to gooseberry. If you've enabled a multicast DNS server on that machine, other computers on the same network segment that can send multicast DNS queries will be able to find it at the domain name gooseberry.local. This is a peer-to-peer protocol and not dependent on gooseberry receiving any specific address via DHCP - so if it reboots and receives a new address, other machines should still be able to find it.
Mac OS X has this enabled out of the box; this can be enabled on most Linux distros (on Debian/Ubuntu you'd install the avahi-daemon and libnss-mdns packages); not sure about Windows, but a quick Google shows encouraging results.
This worked for me (wget part untested, but it finds IP address):
interface="eth0"
ip_addr=$(ifconfig ${interface} | sed -rn 's/^.*inet *([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}).*$/\1/p')
wget -q -O /dev/null http://mywebsite.com/private/CurrentIP.php?send=${ip_addr}
Can't you use:
hostname --ip-address

Persist ifconfig lo multicast across reboots on Linux RHEL 6

Is there a way to persist the following command across reboots on Linux RHEL 6 (other than perhaps creating an init.d script):
ifconfig lo multicast
I have an application that requires multicast on 127.0.0.1, and I was wondering whether there's something in /etc/sysconfig/network-scripts that can help persist the ifconfig lo multicast across reboots.
Thanks in advance.
Thanks for the /etc/rc.local suggestion; it would work, and it's technically not an init.d script, so in my humble opinion, the -1 is a bit unfair. However, I'd like to avoid that route if possible in order to improve the maintenance of the file.
After some digging around, I couldn't find any files in the /etc/sysconfig/network-scripts/ that could help; however, I did find a potential solution; after tracing the ifup scripts, there's a hook to invoke an ifup-${DEVICE} file; since that doesn't exist for lo, I've created an ifup-lo file with 755 permissions as follows:
/etc/sysconfig/network-scripts/ifup-lo
# ifconfig is deprecated; use ip link instead...
/sbin/ip link set lo multicast on
/sbin/ip link set lo up
This will be invoked by the os when ifup lo is called, allowing any custom hooks to be added.
Any better solutions are appreciated.
The ideal solution would be for Redhat to add a MULTICAST=on or MULTICAST=off variable in the /etc/ifcfg-lo , but since that's not currently in place, ifup-lo will do for now...
append ifconfig lo multicast to the end of /etc/rc.local

Changing network ip in a bash script started from a NFS-mounted folder

I wrote a simple Bash script to change the network address of a Linux Host:
#!/bin/sh
REMOTE_HOST=192.168.2.127 # Default Host address
NEW_IP=192.168.30.33 # New IP I want to set
NEW_GW=192.168.30.1 # New Gateway I want to set
sudo ifconfig eth0 192.168.2.1 # Moving to the right network...
#ping $REMOTE_HOST -c 3 # I can correctly ping the host here...
ssh-copy-id root#${REMOTE_HOST} > /dev/null # ...for my comfort...
# Setting the network with new values for the IP addr and the GW...
COMMAND="sed -i 's#address *\\([0-9.]\\+\\)#address ${NEW_IP}#' /etc/network/interfaces\
&& sed -i 's#gateway *\\([0-9.]\\+\\)#gateway ${NEW_GW}#' /etc/network/interfaces"
ssh root#${REMOTE_HOST} $COMMAND
# done!
# Now restart the network services:
ssh root#${REMOTE_HOST} "/etc/init.d/networking restart &" & # (Note the 2nd '&' !!!)
# Come back to my old IP
sudo ifconfig eth0 192.168.30.10
sudo route add default gw 192.168.30.1
This script works almost perfectly but:
1) If I run it from my home folder, no problems; if I run it from a NFS shared folder the script hangs for a minute or two before to end correctly
2) If I omit the second '&' when restarting the network on the host the command never returns...
The questions are:
1) What causes the long wait (NFS, different IP address, different gateway)? And is it possible to workaround it?
2) Why it happens? How could I avoid it?
Thanks for any kind of help and sorry for my bad English!
You're restarting networking services, which drops all active connections.
Bash reads the file you're running line by line. Since NFS is a Network File System, this will terminate the connection to the file. So the system waits (can't actually) with executing the lines after networking restart until the connection is re-established.
Instead, you should first make a local copy of the entire script and then run it locally.
You could also code a script for that ;-)

Resources