Skip to main content

Package & Service Management

Comprehensive package and service management using bash commands for Debian/Ubuntu and CentOS with step-by-step instructions.


Package Management

1. Update Package Lists

# Update package lists
sudo apt update

# Check for available updates
apt list --upgradable

# Show package information
apt show package_name

2. Install and Remove Packages

# Install a package
sudo apt install package_name

# Install multiple packages
sudo apt install package1 package2 package3

# Remove a package (keep config files)
sudo apt remove package_name

# Remove a package and config files
sudo apt purge package_name

# Remove unused dependencies
sudo apt autoremove

# Clean package cache
sudo apt clean

3. Search for Packages

# Search for packages
apt search keyword

# Search for installed packages
apt list --installed | grep keyword

# Show package contents
dpkg -L package_name

# Check if package is installed
dpkg -l | grep package_name

Service Management

1. Check Service Status

# Check service status
sudo systemctl status service_name

# Check if service is running
sudo systemctl is-active service_name

# Check if service is enabled
sudo systemctl is-enabled service_name

# List all services
sudo systemctl list-units --type=service

# List running services
sudo systemctl list-units --type=service --state=running

2. Start, Stop, and Restart Services

# Start a service
sudo systemctl start service_name

# Stop a service
sudo systemctl stop service_name

# Restart a service
sudo systemctl restart service_name

# Reload service configuration
sudo systemctl reload service_name

# Enable service to start at boot
sudo systemctl enable service_name

# Disable service from starting at boot
sudo systemctl disable service_name

3. Service Logs and Troubleshooting

# View service logs
sudo journalctl -u service_name

# View recent logs
sudo journalctl -u service_name -f

# View logs since boot
sudo journalctl -u service_name -b

# View logs for last hour
sudo journalctl -u service_name --since "1 hour ago"

# Check service dependencies
sudo systemctl list-dependencies service_name

Automatic Updates

1. Configure Automatic Updates

# Install unattended-upgrades
sudo apt install unattended-upgrades

# Configure automatic updates
sudo dpkg-reconfigure unattended-upgrades

# Edit configuration
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

# Enable automatic updates
sudo systemctl enable unattended-upgrades
sudo systemctl start unattended-upgrades

# Check status
sudo systemctl status unattended-upgrades

2. Manual Update Procedures

# Update package lists
sudo apt update

# Upgrade all packages
sudo apt upgrade

# Upgrade distribution
sudo apt dist-upgrade

# Check for security updates
sudo apt list --upgradable | grep security

# Install security updates only
sudo apt upgrade -s

Remove Unused Packages and Services

1. Find and Remove Unused Packages

# Find unused packages
sudo apt autoremove --dry-run

# Remove unused packages
sudo apt autoremove

# Find orphaned packages
sudo deborphan

# Remove orphaned packages
sudo apt purge $(deborphan)

# Clean package cache
sudo apt clean
sudo apt autoclean

2. Disable Unused Services

# List all services
sudo systemctl list-units --type=service --all

# List enabled services
sudo systemctl list-units --type=service --state=enabled

# Disable unused service
sudo systemctl disable service_name

# Stop unused service
sudo systemctl stop service_name

# Mask service (prevent enabling)
sudo systemctl mask service_name

# Unmask service
sudo systemctl unmask service_name

Package Repository Management

1. Add and Remove Repositories

# Add repository
sudo add-apt-repository ppa:repository-name

# Remove repository
sudo add-apt-repository --remove ppa:repository-name

# Update package lists after adding repository
sudo apt update

# List enabled repositories
grep -r --include="*.list" "^deb" /etc/apt/sources.list.d/

# Backup current sources
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

2. GPG Key Management

# Import GPG key
sudo apt-key add key-file.asc

# List GPG keys
apt-key list

# Remove GPG key
sudo apt-key del key-id

# Update GPG keys
sudo apt-key update

Package Verification and Security

1. Verify Package Integrity

# Verify package signature
dpkg-sig --verify package.deb

# Check package contents
dpkg -c package.deb

# Verify installed package
dpkg -V package_name

# Check package dependencies
apt-cache depends package_name

2. Security Updates

# Check for security updates
apt list --upgradable | grep security

# Install security updates only
sudo apt upgrade -s

# Check security advisories
apt-get changelog package_name

# Update security packages
sudo unattended-upgrade --dry-run

Note: Always test package installations and service changes in a safe environment before applying them to production servers. Keep backups of important configuration files before making changes.