PowerShell: Unable to get hostname from `[System.Net.DNS]::GetHostByName('')` after changing hostname with `hostnamectl hostname `.

Prerequisites

The headings have been chronologically numbered and ordered.

  1. Expected behavior

    rokejulianlockhart@localhost:~> pwsh-preview
    PowerShell 7.4.0-preview.1
    PS /home/rokejulianlockhart> [System.Net.DNS]::GetHostByName('')
    
    HostName              Aliases AddressList
    --------              ------- -----------
    localhost.localdomain {}      {127.0.0.1, 192.168.0.132, fd60:b525:84eb:0:17dc:c4a7:874b:a81a, fd60:b525:84eb:0:f22f:74ff:fe23:55ef…}
    
    PS /home/rokejulianlockhart> hostnamectl status                                                                                        
    Static hostname: (unset)                            
    Transient hostname: localhost           
        CPE OS Name: cpe:/o:opensuse:tumbleweed:20230209
                Kernel: Linux 6.1.10-1-default
    PS /home/rokejulianlockhart>
    
  2. Steps to reproduce

    rokejulianlockhart@localhost:~> hostnamectl hostname
    localhost
    rokejulianlockhart@localhost:~> hostnamectl hostname --pretty
    
    rokejulianlockhart@localhost:~> hostnamectl hostname --static
    
    rokejulianlockhart@localhost:~> hostnamectl hostname --transient
    localhost
    rokejulianlockhart@localhost:~> hostnamectl hostname beedell.rokejulianlockhart.1676161096
    rokejulianlockhart@localhost:~> hostnamectl hostname
    beedell.rokejulianlockhart.1676161096
    rokejulianlockhart@localhost:~> hostnamectl hostname --pretty
    
    rokejulianlockhart@localhost:~> hostnamectl hostname --static
    beedell.rokejulianlockhart.1676161096
    rokejulianlockhart@localhost:~> hostnamectl hostname --transient
    beedell.rokejulianlockhart.1676161096
    rokejulianlockhart@localhost:~> hostnamectl hostname --pretty
    
  3. Actual behavior

    PS /home/rokejulianlockhart> [System.Net.DNS]::GetHostByName('').HostName                                                                                 
    MethodInvocationException: Exception calling "GetHostByName" with "1" argument(s): "Name or service not known"
    
  4. Error details

    PS /home/rokejulianlockhart> Get-Error                                                                                                                                      
    
    Exception             : 
        Type           : System.Management.Automation.MethodInvocationException
        ErrorRecord    : 
            Exception             : 
                Type    : System.Management.Automation.ParentContainsErrorRecordException
                Message : Exception calling "GetHostByName" with "1" argument(s): "Name or service not known"
                HResult : -2146233087
            CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
            FullyQualifiedErrorId : SocketException
            InvocationInfo        : 
                ScriptLineNumber : 1
                OffsetInLine     : 1
                HistoryId        : -1
                Line             : [System.Net.DNS]::GetHostByName('').HostName
                PositionMessage  : At line:1 char:1
                                + [System.Net.DNS]::GetHostByName('').HostName
                                + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                CommandOrigin    : Internal
            ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
        TargetSite     : 
            Name          : ConvertToMethodInvocationException
            DeclaringType : System.Management.Automation.ExceptionHandlingOps, System.Management.Automation, Version=7.4.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
            MemberType    : Method
            Module        : System.Management.Automation.dll
        Message        : Exception calling "GetHostByName" with "1" argument(s): "Name or service not known"
        Data           : System.Collections.ListDictionaryInternal
        InnerException : 
            Type            : System.Net.Sockets.SocketException
            Message         : Name or service not known
            SocketErrorCode : HostNotFound
            ErrorCode       : -131073
            NativeErrorCode : -131073
            TargetSite      : 
                Name          : GetHostEntryOrAddressesCore
                DeclaringType : System.Net.Dns
                MemberType    : Method
                Module        : System.Net.NameResolution.dll
            Source          : System.Net.NameResolution
            HResult         : 5
            StackTrace      : 
    at System.Net.Dns.GetHostEntryOrAddressesCore(String hostName, Boolean justAddresses, AddressFamily addressFamily, Int64 startingTimestamp)
    at System.Net.Dns.GetHostByName(String hostName)
    at CallSite.Target(Closure, CallSite, Type, String)
        Source         : System.Management.Automation
        HResult        : -2146233087
        StackTrace     : 
    at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
    at CallSite.Target(Closure, CallSite, Type, String)
    at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
    at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
    CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    FullyQualifiedErrorId : SocketException
    InvocationInfo        : 
        ScriptLineNumber : 1
        OffsetInLine     : 1
        HistoryId        : -1
        Line             : [System.Net.DNS]::GetHostByName('').HostName
        PositionMessage  : At line:1 char:1
                        + [System.Net.DNS]::GetHostByName('').HostName
                        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        CommandOrigin    : Internal
    ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    
    PS /home/rokejulianlockhart>
    

Environment data

PS /home/rokejulianlockhart> $PSVersionTable 

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.1
PSEdition                      Core
GitCommitId                    7.4.0-preview.1
OS                             Linux 6.1.10-1-default #1 SMP PREEMPT_DYNAMIC Mon Feb  6 07:27:26 UTC 2023 (2a0570b)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS /home/rokejulianlockhart>

Visuals

image

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21

Most upvoted comments

That is good, in that it looks like a network configuration problem with your machine rather than a problem with either dotnet or powershell.

Original issue was reported on opensuse hence I was replicating on opensuse.

Differences only in distribution methods

Not really. Different distributions can use different versions of the kernel and compile options, different boot loaders and optjons, different system configuration, different init systems, eg classic sys-v init, systemd, or openrc, different versions of each library and compilation options, different networking configuration and defaults, for instance static network configuration or DHCP, manually created /etc/resolv.conf or one generated by DHCP services. Different packaging mechanisms, although systems may use x86_64 and ELF, there is technically no guaranteed binary compatibility between different distributions unless explicitly stated.

@rokejulianlockhart unfortunately we can’t transfer issues to the dotnet repo’s, so you’ll need to find the right one and raise it there & link it to this issue.

Original issue was reported on opensuse hence I was replicating on opensuse, admittedly mine is leap and original was tumbleweed.

CPE OS Name: cpe:/o:opensuse:tumbleweed:20230209