Build a private sing-box proxy on Linux: SOCKS, VLESS Reality and Hysteria2
This guide builds one Linux server that can offer three different entry points through sing-box. SOCKS suits apps that already support it, VLESS with Reality is a TCP option designed to resemble ordinary TLS traffic, and Hysteria2 uses QUIC over UDP for routes where TCP behaves poorly. The surrounding work matters as much as the protocols: key-only SSH, a default-deny firewall, brute-force bans, local recursive DNS, BBR, certificates and log checks.
What this setup is for, and what it is not
A self-run proxy inserts a server you control between a client and its destination. That can help on public Wi-Fi, in networks that block particular ports, when a direct route is unstable, or when you need a controlled endpoint for development and testing. It also teaches how Linux networking, DNS and encryption fit together.
It does not make traffic anonymous. Providers, destinations, accounts and device fingerprints can still link activity. Laws and network rules differ by place, so check the local position before using proxy tools, and use them only for lawful privacy, research, development or infrastructure work. Treat the commands as an educational pattern to review and adapt, not as a turnkey production security blueprint.
Prerequisites and a pre-flight checklist
You need a VPS with a public IP and root access, Ubuntu as the assumed distribution, a local terminal with SSH, and enough Linux familiarity to edit files and use sudo. Providers are interchangeable for this purpose; the important requirements are a reachable address and administrative control.
Decision check before starting: confirm you can log in as root, note the server IP, decide whether you will register a domain for Hysteria2 TLS, and keep a second terminal open while changing SSH settings so a mistake does not lock you out.
Harden the base system first
Update the package lists and installed packages, then remove unneeded packages and cached files. The exact commands are provided in protected module code0001. This is housekeeping rather than a proxy step, but a current base reduces later dependency surprises.
Next, create an Ed25519 key pair on your own computer, never on the server. Protected module code0002 generates it. Keep the private key local, create a non-root user with sudo rights using code0003, place the public key in that user's authorized_keys file using code0004, and correct ownership and permissions with code0005. Then edit the SSH daemon configuration through code0006 and apply the settings in code0007: password authentication off, root SSH login off, public-key authentication on, and only the named user allowed. Restart SSH with code0008.
Critical check: open a new terminal and prove the new user and key work before closing the root session. If that test fails, keep the existing session alive and fix the key or configuration.
Firewall and brute-force protection
Install ufw from code0009. On a fresh server you may reset existing rules with code0010, but on a server already running services that reset would erase current policy, so pause and inventory rules first. Set incoming traffic to deny and outgoing traffic to allow with code0011, allow SSH with rate limiting through code0012, enable the firewall with code0013, and inspect the result with code0014. At this stage only SSH should be reachable; each proxy port is opened later, one at a time.
Install fail2ban with code0015 and start it through code0016. Create the local jail file with code0017 and add the settings in code0018. In plain terms, five failed SSH logins inside ten minutes cause a one-hour ban. Restart the service with code0019, confirm the sshd jail with code0020 and code0021, and optionally test the filter against the authentication log with code0022 or inspect failed passwords with code0023.
Run Unbound locally and prevent DNS leaks
A local resolver gives the server control over lookups, caching and optional DNSSEC validation instead of depending on the hoster's resolver. This is classic recursive resolution, not DNS-over-TLS: Unbound starts from the root hints and follows the DNS chain itself. That distinction matters because forwarding to an external encrypted resolver would reintroduce a third-party resolver.
Install curl and Unbound with code0024, check the service with code0025, and download root hints with code0026. Create the resolver configuration file with code0027 and add the settings in code0028. The important choices are loopback-only listening, root-hint resolution, hardening, caching, hidden identity and version, and access limited to loopback addresses. Validate with code0029, restart with code0030, then test a lookup with code0031 and confirm port 53 listening with code0032.
Point the system resolver at the local service by editing the systemd-resolved configuration through code0033 and applying code0034, which sets the local address, clears fallback DNS, allows DNSSEC downgrade handling, and disables the stub listener and DNS-over-TLS in that component. Restart systemd-resolved with code0035, inspect status with code0036, read the effective resolv.conf with code0037, and recreate the symlink with code0038 if the file still points elsewhere.
Client-side warning: a working proxy tunnel does not guarantee DNS travels through it. Check the client app and operating-system DNS settings so lookups do not leak to the local provider. Tools such as dnscrypt-proxy, or platform options such as NextDNS or DNS Cloak on macOS, are mentioned as possible client-side controls depending on platform and threat model. A connection that leaks DNS is only partly working.
Enable BBR
Check available congestion-control algorithms with code0039. If BBR is absent, load its kernel module with code0040 and verify it with code0041. Add the queue discipline and BBR selection through code0042 and code0043, apply the settings with code0044, and confirm the active algorithm with code0045; the expected value is shown in code0046. BBR aims to keep throughput high without flooding the path, which can help long-distance or busy connections, though results depend on the route.
Install sing-box and prepare logging
Sing-box is used as one service for SOCKS, VLESS and Hysteria2 instead of separate daemons. Install it from the project's APT repository: create the keyring directory and fetch the signing key with code0047, add the repository with code0048, refresh package lists with code0049, and install sing-box with code0050.
Systemd logging alone is sufficient for many setups. If you prefer a dedicated file, create the log directory with code0051, create the file with code0052, give it to the sing-box user with code0053, and set directory and file permissions with code0054 and code0055. Consider log rotation if you keep file logging.
Prepare TLS for Hysteria2
Hysteria2 needs a normal TLS certificate, so point a domain's A record at the server before issuing one. Install certbot with code0056, temporarily allow TCP port 80 with code0057, issue the certificate with code0058, and close port 80 again with code0059.
Let the sing-box service user read certificate material without running as root: create the ssl-cert group and add the user with code0060, set group ownership and directory permissions with code0061, restrict the private key with code0062, and create pre- and post-renewal hooks with code0063 through code0066. Make the hooks executable with code0067 and test renewal with code0068; an error is expected if sing-box is not running yet. Create the certificate directory with code0069, link the full chain and key with code0070, set ownership and permissions with code0071, install ACL support with code0072, and grant current and inherited read access with code0073 and code0074. Verify with code0075.
Add SOCKS5, test it, then protect it
SOCKS5 is the simplest entry point and is supported by browsers, Telegram and many developer tools. Generate a long random password with code0076 rather than using a memorable word. Create or edit the sing-box configuration with code0077 and use the example in code0078.
Read the configuration by function: logging goes to the file prepared earlier; DNS uses the local Unbound resolver on loopback; routing sends traffic directly onward; the SOCKS inbound listens on the server IP and chosen port with username and password authentication; and the direct outbound leaves toward the destination. Replace the server IP, port and credentials with your own values.
Open the SOCKS port with code0079, validate the configuration with code0080, then start and enable the service with code0081. Restart later with code0082, confirm listening with code0083, and follow systemd logs with code0084. For Telegram, the shareable link format is in code0085; opening it in Telegram offers to add the proxy, but share it only with trusted people because it contains credentials. Watch the log file with code0086 while testing.
Because SOCKS is internet-facing, add fail2ban protection. Create the filter file with code0087 and add the pattern in code0088, test it against the log with code0089, add the jail through code0090 and code0091, restart fail2ban with code0092, and check overall and jail status with code0093 and code0094. The policy bans an address after five failed SOCKS authentications within ten minutes, for twenty-four hours, enforced through ufw.
Add VLESS with Reality
VLESS with Reality is the stealth-oriented TCP option: Reality makes the handshake resemble a normal TLS connection to a real site. Generate a client UUID with code0095, create a Reality key pair with code0096, and generate a short handshake identifier with code0097. Keep the private key on the server and put only the public key in client settings. Open the chosen TCP port with code0098.
Edit the configuration with code0099 and add the VLESS inbound alongside SOCKS using code0100. The UUID authenticates the client, the flow setting selects the modern transport behaviour, server_name and the handshake server name the camouflage domain, and the Reality block holds the private key and short identifier. That domain should have a working TLS site.
Validate with code0101, restart and check the service with code0102, confirm the port with code0103, and follow logs with code0104. For clients, v2rayN on Windows, v2rayNG on Android, and Streisand on macOS or iOS are named as common options. Import the URL format in code0105; its UUID, address, port, Reality security flag, SNI, public key, short ID and fingerprint fields must match the server. If connection fails, inspect the log with code0106 to separate reachability, authentication and handshake problems.
Protect this endpoint too. Create the Reality filter with code0107 and code0108, test it with code0109, add the jail with code0110 and code0111, restart fail2ban with code0112, and verify status with code0113 and code0114. Five invalid Reality handshakes within ten minutes cause a one-hour ufw ban. No matches on a fresh server are normal if nobody has probed the port yet.
Add Hysteria2, with optional obfuscation
Hysteria2 uses QUIC over UDP, which often feels smoother on lossy, mobile or unstable routes. Generate a strong password with code0115 and open UDP port 443 with code0116. Add the inbound using code0117.
Key choices in that configuration: the service listens on UDP 443, bandwidth hints should roughly match the server and network rather than promise speed, the password authenticates clients, TLS uses the certificate prepared earlier, and masquerade returns a harmless plain response to non-Hysteria2 probes. Validate with code0118, restart with code0119, confirm UDP listening with code0120, and follow logs with code0121. The server now exposes SOCKS, VLESS with Reality and Hysteria2.
Optional obfuscation adds a Salamander layer to make fingerprinting harder; it does not replace TLS or make traffic invisible. Generate the obfuscation password with code0122, add the obfs block shown in code0123, then validate and restart with code0124. The same password must be set on the client.
Import the client URL format from code0125 into a supporting client such as v2rayN, v2rayNG or Streisand. The password, domain, port, SNI, ALPN, obfuscation mode and obfuscation password all need to agree with the server; insecure flags are client-dependent and not mandatory. Debug with code0126 while connecting.
Fail2ban is usually unnecessary for Hysteria2. Random scanners rarely reach authentication because QUIC and TLS fail early, failed attempts rarely form a useful pattern, and aggressive bans add little benefit. Strong passwords, TLS, optional obfuscation, the firewall and the other protections provide the baseline.
Finishing touches and maintenance
A useful finishing touch is a simple static website on TCP 443. It does not conflict with Hysteria2 on UDP 443 and gives the domain a normal HTTPS presence. Trojan is also mentioned as another sing-box protocol to experiment with once the base is stable.
Keep the system patched, rotate credentials if they are ever shared, review logs periodically, and do not expose services you do not need. The main gain is understanding: when something breaks, you can tell whether the fault is in SSH, firewall, DNS, certificates, a specific inbound, or the client, instead of treating the whole stack as a black box.
| Protocol | Transport | Best fit | Domain or certificate needed | Fail2ban in this setup |
|---|---|---|---|---|
| SOCKS5 | TCP | Apps with native SOCKS support, such as Telegram or browsers | No; IP address is enough | Yes: 5 failures in 10 minutes, 24-hour ban |
| VLESS with Reality | TCP with TLS camouflage | Restrictive networks where ordinary proxy traffic may be blocked | Camouflage domain with working TLS; no certificate for your own domain | Yes: 5 invalid handshakes in 10 minutes, 1-hour ban |
| Hysteria2 | QUIC over UDP | Unstable, lossy or mobile routes where TCP suffers | Yes: domain plus TLS certificate | No; scanners usually fail before authentication |
Ports and credentials in the protected modules are examples to replace with your own values. Availability varies by network and provider.
| Check | Pass signal | If it fails, inspect first |
|---|---|---|
| SSH key login as the new user | New terminal connects without a password | authorized_keys, permissions, sshd settings |
| Firewall status | Only SSH open before proxy ports are added | ufw rules and service ports |
| Local DNS lookup | A test domain resolves through 127.0.0.1 | Unbound service, root hints, systemd-resolved settings |
| Client DNS path | Lookups follow the proxy path, not the local provider | Client app and operating-system DNS settings |
| Sing-box configuration | Validation passes before restart | JSON syntax, IPs, ports, credentials, certificate paths |
| Listening ports | Each expected TCP or UDP port is listening | Inbound settings and ufw rules |
| Client connection | Log shows connection and successful authentication | URL fields, SNI, keys, passwords, obfuscation settings |
Run checks in order; later checks are meaningless if an earlier layer is down.
Frequently asked questions
Do I really need all these protocols?
No. Start with one protocol that fits your network and app support. Add others only when a specific route, app, or filtering condition makes them useful.
Is this setup anonymous?
No. A proxy adds separation and control, but logs, fingerprints, account activity and other signals can still correlate traffic. Encryption inside a tunnel does not remove accountability.
Why not just use a VPN?
A VPN is a valid alternative. Building this stack is mainly useful when you want direct control over protocols, routing and DNS, and want to understand how those pieces interact.
Can I get blocked anyway?
Yes, blocking can still happen, especially on strict networks. Keeping more than one protocol gives you another path to test when one is filtered or performs poorly.
Do I need a domain for everything?
Only where TLS is required, notably Hysteria2. SOCKS can be reached by IP address alone, while VLESS with Reality uses a camouflage domain in its handshake rather than a certificate for your own domain.
What if I want to use this for scraping?
A single personal server can suit small experiments. Production scraping usually needs rotation, rendering, retries and browser handling; a managed scraping service is normally more practical for that workload. {{OWNER_SCRAPING_SERVICE}}
Is this safe to run long-term?
For personal use it can be reasonable if the system stays patched, credentials stay strong and unique, logs are reviewed, and only needed ports are exposed. Treat it as an educational setup, not a guaranteed production security design.
sudo mkdir -p /etc/apt/keyrings
sudo curl -fsSL https://sing-box.app/gpg.key -o /etc/apt/keyrings/sagernet.asc
sudo chmod a+r /etc/apt/keyrings/sagernet.asc
{
"log": {
"level": "info",
"output": "/var/log/sing-box/sing-box.log"
},
"dns": {
"servers": [
{
"type": "tcp",
"tag": "dns_unbound",
"server": "127.0.0.1",
"server_port": 53
}
],
"final": "dns_unbound",
"strategy": "prefer_ipv4",
"cache_capacity": 4096
},
"route": {
"final": "direct",
"default_domain_resolver": {
"server": "dns_unbound",
"strategy": "prefer_ipv4"
}
},
"inbounds": [
{
"type": "socks",
"tag": "tg-socks",
"listen": "SERVER_IP",
"listen_port": 1081,
"users": [
{
"username": "utg",
"password": "your_generated_password_here"
}
]
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct",
"domain_resolver": {
"server": "dns_unbound",
"strategy": "prefer_ipv4"
}
}
]
}
{
"log": {
"level": "info",
"output": "/var/log/sing-box/sing-box.log"
},
"dns": {
"servers": [
{
"type": "tcp",
"tag": "dns_unbound",
"server": "127.0.0.1",
"server_port": 53
}
],
"final": "dns_unbound",
"strategy": "prefer_ipv4",
"cache_capacity": 4096
},
"route": {
"final": "direct",
"default_domain_resolver": {
"server": "dns_unbound",
"strategy": "prefer_ipv4"
}
},
"inbounds": [
{
"type": "socks",
"tag": "tg-socks",
"listen": "SERVER_IP",
"listen_port": 1081,
"users": [
{
"username": "utg",
"password": "your_socks_password"
}
]
},
{
"type": "vless",
"tag": "vless-reality",
"listen": "SERVER_IP",
"listen_port": 10443,
"users": [
{
"uuid": "your_generated_uuid",
"flow": "xtls-rprx-vision"
}
],
"tls": {
"enabled": true,
"server_name": "example.com",
"min_version": "1.3",
"reality": {
"enabled": true,
"handshake": {
"server": "example.com",
"server_port": 443
},
"private_key": "your_private_key",
"short_id": [
"your_short_id"
]
}
}
}
],
"outbounds": [
{
"type": "direct",
"tag": "direct",
"domain_resolver": {
"server": "dns_unbound",
"strategy": "prefer_ipv4"
}
}
]
}
{
"log": {
// ...
},
"dns": {
// ...
},
"route": {
// ...
},
"inbounds": [
{
"type": "hysteria2",
"tag": "hy2-in",
"listen": "SERVER_IP",
"listen_port": 443,
"up_mbps": 200,
"down_mbps": 200,
"users": [
{
"name": "main",
"password": "your_hysteria2_password"
}
],
"tls": {
"enabled": true,
"server_name": "example.com",
"min_version": "1.3",
"certificate_path": "/etc/sing-box/certs/cert.pem",
"key_path": "/etc/sing-box/certs/key.pem"
},
"masquerade": {
"type": "string",
"status_code": 200,
"headers": {
"content-type": "text/plain; charset=utf-8"
},
"content": "A short example update."
}
}
],
"outbounds": [
// ...
]
}
{
"type": "hysteria2",
"users": [
{
"name": "main",
"password": "your_hysteria2_password"
}
],
// ... other config ...
"obfs": {
"type": "salamander",
"password": "your_generated_obfs_password"
}
}