Essential apt-get Commands for Debian/Ubuntu
The apt-get command is the core package management tool for Debian-based systems like Ubuntu. Here are the most commonly used commands.
Package Installation & Removal
apt-get install package: Install a package.apt-get install package --reinstall: Reinstall a package.apt-get -f install: Fix broken dependencies (-fis short for--fix-missing).apt-get remove package: Remove a package but keep its configuration files.apt-get remove package --purge: Remove a package and delete its configuration files.
Package Updates & Upgrades
apt-get update: Refresh the local package index from repositories.apt-get upgrade: Upgrade all installed packages to their latest versions (does not handle dependency changes).apt-get dist-upgrade: Perform a distribution upgrade, intelligently handling dependency changes (may add or remove packages).apt-get dselect-upgrade: Upgrade packages based on selections made with thedselecttool.
Source Code & Build Environment
apt-get build-dep package: Install all dependencies required to build a specific package from source.apt-get source package: Download the source code for a package.
System Cleanup
apt-get clean: Remove all downloaded package files (.deb) from/var/cache/apt/archives/.apt-get autoclean: Remove only outdated package files from the cache.apt-get autoremove: Automatically remove packages that were installed as dependencies but are no longer needed.apt-get check: Verify the system for broken dependencies.
apt-cache Query Commands
The apt-cache command queries the APT package cache for information without installing or removing anything.
apt-cache show package: Display detailed information about a package.apt-cache search keyword: Search for packages matching a keyword.apt-cache showpkg package: Show package details, focusing on dependencies.apt-cache policy package: Show the installation status, candidate version, and repository priority for a package.apt-cache depends package: List dependencies for a package.apt-cache rdepends package: List reverse dependencies (packages that depend on the specified package).
Essential dpkg Operations
dpkg -L package: List all files installed by a package.dpkg -S filename: Find which installed package owns a specific file.dpkg --get-selections > list.txt: Backup the list of currently selected packages.dpkg --set-selections < list.txtfollowed byapt-get dselect-upgrade: Restore package selections from a backup file.
System Information & Maintenance
System Information
- Kernel version:
uname -a - Distribution info:
cat /etc/os-release - CPU info:
cat /proc/cpuinfo - Memory usage:
free -h - Disk space:
df -h - Directory size:
du -sh directory_name
Process Management
- View processes:
ps auxortop - Terminate process:
kill PIDorkillall process_name - Force kill:
kill -9 PID
Network Commands
- IP address:
ip addr show(preferred) orifconfig - Routing table:
ip route showorroute -n - Test connectivity:
ping host - Network connections:
ss -tuln(preferred) ornetstat -tuln - Transfer files via SSH:
scp -r local_file user@remote_host:/path/ # Upload scp -r user@remote_host:/path/file local_destination # Download
File & Directory Operations
- Find files:
find /path -name "filename" - Search content:
grep -r "pattern" /path - Tail log file:
tail -f /var/log/syslog(follow in real-time) - Archive & compress:
- Extract .tar.gz:
tar -xzf file.tar.gz - Extract .tar.bz2:
tar -xjf file.tar.bz2 - Create archive:
tar -czf archive.tar.gz dir1 dir2
- Extract .tar.gz:
Other Useful Commands
- Time sync:
sudo timedatectl set-ntp true(systemd) orsudo ntpdate pool.ntp.org - Command manual:
man commandorcommand --help - Search commands:
apropos keyword
Note: Most commands listed require superuser privileges. Prepend
sudoif you are a regular user. Some commands may vary based on system version or configuration; consult the manual (man) for the most current information.