Pi-hole logo with a black background, and a command line prompt.

How to Manage Pi-hole Adlists from The Command Line

  • Adam Douglas

Ever since I started using Pi Hole I’ve found it quite frustrating that I’m not able to add, remove or modify the adlist from the command line. It doesn’t make sense to me considering I can do everything else I’ve ever need to do entirely from the command line. I have a pretty good idea how I could achieve this, I just haven’t tried to test it out until now.

Pi-hole’s Database

Adding, removing, and modifying the adlist records are done using Structured Query Language (SQL) against Pi-hole’s SQLite database. In order to add an adlist we have to connect to Pi-hole’s SQLite database, so you must know where the database is stored on the file system. In my particular case Pi-hole has been installed, and configured following the knowledge base article Arch Linux ARM Install Pi-Hole On A Raspberry Pi, and therefore the database is located on the system at “/etc/pihole/gravity.db”.

Danger

The instructions outlined below are done at your own risk. Damage to the Pi-hole database may occur if not done correctly.

Add an Adlist

The command examples below will add an adlist by inserting a new record into the database, and then applying the changes.

# sqlite3 /etc/pihole/gravity.db "INSERT INTO adlist (address, enabled, comment) VALUES ('https://www.example.com/hosts/lists/hosts.txt', 1, 'Example Block List');"
$ pihole -g

Modify an Adlist

First we have to query for the adlist record “id” (12) that we want to change, run an update statement, and then apply the changes. In the example shown below, the adlist record will be disabled.

# sqlite3 /etc/pihole/gravity.db "SELECT id, address, comment FROM adlist"
...
12|https://www.example.com/hosts/lists/hosts.txt|Example Block List
# sqlite3 /etc/pihole/gravity.db "UPDATE adlist SET enabled=0 WHERE id=12"
$ pihole -g

Remove An Adlist

To delete an adlist record we must get the adlist record “id” (12), then delete the record, and apply the changes.

# sqlite3 /etc/pihole/gravity.db "SELECT id, address, comment FROM adlist"
...
12|https://www.example.com/hosts/lists/hosts.txt|Example Block List
# sqlite3 /etc/pihole/gravity.db "DELETE from adlist WHERE id=12"
$ pihole -g

This is post 82 of 100, and is round 2 of the 100 Days To Offload challenge.

    • change 100DaysToOffload message
    • correct spelling