Visit Our UNIX and Linux User Community


How to get the correct IP on a Linux server?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the correct IP on a Linux server?
# 1  
Old 08-24-2013
How to get the correct IP on a Linux server?

Hi

I need to get IP address from linux server.
There are many script to do this, but no one is perfect.
They just grab eth0 or eth1 from ifconfig, and this may, may not be correct.

You may have several IF configured, but only one is default GW

Here is how I would like the process to be.

1. What is the default gateway?
This can be found using route -n and look at flags UG

2. Look at ifconfig and select the IP that correspond to default gateway within subnetmask.

This would give correct IP and ifname if you need it.


But there is but.
One one virtual server it may looks like this
Code:
route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         0.0.0.0         0.0.0.0         U     0      0        0 venet0

Code:
ifconfig
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:12840125 errors:0 dropped:0 overruns:0 frame:0
          TX packets:12840125 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:4483434938 (1.4 GB)  TX bytes:4483434938 (1.4 GB)

venet0    Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:127.0.0.2  P-t-P:127.0.0.2  Bcast:0.0.0.0  Mask:255.255.255.255
          inet6 addr: 2b00:daa0:da:99:225:71:fd32:d097/128 Scope:Global
          UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1
          RX packets:41235259 errors:0 dropped:0 overruns:0 frame:0
          TX packets:39244451 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2827188617 (1.8 GB)  TX bytes:3421181418 (1.4 GB)

venet0:0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
          inet addr:182.23.5.11  P-t-P:37.247.52.130  Bcast:0.0.0.0  Mask:255.255.255.255
          UP BROADCAST POINTOPOINT RUNNING NOARP  MTU:1500  Metric:1

No default gateway, and SM 255.255.255.255
This is typical on VPN tunnels etc.

So how do I find DG, IP and correct ifname??
# 2  
Old 08-24-2013
Code:
ip route
default via 10.1.1.254 dev eth0  proto static 
10.1.1.0/24 dev eth0  proto kernel  scope link  src 10.1.1.1  metric 1

?
# 3  
Old 08-24-2013
Code:
ip route
default dev venet0  scope link

Does help, but I still have two if name with venet0, venet0 and venet0:0


EDIT: This shows only one if and can be used with ip route
Code:
ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/void
    inet 127.0.0.2/32 scope host venet0
    inet 182.23.5.11/32 scope global venet0:0
    inet6 2b00:daa0:da:99:225:71:fd32:d097/128 scope global
       valid_lft forever preferred_lft forever

# 4  
Old 08-24-2013
Looking at your post#1, venet0 is the host itself. The :0 I know as alias interfaces, see man ifconfig. That setup is a bit strange to me.
# 5  
Old 08-24-2013
Its because its a VPS and I have no control of it.
But I seems that global only exist with line having active IP.

This seems to work on both physical and VPS server.
Code:
ip addr show | awk -F"[ /]+" '/inet / && /global/ {print $3}'

Can other test this and see if it give only one correct IP for the active IF
# 6  
Old 08-24-2013
try:
Code:
$ ip add sh | grep -w inet

This will grep all ip on all interfaces
# 7  
Old 08-25-2013
And that is what i do not need.
If you read post above, I need only the IP that is used for traffic.
In same range as Default Gateway.

Previous Thread | Next Thread
Test Your Knowledge in Computers #293
Difficulty: Easy
Linux is the most popular operating system among supercomputers today (2019).
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux correct permissions for .bash_aliases

Above is a search string I tried on Google that produced lots of hits but not much in the way of clues. And I wasn't looking for clues. So I decided, for the first time, to ask the question here. What are the correct permissions for the .bash_aliases file? iwrong (1 Reply)
Discussion started by: iamwrong
1 Replies

2. Windows & DOS: Issues & Discussions

Passwordless login from Linux server to windows server

Hi, I am a newbie and has been given a task to connect to a windows server from a linux server (using putty of course; for file transfer) that too passwordless. Searching through google I found "ssh-keygen" but this also couldn't work because as I tried ssh <username>@<windowsServerIP>, it says... (5 Replies)
Discussion started by: pratria
5 Replies

3. Linux

How to connect Linux server (configure two way authentication) with Windows server?

Hi my name is Manju. ->I have configure the two way authentication on my linux server. ->Now I am able to apply two way authenticator on particuler user. ->Now I want to map this linux server to my AD server. ->Kindly tell me how to map AD(Active Directory) with this linux server. ... (0 Replies)
Discussion started by: manjusharma128
0 Replies

4. Homework & Coursework Questions

Configure the AD Window server with Linux server(google Authenticator is installed)

Hi my name is Manju. ->I have configure the two way authentication on my linux server. ->Now I am able to apply two way authenticator on particuler user. ->Now I want to map this linux server to my AD server. ->Kindly tell me how to map AD(Active Directory) with this linux server. ... (2 Replies)
Discussion started by: manjusharma128
2 Replies

5. Shell Programming and Scripting

Copy files from Linux server to Windows server

Hi All, I am generating report in a Linux server and once the report is generated the report(.txt file) needs to be automatically saved in a Windows servers. So i am looking for a script to transfer the file automatically from Linux server to Windows server? Please advise. Thanks... (3 Replies)
Discussion started by: arunmanas
3 Replies

6. Shell Programming and Scripting

Connect (SSH) to Windows server via Linux server through a script and passing command.. but failing

I am trying to connect to Windows server via Linux server through a script and run two commands " cd and ls " But its giving me error saying " could not start the program" followed by the command name i specify e g : "cd" i am trying in this manner " ssh username@servername "cd... (5 Replies)
Discussion started by: sunil seelam
5 Replies

7. Shell Programming and Scripting

Copy folder and files from unix server to linux server

We would be migrating unix solaries to Linux redhat. Basically source is unix and target is linux. i would like to copy entire file system unix/source/* to target linux/souce/* but target linux has only folder setup so what ever files copied need to be placed in the linux server with same... (8 Replies)
Discussion started by: balajikalai
8 Replies

8. Red Hat

microsoft Server 2008 Active authentication to a linux server

Hi, Please could someone advise I'm trying to use winscp from a Window server 2008 R2, but i need to add the authentication key to access the linux rh 5.4 servers ? What is the best way of approaching this ? If there are any web links that could help me do this, that would be good. ... (1 Reply)
Discussion started by: venhart
1 Replies

9. UNIX for Dummies Questions & Answers

Correct time in server

Hi, we have a server which when we check time it has different time for different user. When we're using root user, the time is correct but when we login as individual user the time is delayed 1 hour. How can we correct the time when we're using individual user like my own user? Please help me to... (4 Replies)
Discussion started by: ayhanne
4 Replies

Featured Tech Videos