title: Dns.GetHostAddress(hostname) No such device or address Exception in Ubuntu 16.04
body: <p>I have a .net core 2.0 project which is trying to get Host name and Host IP address from a linux machine. The program runs well in Mac OS and Windows but not on linux - Ubuntu 16.04</p>
  <pre class="lang-cs prettyprint-override"><code>public class Program
  {
      public static void Main(string[] args)
      {
          var HostName = Dns.GetHostName();
          Console.WriteLine(&quot;Host name : &quot; + HostName);
          var HostAddress = GetHostAddress(HostName);
          Console.WriteLine(&quot;Host address : &quot; + HostAddress);
      }
  
      private static string GetHostAddress(string hostName)
      {
          try
          {
              var addressList = Dns.GetHostAddresses(hostName);
  
              foreach (IPAddress address in addressList)
              {
                  Console.WriteLine(&quot;IP Address : &quot; + address.ToString());
                  if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                  {
                      string ip = address.ToString();
                      if (!ip.StartsWith(&quot;127.&quot;))
                          return ip;
                  }
              }
  
              return &quot;127.0.0.1&quot;;
          }
          catch (Exception e)
          {
              Console.WriteLine(e);
              throw;
          }
          
      }
  }
  </code></pre>
  <p>The exception I got is</p>
  <blockquote>
  <p>ubuntu@ip-10-40-121-185:~/home/IP$ dotnet IP.dll</p>
  <p>Host name : ip-10-40-121-185</p>
  <p>System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (0x00000005): No such device or address</p>
  <p>at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)</p>
  <p>at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)</p>
  <p>at IP.Program.GetHostAddress(String hostName) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 34</p>
  <p>Unhandled Exception:
  System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address</p>
  <p>at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6)</p>
  <p>at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)</p>
  <p>at IP.Program.GetHostAddress(String hostName) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 52</p>
  <p>at IP.Program.Main(String[] args) in /Users/jliu/RiderProjects/IpTest/IP/Program.cs:line 20
  Aborted (core dumped)</p>
  </blockquote>
  <p>Any idea how to fix it or any alternative to get IP on a linux machine?
  Thanks.</p>