如何使用Bash脚本生成有效的唯一本地地址(ULA)?
Local ULA Generator Scripts (RFC 4193 Compliant)
Hey there! I totally get the frustration with flaky online ULA generators—having a local, reliable script is way better. Below are two straightforward, offline scripts that follow the RFC 4193 specs to generate valid Unique Local Address prefixes (the most common use case is a /48 prefix for subnetting).
Bash Script
This uses openssl for cryptographically secure random number generation (most Unix-like systems have this pre-installed):
#!/bin/bash # Generates a RFC 4193 compliant ULA /48 prefix # Generate 5 random bytes (40 bits) for the global ID section global_id=$(openssl rand -hex 5) # Split the 10-character hex string into IPv6-friendly segments part1=${global_id:0:2} part2=${global_id:2:4} part3=${global_id:6:4} # Build the final ULA prefix ula_prefix="fd${part1}:${part2}:${part3}::/48" echo "Generated ULA Prefix: $ula_prefix"
How to use:
- Save this as
generate-ula.sh - Make it executable:
chmod +x generate-ula.sh - Run it:
./generate-ula.sh
Python Script
If you prefer Python (no external dependencies beyond the standard library):
import secrets import string def generate_ula_prefix(): # Generate 10 random lowercase hex chars (40 bits for the global ID) global_id = ''.join(secrets.choice(string.hexdigits.lower()) for _ in range(10)) # Split into segments for proper IPv6 formatting part1 = global_id[:2] part2 = global_id[2:6] part3 = global_id[6:10] # Construct the RFC-compliant ULA /48 prefix return f"fd{part1}:{part2}:{part3}::/48" if __name__ == "__main__": print(f"Generated ULA Prefix: {generate_ula_prefix()}")
How to use:
- Save this as
generate-ula.py - Run it:
python3 generate-ula.py
Key Notes
- Both scripts generate a
/48prefix, which is the recommended size for home/office networks (you can create up to 65536/64subnets from it) - The global ID is generated with cryptographically secure randomness, so the chance of collision is extremely low (far lower than any practical concern)
- If you need a full host address instead of a prefix, you can extend the result—for example:
fdxx:xxxx:xxxx:0001::1/128(replace0001with your subnet ID and::1with the host address) - These scripts run entirely offline, so you don't have to worry about online tool downtime or privacy risks
内容的提问来源于stack exchange,提问作者Benoit-Pierre DEMAINE
相关产品推荐
相关产品推荐

