Nmap (Network Mapper) is a powerful open-source tool used for network discovery and security auditing. It is widely used by network administrators and security professionals to map out networks, identify active devices, and detect open ports and services running on those devices. Nmap can also be used for vulnerability scanning and network inventory management, making it an essential tool for maintaining network security.
On Ubuntu 24.04, 22.04, or 20.04, Nmap can be installed using two primary methods. The easiest method is through the Ubuntu default repository, which provides a stable version of Nmap suitable for most users. Alternatively, for those who require the latest features or need to customize their installation, you can download and compile the Nmap source code to create the latest version of the binary. This guide will walk you through both installation methods to ensure you have the version of Nmap that best meets your needs.
Update Ubuntu System Before Nmap Installation
Let’s initiate the system update by executing the following command in the terminal. This command performs an update check for all packages and then upgrades those packages to their latest version:
sudo apt update && sudo apt upgrade
Once this is complete, proceed to install Nmap.
Method 1: Install Nmap via Ubuntu Repository
By default, Ubuntu includes Nmap in its primary software repositories, meaning you can install Nmap directly without adding any additional repositories or compiling any source code.
You can initiate the installation of Nmap by running the following command:
sudo apt install nmap
This command will fetch Nmap from the repository, resolve any dependencies, and proceed with the installation.
After installation, it’s crucial to verify that Nmap is correctly installed and ready to use. One straightforward method to confirm this is checking the Nmap version installed on your system.
Use the following command to check the version of Nmap:
nmap --version
The command nmap –version will provide output detailing the installed version and build of Nmap. This confirms that Nmap is installed correctly and ensures you work with the latest version.
Method 2: Install Nmap via Source Archive
In some cases, users might require the latest version of Nmap or the pre-packaged version provided in Ubuntu’s repositories might not meet their specific needs. Compiling Nmap from source provides a solution for such users, ensuring you have the latest features and updates. This method involves extra steps, and you would need to check for updates and re-compile regularly, but it could provide benefits for advanced users or specific requirements.
Install Nmap Initial Packages
Before delving into the source code world, we must prepare our system with the right tools. The package ‘build-essential’ contains an informational list of packages considered essential for building Ubuntu packages, including the gcc compiler, make, and other required tools. This package will allow us to compile and build Nmap from the source. To install ‘build-essential,’ open your terminal and enter:
sudo apt install build-essential libssh2-1-dev libssl-dev python3-distutils
Download Nmap Source Archive
The next step is to download the Nmap source code. This can be obtained from the official Nmap download page. We will download the latest stable release using the wget command. Please note that at the time of this guide, the latest stable release is 7.93, but this may vary. Always check the download page for the newest version number and link, and adjust the command accordingly.
To download the Nmap source code, use the following:
wget https://nmap.org/dist/nmap-7.93.tar.bz2
Note: Ensure you download the latest version of Nmap. Commands for Nmap v7.93 may be outdated, so always use commands compatible with the most recent version you’ve downloaded.
Extracting the Nmap Source Code
Having downloaded the Nmap source code, we now need to extract the files from the archive. To do this, use the following commands:
bzip2 -cd nmap-7.93.tar.bz2 | tar xvf -
cd nmap-7.93
Configure the Nmap Build
We’re now ready to start the configuration process. This prepares the Nmap source code for compilation on your specific system, ensuring compatibility and optimized performance. To configure the build, run the following:
./configure
Alternatively, you may prefer to install with localdirs:
./configure --with-localdirs
Compile Nmap Build
With the build configured, we can now compile the source code. This is done using the ‘make’ command, which reads the ‘Makefile’ in the Nmap source directory and compiles the source code accordingly:
make
Install Nmap via make install Command
We can install Nmap after we’ve successfully compiled the source code. This step places the compiled program in appropriate directories on your system. To install Nmap, use the following:
make install
Verifying the Compiled Nmap Installation
After installation, it’s crucial to confirm that Nmap has been correctly installed from the source and it’s the latest version. To check this, use the command:
nmap --version
This command will display the installed version of Nmap, which should correspond to the version of the source code you downloaded. With this, you’ve successfully compiled and installed the latest version of Nmap from source on your Ubuntu system.
Closing Thoughts
With Nmap installed on your Ubuntu system, you can effectively manage and secure your network by leveraging its powerful scanning and auditing capabilities. Whether you choose the simplicity of installing from the Ubuntu default repository or the flexibility of compiling the latest version from source, both methods provide you with a robust tool for network analysis. Regularly updating Nmap, especially when compiling from source, will ensure you have access to the latest security features and improvements, keeping your network security practices up-to-date.