How to Find Which Ubuntu Package Contains a Specific Command or Tool


2 views

When working with Ubuntu, you might encounter situations where a command isn't available because its package isn't installed. For example, trying to install nslookup directly fails:

sudo apt-get install nslookup
E: Unable to locate package nslookup

This happens because many command-line tools are bundled in packages with different names than the commands themselves.

The most reliable method is using apt-file, which searches the contents of all available packages:

sudo apt-get install apt-file
sudo apt-file update
apt-file search nslookup

Sample output:

dnsutils: /usr/bin/nslookup

This tells us nslookup is in the dnsutils package.

Using dpkg -S (For Installed Packages)

If the command is already installed somewhere on your system:

dpkg -S $(which nslookup)

Using apt search

For some tools, you can try:

apt search nslookup

Though this is less reliable as it searches package names and descriptions rather than file contents.

Let's look at some common tools and their packages:

# ifconfig
apt-file search /sbin/ifconfig
# Output: net-tools: /sbin/ifconfig

# iptables
apt-file search /sbin/iptables
# Output: iptables: /sbin/iptables

# dig
apt-file search /usr/bin/dig
# Output: dnsutils: /usr/bin/dig

For frequent use, create a simple bash function:

findpkg() {
    if [ -z "$1" ]; then
        echo "Usage: findpkg <command>"
        return 1
    fi
    if ! command -v apt-file >/dev/null; then
        sudo apt-get install -y apt-file >/dev/null
        sudo apt-file update >/dev/null
    fi
    apt-file search $(which "$1" 2>/dev/null || echo "/usr/bin/$1") | head -n 1
}

Usage:

findpkg nslookup
# Output: dnsutils: /usr/bin/nslookup
  • Always run sudo apt-file update after installing apt-file to build the search database
  • The database can be large (100MB+), so consider disk space
  • For containers or minimal installations, you might need to enable additional repositories first

You're working on an Ubuntu system and need to use a specific command-line tool (like nslookup), but when you try to install it directly:

sudo apt-get install nslookup
Reading package lists... Done
Building dependency tree... Done
E: Unable to locate package nslookup

Ubuntu provides several powerful tools to discover which package contains a specific binary:

Method 1: Using apt-file

First, install apt-file if you don't have it:

sudo apt-get update
sudo apt-get install apt-file
sudo apt-file update

Then search for your command:

apt-file search bin/nslookup
dnsutils: /usr/bin/nslookup

Method 2: Using dpkg -S (For Installed Packages)

If the package is already installed:

dpkg -S $(which nslookup)
dnsutils: /usr/bin/nslookup

Method 3: Using apt search

For broader pattern matching:

apt search nslookup
Sorting... Done
Full Text Search... Done
dnsutils/focal-updates,focal-security 1:9.16.1-0ubuntu2.14 amd64
  Clients provided with BIND

Here's a handy bash function you can add to your .bashrc:

function findpkg() {
    if [ -z "$1" ]; then
        echo "Usage: findpkg [command]"
        return 1
    fi
    if command -v "$1" >/dev/null 2>&1; then
        dpkg -S $(which "$1")
    else
        apt-file search "bin/$1" || \
        apt search "$1" | head -10
    fi
}

Let's solve some common cases:

# Who provides ifconfig?
findpkg ifconfig
net-tools: /bin/ifconfig

# Where's htop coming from?
findpkg htop
htop: /usr/bin/htop

Ubuntu packages often follow these patterns:

  • Utilities: Usually in *-utils packages (dnsutils, net-tools)
  • Development tools: Often in *-dev or *-devel packages
  • Common commands: Sometimes in core packages like coreutils

For systems where you can't install additional tools, use Ubuntu's package search website:

https://packages.ubuntu.com/search?searchon=contents&keywords=nslookup&mode=filename&suite=focal&arch=any