Blog / Linux/ Essential apt-get and System Commands for Debian and Ubuntu

Essential apt-get and System Commands for Debian and Ubuntu

Debian常用apt-get命令

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 (-f is 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 the dselect tool.

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.txt followed by apt-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 aux or top
  • Terminate process: kill PID or killall process_name
  • Force kill: kill -9 PID

Network Commands

  • IP address: ip addr show (preferred) or ifconfig
  • Routing table: ip route show or route -n
  • Test connectivity: ping host
  • Network connections: ss -tuln (preferred) or netstat -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

Other Useful Commands

  • Time sync: sudo timedatectl set-ntp true (systemd) or sudo ntpdate pool.ntp.org
  • Command manual: man command or command --help
  • Search commands: apropos keyword

Note: Most commands listed require superuser privileges. Prepend sudo if you are a regular user. Some commands may vary based on system version or configuration; consult the manual (man) for the most current information.

Post a Comment

Your email will not be published. Required fields are marked with *.