What does the SSH Config to /etc/hosts Converter do?
The SSH Config to /etc/hosts Converter parses OpenSSH client configuration files (~/.ssh/config) and extracts Host aliases and HostName destinations (IP addresses or domains). It generates structured, fixed-width mapped lines ready to append directly into /etc/hosts (Linux/macOS) or C:\Windows\System32\drivers\etc\hosts (Windows).
Core Concepts
Understanding SSH config to /etc/hosts mapping:
- Host & HostName Pairing: Maps each target IP address from
HostNameto all declaredHostalias nicknames on the same line. - Multiple Alias Support: Automatically extracts multi-alias declarations (e.g.
Host db db.internal db.local) into a single consolidated line. - Column Padding: Formats output with 20-character column spacing for clean visual alignment in system network hosts files.
How to use the tool?
- Paste SSH Config: Paste your
~/.ssh/configfile content into the editor or click Load Sample Config. - Execute Conversion: Click Convert to /etc/hosts Format to extract host mapping entries.
- Copy & Apply: Click Copy or Download, then append the output to
/etc/hostsor your system hosts file.
Related Developer Utilities
If you work with SSH configurations, networking, and server provisioning, explore these complementary tools:
- SSH Config to Ansible Inventory Converter: Convert
~/.ssh/configto Ansible INI/YAML inventories. - SSH Public Key Fingerprint Generator: Calculate SHA256 & MD5 SSH key fingerprints.
- SSH Public Key Format Converter: Convert keys between OpenSSH, RFC 4716, and PEM formats.
- YAML to JSON Converter: Convert structured YAML documents into clean JSON.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/ssh/ssh-config-to-hosts) to programmatically convert OpenSSH client configuration (~/.ssh/config) files into /etc/hosts mapping definitions.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText |
String | The ~/.ssh/config file content string to parse. |
"Host prod-db\n HostName 10.0.1.50" |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/ssh/ssh-config-to-hosts \
-H "Content-Type: application/json" \
-d '{
"rawText": "Host prod-db\n HostName 10.0.1.50\n User ubuntu\n\nHost web-staging\n HostName 192.168.1.100"
}'Python
import requests
url = "https://blueutils.com/api/ssh/ssh-config-to-hosts"
payload = {
"rawText": "Host prod-db\n HostName 10.0.1.50\n User ubuntu\n\nHost web-staging\n HostName 192.168.1.100"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": "Host prod-db\\n HostName 10.0.1.50\\n User ubuntu\\n\\nHost web-staging\\n HostName 192.168.1.100"
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/ssh/ssh-config-to-hosts"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether conversion succeeded. | true |
convertedCount |
Number | Count of valid mapped host entries. | 2 |
converted |
String | Formatted /etc/hosts file content string. |
"10.0.1.50 prod-db\n..." |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"convertedCount": 2,
"converted": "# Generated from ~/.ssh/config via Blueutils\n# Format: <IP_OR_TARGET> <HOST_ALIAS>\n10.0.1.50 prod-db\n192.168.1.100 web-staging"
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "No valid Host blocks with a \"HostName\" directive were found in the SSH config."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to convert SSH config to /etc/hosts?
Integrating the SSH Config to /etc/hosts API into DevOps provisioning scripts, devcontainers, or AI agent tool calling provides key benefits:
- Rapid Script Validation: Generates local DNS aliases for ephemeral VM clusters and dev containers automatically.
- Optimized Token Efficiency for AI Agents: LLMs frequently mix up multi-alias column alignment. Calling the API extracts IP-hostname pairs deterministically without token consumption.
- Deterministic Accuracy Without Hallucinations: Ensures 100% accurate column alignment and syntax formatting for system network configuration.
Native Usage
How to convert SSH configs to /etc/hosts locally in terminal environments or scripts:
Windows (CMD / PowerShell)
# Extract Host and HostName from ~/.ssh/config in PowerShell
$configPath = "$env:USERPROFILE\.ssh\config"
if (Test-Path $configPath) {
Get-Content $configPath | ForEach-Object {
if ($_ -match '^\s*Host\s+([^\*]+)$') { $h = $matches[1].Trim() }
elseif ($_ -match '^\s*HostName\s+(\S+)' -and $h) {
Write-Output "$($matches[1].Trim().PadRight(20)) $h"
$h = ""
}
}
}Linux / Unix (Bash)
# Parse ~/.ssh/config using awk in Linux
awk '
tolower($1) == "host" && $2 != "*" { host = $2 }
tolower($1) == "hostname" && host != "" {
printf "%-20s %s\n", $2, host
host = ""
}
' ~/.ssh/configPython
Using Python:
import re
with open("~/.ssh/config".replace("~", "/home/user")) as f:
content = f.read()
for match in re.finditer(r'Host\s+([^\n]+)\n\s+HostName\s+([^\n]+)', content):
aliases = match.group(1).strip()
ip = match.group(2).strip()
print(f"{ip:<20} {aliases}")Java
Using Java:
import java.nio.file.*;
import java.util.List;
public class SshToHostsExample {
public static void main(String[] args) throws Exception {
List<String> lines = Files.readAllLines(Paths.get(System.getProperty("user.home"), ".ssh", "config"));
String host = "";
for (String line : lines) {
String trimmed = line.trim();
if (trimmed.startsWith("Host ") && !trimmed.contains("*")) {
host = trimmed.substring(5).trim();
} else if (trimmed.startsWith("HostName ") && !host.isEmpty()) {
String ip = trimmed.substring(9).trim();
System.out.printf("%-20s %s%n", ip, host);
host = "";
}
}
}
}