Blocking Ads and Trackers on elementary OS with a Hosts File
elementary OS provides a polished desktop, but a comfortable interface does not remove the background advertising and tracking common on the web. A local hosts file can stop many requests to unwanted advertising, telemetry and known malicious domains before they reach the network. It is a simple, transparent technique that suits elementary OS because the file is plain text and changes remain under the administrator’s control.
This guide explains how to enable hosts-file filtering on elementary OS, choose a dependable list, test the result and combine it with browser and account protections. The aim is to reduce noise and exposure, not to promise perfect blocking. Modern sites mix first-party and third-party content, and some applications perform their own DNS lookups, so realistic expectations matter before changing a working system.
Why a hosts file helps with elementary OS security
A hosts file is a static lookup table that maps hostnames to IP addresses. When an application asks the system to resolve a name such as ads.example.com, the resolver checks /etc/hosts before sending a DNS query. Mapping an unwanted hostname to 0.0.0.0 prevents the browser from reaching its real server, while the system continues working normally for unrelated names.
The security value comes from removing entire categories of unwanted traffic. A curated list can block advertising platforms, analytics endpoints and some services associated with malicious software. This reduces the number of third parties able to record visits, build profiles around browsing habits or deliver files through compromised domains. It does not block every advertisement, malware download or fraudulent page, but it adds a useful layer without requiring a separate firewall application.
Daily Australian habits make machine-level filtering useful. A commuter checking news on a phone hotspot in Brisbane, a student joining café Wi-Fi in Melbourne or an office worker opening a link at an airport in Sydney may encounter different networks but the same device-level rules. Ad-supported Australian news, retail and streaming services also make domain blocking relevant for households that would rather not share browsing activity with numerous advertising networks.
Preparing and editing /etc/hosts safely
Begin by creating a backup and checking the file’s ownership. The following command preserves the first copy without replacing an existing backup:
sudo cp --no-clobber /etc/hosts /etc/hosts.original
ls -l /etc/hosts
The file should normally be owned by root, with read access for other users and write access restricted to the administrator. Open it with a terminal editor:
sudo nano /etc/hosts
Leave the existing localhost and local hostname entries intact. Add blocked domains near the bottom, with one hostname on each line and an optional comment above them:
# Local ad and tracker blocking
0.0.0.0 ads.example.com
0.0.0.0 tracker.example.net
Do not include https://, a path or a port number. Hosts files do not support wildcard domains, so *.example.com will not work. Some lists use 127.0.0.1 instead of 0.0.0.0; either can redirect traffic locally, and using one format consistently makes the file easier to audit.
In nano, press Ctrl+O, then Enter, to save the file and Ctrl+X to exit. If the desktop appears unchanged, close and reopen the browser. The change is immediate for new system lookups, although cached DNS information or an already loaded page can delay the visible result. Restore the original file with sudo cp /etc/hosts.original /etc/hosts if an essential service stops working.
Choosing a trustworthy blocklist
A useful blocklist should have a clear source, regular updates and a stated purpose. Steven Black’s unified hosts file is a well-known combined starting point, while Peter Lowe’s advertising and tracking list focuses more narrowly on unwanted network endpoints. Large commercial bundles are harder to inspect and may contain entries that cause unexpected failures. Download only the plain-text list from the project’s official source, and avoid scripts advertised as automatic “privacy optimisers.”
Entries should be reviewed rather than pasted blindly. Preserve the local host declarations, remove obviously broken entries and exclude services required for work or account access. Blocking a software repository, single sign-on provider or security service can prevent updates and authentication. Lists also age: a domain retired by an advertising company may later be reused for something legitimate, while newly introduced trackers will not appear until the next update.
An advertising-heavy page can provide a useful before-and-after comparison. A page such as an offshore betting page may load external advertising and analytics hostnames, although access may be restricted in Australia and the link is not an endorsement of the operator. Internet casinos are prohibited in Australia, while authorised betting services operate under federal, state and territory arrangements. The Interactive Gambling Act 2001 provides part of that framework, and ACMA can arrange blocking of illegal offshore gambling services. The legal status of the example does not make it a trusted source; its only value here is illustrating the network requests associated with a complex page.
Testing and troubleshooting blocked requests
Test a domain after saving the hosts file by replacing it with one of your actual entries. getent uses the system’s normal name-resolution path:
getent hosts ads.example.com
resolvectl query ads.example.com
The address returned should be 0.0.0.0 rather than a public IP address. If a real address appears, check the spelling and make sure the line contains only a hostname. If the system honours the entry but the browser still displays the page, close all browser windows and clear its cached data. The resolver cache can also be cleared with:
sudo resolvectl flush-caches
Restarting systemd-resolved is rarely necessary because hosts entries are checked for each new lookup, but sudo systemctl restart systemd-resolved can help when stale cached results persist. Encrypted DNS is not an automatic escape hatch: ordinary desktop lookups normally consult /etc/hosts before sending a DNS query. Applications with their own isolated resolver can behave differently, and content hosted on the same domain as the page cannot be removed with a hostname entry alone.
Keep legitimate local names available. Blocking the router, network printer or a captive portal can make an entire service appear broken. The site’s printer and queue guide explains how elementary OS discovers network printers and manages queues, which is useful when checking the actual hostname of a local device. Precise entries are safer than redirecting broad groups of private addresses.
Layering hosts blocking with other protections
Browser protections can complement the hosts file because they can react to page content and individual requests. Firefox’s Enhanced Tracking Protection offers a straightforward built-in option, while a reputable extension such as uBlock Origin provides more detailed controls. Extensions should come from the browser’s official directory and should be reviewed for permissions. If one breaks a bank, workplace portal or video service, pause it on that site rather than abandoning the broader setup.
A hosts file protects only the computer on which it is edited. DNS filtering at an NBN router can cover phones, smart televisions and other household devices that never run elementary OS. Australian providers such as Telstra, Optus and Aussie Broadband supply routers with varying levels of control, so the available features depend on the model. Router-level filtering may also be disabled by encrypted DNS settings on individual devices, making a local hosts file and browser controls useful complements rather than competing approaches.
The hosts file is not an antivirus product, a firewall or a substitute for secure authentication. Direct-IP connections, first-party advertising and harmful content on an otherwise legitimate domain can still reach the computer. If the Uncomplicated Firewall is installed, sudo ufw status can show its state, but enabling a firewall while connected remotely can interrupt access and should be handled carefully. The Privacy Act 1988 and Australian Privacy Principles regulate how many organisations collect and handle personal information, while the Spam Act 2003 governs commercial electronic messages. Local filtering can reduce some tracking requests, but it does not make an organisation compliant or a device anonymous.
What to remember
A hosts file is most effective when it is treated as an inspectable, local privacy layer. Accurate entries, regular list updates and carefully chosen exclusions preserve normal system use while reducing domain-based advertising and unnecessary tracking. Its transparency also makes it suitable for users who want to understand exactly which names their elementary OS computer is refusing to contact.
The point to remember is precise: a maintained hosts file can silence many advertising and tracking requests, but its value depends on accurate entries, sensible exceptions and the wider browser, account and update protections surrounding it.