You may encounter a “404 Not Found” error when you want to install a package in a Debian-based host. The reason is quite obvious, the URLs for downloading the packages are not working anymore, thus 404. For example, I was getting 404 error in a fresh installation of Debian 11 when I was trying to install openssh-server
.
sudo apt install openssh-server
The error looks like the following:
Err:1 http://deb.debian.org/debian bullseye/main amd64 openssh-sftp-server amd64 1:8.4p1-5
404 Not Found [IP: 199.232.150.132 80]
Err:2 http://deb.debian.org/debian bullseye/main amd64 openssh-server amd64 1:8.4p1-5
404 Not Found [IP: 199.232.150.132 80]
E: Failed to fetch http://deb.debian.org/debian/pool/main/o/openssh/openssh-sftp-server_8.4p1-5_amd64.deb 404 Not Found [IP: 199.232.150.132 80]
E: Failed to fetch http://deb.debian.org/debian/pool/main/o/openssh/openssh-server_8.4p1-5_amd64.deb 404 Not Found [IP: 199.232.150.132 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

How to Fix?
The Debian /etc/apt/sources.list
file contains repository URLs for downloading any package. When the repository URLs are updated or redirected to other URLs, you will receive a 404 error. We need to update those URLs in our sources.list file.
Step 1: Find the outdated or broken repository URLs
From the output of your apt command, you will find the broken URLs.
Step 2: Find the updated repository URLs
For example, typing http://deb.debian.org/debian on the browser redirects to http://ftp.debian.org/debian/ so we can update deb.debian.org to ftp.debian.org in /etc/apt/sources.list. You can find a list of all Debian mirrors here.
Step 3: Backup your sources file
cp /etc/apt/sources.list /etc/apt/sources.list.bak
Step 4: Use sed to update the sources.list
file
sed -i 's/deb.debian.org/ftp.debian.org/g' /etc/apt/sources.list
The -i
option tell sed to directly make the changes in the file.
The s
stands for substitute
deb.debian.org
is the text we want to replace
ftp.debian.org
is the replacement or updated URL
g
stands for global, meaning replace all occurrences
Step 5: Update the repository
sudo apt update
Step 6: Install the desired package
sudo apt intall openssh-server
This should be enough to fix the issue

Conclusion
Finding the corrupted URLs in the source repository files (/etc/apt/sources.list) and replacing them with the proper URLs which are working should be enough to solve this error.