LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-02-2009, 07:18 AM   #1
sachin.s.puri
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Rep: Reputation: 0
permission problem


Hi All


Consider a compiled C program - a.out that has only execute permission:
$ ls -l a.out
---x------ 1 Sachin Sachin 4704 Jun 18 19:02 a.out
This program can be run using ./a.out

Consider a shell script - test.sh that has only execute permission:
$ ls -l test.sh
---x------ 1 Sachin Sachin 19 Jun 18 19:05 test.sh
If I try to run this shell script, I get:
$ ./test.sh
bash: ./test.sh: Permission denied

At the system level, what is the reason for the difference between the
two cases?
(Note: Assuming that there are sufficient permissions for the parent directory.)

Thanks in Advance
Sachin
 
Old 07-02-2009, 07:32 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Off the top of my head, your bash shell is trying to "read" the script, so as to interpret the commands prior to running them. Whereas with a binary, it simply executes the compiled code.
 
Old 07-02-2009, 09:35 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,945

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by sachin.s.puri View Post
Hi All


Consider a compiled C program - a.out that has only execute permission:
$ ls -l a.out
---x------ 1 Sachin Sachin 4704 Jun 18 19:02 a.out
This program can be run using ./a.out

Consider a shell script - test.sh that has only execute permission:
$ ls -l test.sh
---x------ 1 Sachin Sachin 19 Jun 18 19:05 test.sh
If I try to run this shell script, I get:
$ ./test.sh
bash: ./test.sh: Permission denied

At the system level, what is the reason for the difference between the
two cases?
(Note: Assuming that there are sufficient permissions for the parent directory.)

Thanks in Advance
Sachin
Since you don't say what's in your "test.sh" script, it's hard to say. xeleema makes a good point, but it could also be whatever command(s) your script is trying to run, you don't have permissions to.

Also, this seems very much like a verbatim homework question..........
 
Old 07-02-2009, 12:42 PM   #4
malekmustaq
Senior Member
 
Registered: Dec 2008
Location: root
Distribution: Slackware & BSD
Posts: 1,614

Rep: Reputation: 440Reputation: 440Reputation: 440Reputation: 440Reputation: 440
Sachin:

meditate on what you are posting......

"Consider...."
"Consider...."
"At the system level... what is the reason...?" "Note: assuming...."

You are not solving an actual newbie problem. Read your textbook. Go to the library. Lazy students have the bad habit of having others solve their homeworks. If you are serious, go post your issue to linux/software forum, not here.

Be reminded that we are NOT tutors for classroom homeworks.
 
Old 07-02-2009, 09:32 PM   #5
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
@malekmustaq - I get the feeling you might be right on the verbatim homework question. I've never taken any "UNIX 101" courses before, but I certainly have contributed to them. Coupled with the lack of a response from sachin.s.puri, I'm tempted to agree with you.

However, even though sachin.s.puri only has two posts, his/her other post doesn't strike me as a homework question, so I'm giving him/her the benefit of the doubt.
(Which I'm hoping will be rewarded by a response shortly, perhaps followed by a "Thanks!" if one of us got this right)
 
Old 07-03-2009, 02:19 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
xeleema, you are correct; executable binaries (regardless of src lang) can be run with only 'x'. Ditto, 'scripts' in any lang eg bash, ksh, perl, awk etc must have 'rx', because the kernel has to actually read them (specifically the first line '#!/path/to/actual_binary' so as to hand off the processing to the named binary.
Alternately, you can create a 'script' file with only 'r' and get the correct 'binary' to read/process it eg

bash file.sh
perl another.pl

etc

Another wrinkle is that on linux (possibly other *nix) the kernel will NOT read/run a shell script that is setuid ie owner 'x' perm is set 's'.
This is for security reasons.
 
Old 07-06-2009, 06:32 AM   #7
sachin.s.puri
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
If my post does reflects like home work question, I am sorry but actually I was learning about unix permissions and found this behavior when I was experimenting with it. The book did not give a clear explanation .

By the way thanks xeleema for the reply
I want to know following from you after reading your reply

1. How is the binary executed, when it does not have a read permission. Doesn't it need to to be "read" into memory first before executing it?

2. I can execute a shell script that does not have a shebang line. How does the kernel know which interpreter to use, when I have not specified the name of the interpreter.

3. When I run a shell script using ./test.sh, it actually the interpreter that is executing. So why does the script too need execute permission?

Thanks
Sachin
 
Old 07-07-2009, 06:23 PM   #8
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 987
Blog Entries: 4

Rep: Reputation: 252Reputation: 252Reputation: 252
Executable Files


Regarding your questions;

1. How is a binary executed without having "read" permissions.
Technically, no. The "read" permissions bit is typically reserved for "userland", not "kernel space". Basically, a shell (like bash) can execute the machine code within a file as long as you have sufficient permission. That being "execute". There's no interpretation of the binary necessary, so you don't have to "read" it first (in the permissions sense), it's "read" by the kernel.
2. I can execute a shell script that does not have a shebang line. How does the kernel know which interpreter to use, when I have not specified the name of the interpreter.
If a she-bang line isn't the first line of a script, it's executed by the currently running shell. So if you're running ksh, it runs in ksh.
3. When I run a shell script using ./test.sh, it actually the interpreter that is executing. So why does the script too need execute permission?
Your currently running shell will "read" and then "execute" the script in question (unless a she-bang tells it to fire up another interpreter). This "read" and "execute" is done at the permissions level to make sure you're not trying to run a "killall" against a root process, etc.
 
Old 07-08-2009, 05:15 AM   #9
sachin.s.puri
LQ Newbie
 
Registered: Mar 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks xeleema

You answers most of My doubts. Its exactly how I had guessed it to be. There are still some grey areas if we want to poke further:

1. When a shell script without a shebang line is executes using ./test.sh, does it run in the current shell, or is a new instance of the current shell created and the script is run in that?
(I think a new instance should be created. If it was not so, then running a shell script with a single line "cd /var/tmp/" would change the directory of the current shell, which is not true. This can also be seen when you run a shell script with a single line "ps". If you run this shell script in bash using ./test.sh, you will see 2 instances of bash. But strangely, when this same shell script is run in ksh, I can see only 1 instance of ksh. I don't know why this anomaly exists.)

2. I still didn't understand this:

> *3.* When I run a shell script using ./test.sh, it actually the interpreter that is executing. So why does the script too need execute permission?Your currently running shell will "read" and then "execute" the script in question (unless a she-bang tells it to fire up another interpreter). This "read" and "execute" is done at the permissions level to make sure you're not trying to run a "killall" against a root process, etc.

Considering that a new instance of an interpreter is fired up every time "./test.sh" is run, can't the execute permission of the interpreter be checked instead of the execute permission of the script, since ultimately its the interpreter, and not the script that is running. Whether or not you are trying to perform a superuser operation would be checked in either case.

Thanks
Sachin
 
Old 07-08-2009, 05:19 AM   #10
jeromeNP7
Member
 
Registered: Jun 2009
Posts: 101

Rep: Reputation: 19
The a.out binary is an application, while the script is a text file. You do not run a text file directly, but by using some other application to read the textfile and parse the commands.
$ ./a.out
will run a binary from the current location
$ sh my.txt
will use the sh tool to read and process the script my.txt which is a text file
$ . my.txt
will use the current shell to 'source' the file my.txt and see what happens (the dot by itself is also a shell command for reading a file, aka sourcing)

If my.txt contains valid shell scripting syntax, then it will be executed. If it contains your latest poem, then you'll get an error message, but you shouldn't take that as a critic on your art ;-)

Linux

Last edited by jeromeNP7; 09-04-2009 at 10:17 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permission problem linux6699 Linux - Newbie 1 07-11-2008 04:01 AM
Samba sharing problem (user permission problem) beikokutn Linux - Software 4 03-11-2008 06:55 AM
Permission problem Fugazi Linux - Newbie 5 10-29-2007 06:29 PM
permission problem - "You don't have permission to access" radone Linux - General 3 07-24-2006 11:23 AM
permission problem.. bit7 Linux - Software 4 02-26-2003 06:08 AM


All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration