Linux command not found что

Command not found when using sudo

I have a script called foo.sh in my home folder. When I navigate to this folder, and enter ./foo.sh , I get -bash: ./foo.sh: Permission denied . When I use sudo ./foo.sh , I get sudo: foo.sh: command not found . Why does this happen and how I can fix it?

11 Answers 11

Permission denied

In order to run a script the file must have an executable permission bit set.

In order to fully understand Linux file permissions you can study the documentation for the chmod command. chmod, an abbreviation of change mode, is the command that is used to change the permission settings of a file.

To read the chmod documentation for your local system , run man chmod or info chmod from the command line. Once read and understood you should be able to understand the output of running .

. which will list the READ, WRITE and EXECUTE permissions for the file owner, the group owner and everyone else who is not the file owner or a member of the group to which the file belongs (that last permission group is sometimes referred to as «world» or «other»)

Here’s a summary of how to troubleshoot the Permission Denied error in your case.

$ ls -l foo.sh # Check file permissions of foo -rw-r--r-- 1 rkielty users 0 2012-10-21 14:47 foo.sh ^^^ ^^^ | ^^^ ^^^^^^^ ^^^^^ | | | | | Owner| World | | | | Name of Group | Group Name of Owner 

Owner has read and write access rw but the — indicates that the executable permission is missing

The chmod command fixes that. (Group and other only have read permission set on the file, they cannot write to it or execute it)

$ chmod +x foo.sh # The owner can set the executable permission on foo.sh $ ls -l foo.sh # Now we see an x after the rw -rwxr-xr-x 1 rkielty users 0 2012-10-21 14:47 foo.sh ^ ^ ^ 

foo.sh is now executable as far as Linux is concerned.

Using sudo results in Command not found

When you run a command using sudo you are effectively running it as the superuser or root.

Читайте также:  Установка сертификата rutoken linux

The reason that the root user is not finding your command is likely that the PATH environment variable for root does not include the directory where foo.sh is located. Hence the command is not found.

The PATH environment variable contains a list of directories which are searched for commands. Each user sets their own PATH variable according to their needs. To see what it is set to run

Here’s some sample output of running the above env command first as an ordinary user and then as the root user using sudo

rkielty@rkielty-laptop:~$ env | grep ^PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games rkielty@rkielty-laptop:~$ sudo env | grep ^PATH [sudo] password for rkielty: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin 

Note that, although similar, in this case the directories contained in the PATH the non-privileged user (rkielty) and the super user are not the same.

The directory where foo.sh resides is not present in the PATH variable of the root user, hence the command not found error.

Источник

Fixing “Command Not Found” Error When Using Sudo

It is a right of passage for almost all Linux users to go through the headaches associated with the error “command not found” whenever you try to execute a script or a file on your Linux terminal as a sudoer user.

This error directly implies that the script or file you are trying to execute either does not exist on your system or the PATH variable is unaware of its unspecified location.

This article guide will walk us through diagnosing and fixing this “command not found” sudo-associated error.

Linux Environment Variables

For users with some software programming experience, a variable is nothing but a changeable placeholder of some value. For instance, when you use the term “my pc” to describe the computer you are using, the term “my pc” is being used as a generic placeholder/variable since it can either be a Lenovo, Hp, Mac, or Raspberry Pi at any given time.

The Linux environment variables are unique variables associated with a system user login session info. The configuration of these variables; in most cases, is by default and occurs during user creation and/or package installation.

They are helpful because scripts, applications, and the system shell need them to effectively execute running commands. These variables can either be global (system-defined) or local (user-defined).

The Linux $PATH Variable

When executing a command on a Linux operating system terminal environment, the Linux system tends to search through a colon-separated directories list specified by the $PATH variable.

Читайте также:  Чем плоха операционная система linux

For instance, on a freshly installed Linux system, the $PATH variable is assigned the default value /usr/bin:/usr/local/bin . This instance tells the Linux system to query the two binary file locations ( /usr/bin and /usr/local/bin ) before executing any command.

List Linux Environment Variables

For example, the absolute path to the ls command binary is /usr/bin/ls .

Check Path of ls Command

The $PATH variable makes it possible to execute the ls command as:

ls Command Example

To add additional directories to $PATH variable, we can reference the following command:

$ PATH=”$PATH:/path/to/this/directory:/path/to/that/directory”.

Working with Script Files in Linux

Let us for example assume the existence of the following script file.

The contents of a script file.

#!/bin/bash echo "Welcome to LinuxShellTips!"

Let us save the file and make it executable:

When we execute the above script while pointing to its location, we should get a response:

Run Script in Linux

However, executing the command on its own even with sudo leads to an error:

$ onescript.sh or $ sudo onescript.sh

Command Not Found Error

Fixing “Command not Found” Error in Linux

If we point the $PATH variable to the above script’s pwd (parent working directory), we should be able to easily execute it without running into the “command not found” error.

$ PATH="$PATH:$HOME" $ onescript.sh $ which onescript.sh

Set PATH Variable to Script

However, running the script as a Sudoer user still leads to the “command not found” error.

Sudo Command Not Found Error

To permanently fix the “command not found” error, we need to manually modify the /etc/sudoer file.

Firstly, let us move our script file to the $HOME/bin directory:

$ mkdir -p $HOME/bin $ mv onescript.sh $HOME/bin

Let us now modify the /etc/sudoer file.

Identify the line starting with Defaults secure_path and add the path to your script location (in my case: $HOME/bin which translates to /home/dnyce/bin ).

Add PATH Variable to Sudoer

Save the file, and re-run our script with sudo:

Run Script Using Sudo in Linux

We have finally solved the mystery behind the “command not found” error when using Sudo.

By manually editing the variable secure_path on /etc/sudoers file, and pointing it to the bin location of your binary file, you can globally access your scripts and apps execution files without using their absolute path.

Источник

How to fix a «Command not found» error in Linux

Photo of frustrated man reclining with a computer in his lap

When you’re trying to run a command (with or without sudo ) and get an error message that reads «Command not found,» this means the script or file you’re trying to execute doesn’t exist in the location specified by your PATH variable. What is this variable, and how can you run commands that it can’t find?

Читайте также:  Samsung m4020 драйвер линукс

Great Linux resources

Understanding environment variables

In computing, a variable is a placeholder for a value that can change. You use variables every day in normal speech, although you don’t think of them as such. When you say «my laptop,» you’re using «laptop» as a generic variable or placeholder for the computer you’re carrying, regardless of whether it happens to be a Lenovo, Mac, or a Raspberry Pi in a fancy case.

Environment variables are special variables that contain information about your login session. Many of these variables are set by default during installation or user creation. They’re stored for the system shell, applications, and scripts to use when executing commands.

There are global, or system-defined, variables and local, or user-defined, variables.

Global variables

Global variables come predefined in your login shell, but they aren’t immutable and can be modified or deleted according to your preferences. You can use the printenv or env commands to display the environment variables on your system:

$ env SHELL=/bin/bash SESSION_MANAGER=local/kiwi.homelinux.local:@/tmp/.ICE-unix/1906,unix/kiwi.homelinux.local:/tmp/.ICE-unix/19 06 WINDOWID=153092103 COLORTERM=truecolor XDG_CONFIG_DIRS=/home/tux/.config/kdedefaults:/etc/xdg:/etc/kde/xdg LESS=-XR XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1 HISTCONTROL=:ignorespace:ignoredups:ignorespace:ignoredups PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/local/share/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig [. ]

The env command prints out all global environment variables. Variables are case sensitive, and all Linux distributions use uppercase for environment variable names by default.

[ Keep your favorite Git commands, aliases, and tips close at hand. Download the Git cheat sheet. ]

Local variables

A local variable exists only within a specific shell. Therefore, when you define a local variable, it’s only available in your current shell. It doesn’t propagate or persist to a new shell session unless you export it as a global variable.

Local variables are often defined in lowercase to avoid overwriting a global variable with the same name.

The PATH environment variable

The PATH global environment variable lists the directories your system searches for valid, executable commands. By default, it contains standard directories that normally store executables like /usr/bin , /usr/local/bin , and so on.

When you type in a command, such as grep or vim , your system searches through all directories listed in your PATH variable, in the order that they’re listed, until it finds an executable file by the same name. Should it fail to find one, it issues the «Command not found» error.

$ printenv PATH /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin $ env $PATH env: /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/tux/.local/bin:/home/tux/bin

5 ways to fix «Command not found» errors

There are several ways to fix this problem. Here are five of them.

Источник

Оцените статью
Adblock
detector