Jump to content

Showing Local Loopback IP instead of network ip in login script.


Recommended Posts

I have Armbian Jessie running on a Orange Pi Lite board and the login script is showing the local loopback ip (127.0.0.1). It appears to be doing this because /etc/update-motd.d/30-sysinfo calls ifconfig -a and pulls the first ip, which in this particular case, is the local loopback. For the orangepi-lite, i wrote the following command in customize_image.sh and it fixes the problem in this case. 

sudo sed -i "s/ifconfig -a/ifconfig wlan0/g" /etc/update-motd.d/30-sysinfo

It basically changes the script to pull the ip address from wlan0 instead of whatever one it finds first This could also be called from the command line for the same effect instead of during compilation. However, for other boards using an ethernet adapter, this obviously wont work without changing wlan0 to eth0 or whatever network you have. I am not the best a bash script, and I am trying to find a way to edit the script in /etc/update-motd.d/30-sysinfo to display the first valid inet address instead of just the first inet address it finds. It is currently pulled like so. 

 

ip_address=$((ifconfig -a) | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p' | head -1)

Anyone have any ideas?

 

 
Link to comment
Share on other sites

It should probably be enough to just add "grep -v 127.0" to the above line like so:

ip_address=$((ifconfig -a) | grep -v 127.0 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p' | head -1)

 

What this means is that any line containing the string "127.0" will be discarded by the script.

Link to comment
Share on other sites

What does 'hostname -I' spits out (the 'I' is a capital 'i')?

'hostname -I' spits out the correct local ip. 

 

 

It should probably be enough to just add "grep -v 127.0" to the above line like so:

ip_address=$((ifconfig -a) | grep -v 127.0 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p' | head -1)

 

What this means is that any line containing the string "127.0" will be discarded by the script.

 

This works as well. Both of you are awesome. hostname -I will probably work in most scenarios unless there are multiple network connections then it might spit out 2 different addresses, but that would be a rare case. Not sure how that will work. I'll just add grep to my solution for now as it would still only spit out 1 ip address in the rare case of there being 2. 

Link to comment
Share on other sites

Just create a multihomed environment (defining stuff like wlan0:1) and check again. In case you get multiple lines this might help (but to be honest -- what is the real value of an IP address displayed when you log in?)

hostname -I | tr "\n" " "
Link to comment
Share on other sites

but to be honest -- what is the real value of an IP address displayed when you log in?

 

It was not really a value loss for me, agreed. But it was showing up there, and it was wrong, sooo... I had to fix it. OCD I guess. I have been putting together a pretty thorough customize-image.sh script to get it exactly the way I like. (adding wifi credential, changing hostname, adding a user, making adjustments to the terminal (the cursor was not showing up for me on hdmi), and of course installing custom packages.) I saw the ip address showed at login was wrong, so I just figured, while I was at it....

Link to comment
Share on other sites

Well according to the manual it's like that:

-i, --ip-address
       Display  the  network address(es) of the host name. Note that this works only 
       if the host name can be resolved. Avoid using this option; use hostname
       --all-ip-addresses instead.

-I, --all-ip-addresses
       Display all network addresses of the host. This option enumerates all configured 
       addresses on all network interfaces. The loopback interface and IPv6 link-local
       addresses are omitted. Contrary to option -i, this option does not depend on name
       resolution. Do not make any assumptions about the order of the output.

So -I and cutting after the 1st space would most likely fulfil every cosmetic requirements ;)

hostname -I | cut -d' ' -f1
Link to comment
Share on other sites

Be careful here, hostname -i  (lowercase) reports whatever is in /etc/hosts no mater how erroneous it might be.

I had several optional names listed in hosts for cellular (2G and 3G), wifi and wired. (Ubuntu 15.04)

 

With the capitol -I it does report only the active one(s).

My VirtualBox VMs report the pretend link to tthe host as well as the private one direct out to the net.

(Ubuntu 14.04, MintMate 17.1)

 

However my Orange Pi PC (5.10 upgraded to 5.13) reports only the active address for each conmmand option. Even with multiple entries in hosts.

 

None of the systems reported 127.0.0.1 (loopback) with either option.

Link to comment
Share on other sites

I use the IP information provided by motd to find my IP address. I is useful when I reboot from the serial connection and like to SSH to the machine. I only look at the screen and see the IP address. I fixed /etc/update-motd.d/30-sysinfo like this:

 

 
#ip_address=$((ifconfig -a) | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p' | head -1)
ip_address=$((ifconfig eth0) | awk '/inet / {gsub("addr:", "", $2); print $2}')
 
now it displays its IP address nicely!
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines