Hi Agrawal, Pranjal,
It is completely understandable to be caught off guard by this error on a fresh installation, especially since this exact sqlcmd syntax likely worked flawlessly on older versions of Windows Server and SQL Server.
To answer your first question directly: Yes, this behavior is absolutely expected. What you are experiencing is the direct result of a major architectural shift toward a "Secure by Default" posture in both the latest client drivers and the database engine. By default, ODBC Driver 18 explicitly sets Encrypt=Yes and mandates strict server certificate validation. Simultaneously, SQL Server 2025 introduces stricter TLS 1.3 and TDS 8.0 security enforcement. When you execute sqlcmd -S localhost..., the driver requests the SSL certificate from SQL Server. Because this is a fresh install, SQL Server presents its automatically generated self-signed certificate. The driver then inspects the Subject Alternative Name (SAN) of this certificate. Since the default self-signed certificate is stamped exclusively with the server's actual NetBIOS name and FQDN, but your connection string specifies localhost, the names do not match. Consequently, the driver actively terminates the connection to prevent a potential man-in-the-middle attack, resulting in the "target principal name is incorrect" error.
Regarding your subsequent questions: No, using TrustServerCertificate=Yes (like appending -C to your sqlcmd command) is not the only supported fix. It is merely a recognized workaround intended for non-production development environments.
For a Microsoft-supported, production-ready configuration that succeeds without disabling certificate validation, you must implement the following administrative steps:
Provision a Trusted Certificate: You must deploy a certificate issued by a trusted Certificate Authority (CA), such as your internal Enterprise PKI or a public CA, rather than relying on the default auto-generated self-signed certificate.
Configure the SAN Correctly: This is the most critical step for resolving the name mismatch. During the certificate request process, you must ensure that the Subject Alternative Name (SAN) extension includes every possible DNS name the client might use to connect. This must include the server's FQDN (e.g., server2025.contoso.com), the NetBIOS name (SERVER2025), and crucially, any aliases you intend to use locally, such as localhost or 127.0.0.1.
Bind the Certificate to SQL Server: Open SQL Server Configuration Manager, navigate to SQL Server Network Configuration > Protocols for SQL2025, right-click to open Properties, and select your newly minted CA certificate in the Certificate tab. Restart the SQL Server service to enforce the binding.
Once these requirements are met, ODBC Driver 18 can keep Encrypt=Yes and TrustServerCertificate=No, and your local connections will succeed securely. As an alternative, if you already have a trusted certificate bound but you simply want to connect using an alias without generating a new certificate, you can append the HostNameInCertificate parameter to your ODBC connection string to explicitly tell the driver which name to expect on the certificate.
I hope this answer provided you with a clear understanding of the new secure-by-default architecture and the proper steps to configure your certificates. If it did, please click "Accept Answer". Should you need any further clarification on generating the SAN certificate, feel free to leave a comment!
Tracy Le.