How to resolve "dnf: command not found..." problem on fedora 39

I was trying to remove python environment and I think I accidentally removed the one comes default with fedora and makes dnf command not available.

I did(not consecutively but I think those three commands are the relevant ones):

sudo rm /bin/python
sudo rm /usr/bin/python
sudo rm /bin/python3 /bin/python3.12 /bin/python-argcomplete-check-easy-install-script /bin/python-argcomplete-tcsh

Then I found that:

n349@fedora:~/Downloads$ sudo dnf upgrade
Place your right index finger on the fingerprint reader
^C[sudo] password for n349: 
sudo: dnf: command not found

I tried to reinstall dnf, but it seems that dnf-4.19.0-1 is currently still installed on my machine and i cannot do it again.

~/Downloads$ dnf --version
bash: dnf: command not found...
Install package 'dnf' to provide command 'dnf'? [N/y] y


 * Waiting in queue... 
 * Loading list of packages.... Failed to install packages: dnf-4.19.0-1.fc39.noarch is already installed
n349@fedora:~/Downloads$ ls -l /usr/bin/dnf
lrwxrwxrwx. 1 root root 19 Mar 11 01:37 /usr/bin/dnf -> /usr/bin/dnf-4.19.0

I am using fedora 39 upgraded using dnf from fedora 38 preinstalled on a thinkpad x1 carbon.

n349@fedora:~/Downloads$ uname -a
Linux fedora 6.7.7-200.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Mar  1 16:53:59 UTC 2024 x86_64 GNU/Linux

I searched online and didn’t found a solution to this problem and hope someone could help me with it.

Added python, workstation

The reason I did this stupid thing earlier today was because I found python3 in both /bin and /usr/bin, so I decided to remove one of them. I thought in the worst senario I just had to reinstall python, but it seems to get complicated.

I tried to reinstall the dependencies:

$ sudo rpm -ivh --force dnf-data-4.19.0-1.fc39.noarch.rpm \
                      dnf-automatic-4.19.0-1.fc39.noarch.rpm \
                      python3-dnf-4.19.0-1.fc39.noarch.rpm \
                      yum-4.19.0-1.fc39.noarch.rpm

sudo rpm -ivh --force dnf-4.19.0-1.fc39.noarch.rpm
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:dnf-data-4.19.0-1.fc39           ################################# [ 25%]
   2:python3-dnf-4.19.0-1.fc39        ################################# [ 50%]
   3:dnf-automatic-4.19.0-1.fc39      ################################# [ 75%]
   4:yum-4.19.0-1.fc39                ################################# [100%]
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:dnf-4.19.0-1.fc39                ################################# [100%]

But it still does not work and the error messages change:

n349@fedora:~/Downloads$ dnf --version
bash: /usr/bin/dnf: cannot execute: required file not found

Try confirming the ‘dnf’ bin file actually did install and all is correct with it on the filesystem with:

rpm -vV dnf

in the listing the /usr/bin/dnf file line should shows eight dots:

.........    /usr/bin/dnf

“file /usr/bin/dnf*” should show:

/usr/bin/dnf:               symbolic link to dnf-3
/usr/bin/dnf-3:             Python script, ASCII text executable
/usr/bin/dnf4:              symbolic link to dnf-3
1 Like

I found the solution here

This line fix the problem:

rpm --reinstall --verbose --hash` [`https://kojipkgs.fedoraproject.org/packages/python3.12/3.12.2/2.fc39/x86_64/python3-3.12.2-2.fc39.x86_64.rpm`](https://kojipkgs.fedoraproject.org/packages/python3.12/3.12.2/2.fc39/x86_64/python3-3.12.2-2.fc39.x86_64.rpm)
$ sudo rpm --reinstall --verbose --hash https://kojipkgs.fedoraproject.org/packages/python3.12/3.12.2/2.fc39/x86_64/python3-3.12.2-2.fc39.x86_64.rpm
Retrieving https://kojipkgs.fedoraproject.org/packages/python3.12/3.12.2/2.fc39/x86_64/python3-3.12.2-2.fc39.x86_64.rpm
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:python3-3.12.2-2.fc39 ################################# [ 50%]
Cleaning up / removing...
2:python3-3.12.2-2.fc39 ################################# [100%]
n349@fedora:~/Downloads$ rpm --query python3
python3-3.12.2-2.fc39.x86_64
n349@fedora:~/Downloads$ dnf --version
4.19.0
Installed: dnf-0:4.19.0-1.fc39.noarch at Mon 11 Mar 2024 07:38:12 AM GMT
Built : Fedora Project at Tue 20 Feb 2024 02:19:51 PM GMT
Installed: rpm-0:4.19.1.1-1.fc39.x86_64 at Wed 21 Feb 2024 07:00:46 PM GMT
Built : Fedora Project at Wed 07 Feb 2024 04:05:57 PM GMT
1 Like

That is because /bin is a symlink to /usr/bin.

As you found python is critical to the operation of Fedora and many tools are implemented in python, like dnf.

Obviously you were not aware that /bin is a link to /usr/bin so everything seen in one path is actually a copied listing of what is in the other location.

$ ls -ld /*
dr-xr-xr-x.   2 root root  4096 Jul 20  2023 /afs
lrwxrwxrwx.   1 root root     7 Jul 20  2023 /bin -> usr/bin

In general, anytime a user does a removal of individual files that are in system locations they are searching for a way to break their system.

1 Like