linuxcentoscontainerdnerdctlalmalinux

Command cannot be recognized


I wanted to ask a likely very simple question regarding Linux (AlmaLinux-9.2)...

I unpacked the file and I wanted to execute the binary file nerdctl. However, I could not do it if I did not add ./ at the beginning of the command?

enter image description here

It is because I need to move or link up this file to another folder /usr/local/bin, so it can be globally used.
But, how can I execute the file without adding ./?


Solution

  • In Linux, the reason you need to use ./ before executing a binary in the current directory is related to the PATH environment variable. The PATH variable tells the shell which directories to search for executable files.

    The ./ before a command indicates to the shell to look in the current directory for the executable. By default, the current directory (.) is usually not part of the PATH for security reasons. If it were, malicious binaries could be disguised with common command names in directories, and you could inadvertently run them.

    If you want to run the nerdctl command from any location without prefixing with ./, you have a few options:

    1. Move or Symlink to a Directory in $PATH: Most system-wide binaries are stored in directories that are included in the PATH variable, such as /usr/local/bin, /usr/bin, etc. If you move or symlink your binary to one of these directories, you can call it from anywhere without the ./ prefix.

      sudo mv nerdctl /usr/local/bin/
      

      OR

      sudo ln -s /path/to/your/nerdctl /usr/local/bin/nerdctl
      
    2. Modify the PATH Variable: This is less recommended because it can introduce the security issues mentioned above, but you could add the directory containing nerdctl to the PATH variable.

      Add this to your ~/.bashrc or ~/.zshrc (depending on your shell):

      export PATH=$PATH:/path/to/the/directory