Why Do You Need the Explicit Pathname to Execute?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why Do You Need the Explicit Pathname to Execute?
# 1  
Old 11-28-2012
Why Do You Need the Explicit Pathname to Execute?

Hi!
If your working directory contains a file you want to work on, or give as an argument, you don't have to give the explicit pathname, just the filename, like so:
Code:
$ vi while_loop.ksh

But if you want execute an executable file, you must supply the explicate pathname, like so:
Code:
./while_loop.ksh

Because, of course, this won't work:
Code:
$ until_loop.ksh
-bash: while_loop.ksh: command not found

Obviously, the shell is telling a lie. Smilie But why?
# 2  
Old 11-28-2012
I think the reason is because current directory is missing in the PATH variable value. Add current directory and retry:-
Code:
export PATH="$PATH:."

# 3  
Old 11-28-2012
That is a terrible idea. Never put relative paths inside your PATH. At best it's a security risk, at worst, it will cause things to actually fail. The shell tries to cache what programs are available when it runs, but this will not work right when things in PATH are relative. Everything inside PATH must be an absolute path!

Second, there's a reason the current directory is not included for running executables. It's not "missing", it's on purpose. It's to prevent people from dumping malign files into a folder you frequent and having you accidentally run them.

If you want to add your own scripts to the PATH, put them in /home/myusername/bin and add /home/myusername/bin to your PATH.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 11-28-2012
Corona688,
I have a question: I do not have current directory added to my PATH, but still I am able to run scripts without specifying absolute path in HP-UX & KSH
Code:
# uname
HP-UX
# echo $SHELL
/usr/bin/ksh

But this is not the case with Linux & BASH:-
Code:
# uname
Linux
# echo $SHELL
/bin/bash

Why is this behavior? Is this something to do with the UNIX shell?
# 5  
Old 11-29-2012
Quote:
Originally Posted by Corona688
That is a terrible idea. Never put relative paths inside your PATH. At best it's a security risk, at worst, it will cause things to actually fail. The shell tries to cache what programs are available when it runs, but this will not work right when things in PATH are relative. Everything inside PATH must be an absolute path!

Second, there's a reason the current directory is not included for running executables. It's not "missing", it's on purpose. It's to prevent people from dumping malign files into a folder you frequent and having you accidentally run them.
Ok, I kinda get that it might be about having executables in permissions protected directories, but if all you need to do is use the absolute pathname, it doesn't seem like much security. Especially if it's just a dot-slash. Of course, it forces you to make sure executing it is what you want to do.
So you guys think that's what the deal is?

---------- Post updated at 11:42 PM ---------- Previous update was at 11:41 PM ----------

Quote:
Originally Posted by bipinajith
I think the reason is because current directory is missing in the PATH variable value. Add current directory and retry:-
Code:
export PATH="$PATH:."

I have no need to put these practice scripts I'm doing in my PATH. It's not an issue - just something I was wondering about. If I ever write something useful, I'll stick it in /usr/local/bin Smilie

I should have mentioned - I'm running Mac OS, bash shell.

Last edited by sudon't; 11-29-2012 at 12:54 AM.. Reason: mention OS
# 6  
Old 11-29-2012
Quote:
Originally Posted by sudon't
Ok, I kinda get that it might be about having executables in permissions protected directories, but if all you need to do is use the absolute pathname, it doesn't seem like much security.
In fact it is: directories where (system) binaries are stored, like "/usr/bin", are writable only for root (and probably for a very select group of other system users). This means that only root can change the contents of the directory. As normal work never happens under root (at least this should be so) normal users and their processes can only use what is in there and not modify it.

Suppose the following: you use "/usr/bin/ls" by typing just "ls" because your PATH contains "/usr/bin". Now i write a program which erases everything in your HOME directory. I couldn't run that program, because the system would not let me. Therefore i place it somewhere and name it "ls". Because you have "." in your path once you enter the directory where this program file is and type "ls", this is executed instead of "/usr/bin/ls" and now you are requesting your HOME directory to be deleted - what the program now is allowed, because it runs under your ID. Mission accomplished.

In fact this makes for an awful lot of security if you do not use root for your daily work, just for system administration purposes) and do your normal work (like surfing the web, etc.) only under your user-ID.

Quote:
Originally Posted by sudon't
If I ever write something useful, I'll stick it in /usr/local/bin
This is a good idea and very very close to what the expert way is. An even better idea would be to: make /usr/local/bin writable only by root. Put in there only things you want to use with all the users on the system. Create a directory $HOME/bin, which will be writable only by you. Put all the scripts which are only for your use there. This is the most canonical way of doing this. Don't forget Unix is truly a multi-user system (unlike Windoze, which is implicitly single-user, even in modern versions. The multi-user-feature is obviously "tinkered on second thoughts".) and you should separate things needed only by you and things needed for everyone on the system - even if it is your own system and nobody else will ever use it.

I hope this helps.

bakunin

Last edited by bakunin; 11-29-2012 at 07:26 PM..
This User Gave Thanks to bakunin For This Post:
# 7  
Old 11-29-2012
Quote:
Originally Posted by sudon't
Ok, I kinda get that it might be about having executables in permissions protected directories, but if all you need to do is use the absolute pathname, it doesn't seem like much security.
Quote:
In fact this makes for an awful lot of security
I should have been a bit more specific. What I meant here, we were speaking of an executable that would be sitting somewhere outside of my PATH, in my working directory, needing the explicit pathname to execute. I was wondering why I have to type that dot-slash to execute something in my working directory, when it's not needed to give the same file as an argument to a command.
The thing that surprises me, is that bash says "command not found," when it had no trouble finding the file only a moment before.
If I may borrow from your example, someone who gained access to my account might look around and see the little innocuous scripts I'm writing, replace one of them with your malicious script, giving it the same name as one of mine. There's nothing stopping me from executing it, except typing dot-slash.
In that sense, I don't see how having to type the explicit path for executables in my working directory, (dot-slash), gives anymore real security than not having to. Is there a reason one has to do that to execute a file?

Quote:
An even better idea would be to: make /usr/local/bin writable only by root.
It looks like it's already set up like that. Well, wheel can write. That's root and admin, right? I know I have to authenticate to write to it.
Code:
$ ls -l /usr/local |grep [^s]bin
drwxrwxr-x  50 root  wheel  1700 Nov 17 16:08 bin

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. IP Networking

Add explicit route

Add explicit route to 10.128.255.41/32 , gateway: 10.128.201.254 if not working, please try gateway through management port: 10.128.55.254 Just want to double confirm if this would be the correct command #route add -net 10.128.255.41/32 10.128.201.254 And if didnt work #route add... (1 Reply)
Discussion started by: Thilagarajan
1 Replies

2. Shell Programming and Scripting

SSH shell script to access FTP over explicit TLS/SSL

Hello, I use the following SSH script to upload *.jpg files via FTP: #!/usr/bin/expect set timeout -1 spawn ftp -v -i expect "" send "\r" expect "Password:" send "\r" expect "ftp>" send "mput *.jpg\r" expect "ftp>" send "quit\r" replaced with actual ftp server/account data. ... (5 Replies)
Discussion started by: mrpi007
5 Replies

3. Shell Programming and Scripting

Perl : Global symbol requires explicit package name Error while executing

I have executed the below perl script for copying the file from one server to another server using scp. #!/usr/bin/perl -w use Net::SCP::Expect; use strict; $server= "x.x.x.x"; my $source = "/mypath/mypath"; my $destination = "/home/"; print "Login...Starting scp..."; $user="admin";... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. AIX

X connection to localhost:10.0 broken (explicit kill or server shutdown)

I want to run applet on AIX 6 machine. I already have setup $DISPLAY variable for putty session by selecting X11 option. I got below error for any X related commands (xclock, X, applet viewer ) X connection to localhost:10.0 broken (explicit kill or server shutdown). Please can anyone... (0 Replies)
Discussion started by: kailas.girase
0 Replies

5. Cybersecurity

IPF pass in connection to port 21 even with no explicit rule

I'm running IPF on solaris 10 bash-3.00# ipf -V #display ipf version ipf: IP Filter: v4.1.9 (592) Kernel: IP Filter: v4.1.9 Running: yes Log Flags: 0 = none set Default: pass all, Logging: available Active list: 1 Feature mask: 0x107 with the following rules bash-3.00# ipfstat -o -i... (0 Replies)
Discussion started by: h@foorsa.biz
0 Replies

6. UNIX for Dummies Questions & Answers

finding pathname for directory

Hi Could someone help me? I'm not sure how to find the full pathname of a directory. I just want to be able to specify a directory. e.g directory1/directory2/directory3/directory4/directory5 I want to be able to put in "directory5" and then i want a return of the full address. ... (3 Replies)
Discussion started by: shomila_a
3 Replies

7. UNIX for Advanced & Expert Users

connection to localhost:10.0 host broken (explicit kill or server shutdown)

Hi All, We use tomcat web server and it will get terminated with below error: connection to localhost:10.0 host broken (explicit kill or server shutdown) Please let me know how to fix this error. (5 Replies)
Discussion started by: bache_gowda
5 Replies

8. Shell Programming and Scripting

Getting pathname variables with ksh

With C Shell you can get the root, head, tail and extension of a pathname by using pathname variable modifiers. Example Script: #! /bin/csh set pathvar=/home/WSJ091305.txt echo $pathvar:r echo $pathvar:h echo $pathvar:t echo $pathvar:e The result of executing this script is: ... (7 Replies)
Discussion started by: BCarlson
7 Replies

9. UNIX for Dummies Questions & Answers

find without pathname

How can I get the results of a find back without the pathname for example if i do find ../../ -name \*.sql i dont want to see directory/directory/filename.sql I only want to see filename.sql (3 Replies)
Discussion started by: MBGPS
3 Replies
Login or Register to Ask a Question