LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-25-2015, 12:38 AM   #1
mostafatalebi
LQ Newbie
 
Registered: Jan 2015
Posts: 13

Rep: Reputation: Disabled
Why should I always use chmod when not as a root user


System Info:

I have normal user in CentOS 7 whose name is "mostafa" (the name of the account).

I naturally have another user called root with all privileges. User "mostafa" is put into sudoers file, too.

The OS is installed in VmWare, so the system is all mine.

Problem:

Now I create a file with touch file.sh and put a command in it, but when I want to run it with
Code:
sudo ./file.sh
, an error is shown that the command
Code:
./file.sh
does not exist. But if I
Code:
 sudo chmod 777 ./file.sh
then it gets run. My question is that, why should I use
Code:
chmod 777
when I myself have created the file, and I am in sudoers.

Can anyone explain me why shuold I still use
Code:
sudo chmod 777
when the creator of the file is me.

Last edited by mostafatalebi; 01-25-2015 at 12:40 AM.
 
Old 01-25-2015, 03:13 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
An executable needs to be readable and executable. So you have set it's permission as

Code:
chmod u=rx file
or
chmod a=rx file
 
Old 01-25-2015, 04:46 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,661

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
You should know that "777" permits any action to the file by anyone.

An insecure operation at anytime, particularly so for an executable image.
 
Old 01-25-2015, 04:52 AM   #4
mostafatalebi
LQ Newbie
 
Registered: Jan 2015
Posts: 13

Original Poster
Rep: Reputation: Disabled
I know that. I'm currently practicing with file ownerships and was confused that each time I should do chmod ***
 
Old 01-25-2015, 06:26 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,661

Rep: Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502
Code:
sudo chmod 777 ./file.sh
will not execute your file.sh, but (only) the chmod command itself.

the content of the sudoers file will not modify the execute permissions (that is set by chmod), but will/may change the actual user, and therefore you need to allow the new user to run the script (not to the user mostafa). But actually the user root is an exception, because it has much more privileges. For example it can read all the files (without read and access permissions), and in my debian it can also run that file without proper execute permissions (actually there should be set at least one execute permission - to be able to execute, but irrelevant which one - owner, group or other is allowed to execute)

So the correct answer is: you need to set execute permission before trying to execute that script, and it is valid for every user (yourself too). So chmod 100 file.sh would be enough for root, and chmod 500 for yourself (and chmod 700 if you want to edit it too).
 
Old 01-25-2015, 10:10 AM   #6
Daws
Member
 
Registered: May 2006
Location: UK
Distribution: Debian
Posts: 448

Rep: Reputation: 37
Quote:
Can anyone explain me why shuold I still use

Code:
sudo chmod 777
when the creator of the file is me.
The default permissions are set by your users "umask" value, on almost every system newly created files are not given execute permission. At the very least it's an extra layer of protection between creating and then executing something that could harm your system, I'm sure there are other security related scenarios where files should not be created with execute permissions on by default, but none come to mind at the moment.
 
Old 01-25-2015, 12:28 PM   #7
mostafatalebi
LQ Newbie
 
Registered: Jan 2015
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank you guys. Your answers were good enough for me to get the point.
 
Old 01-25-2015, 02:26 PM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,661

Rep: Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502Reputation: 2502
glad to help you.
If you want to say thanks just press YES. Also, if you think your problem is now solved, please mark the thread solved.
 
  


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
Cannot delete a directory that has been chmod to 777 and I am a root user fail_distraction Debian 3 05-19-2013 08:28 PM
How to revert the permissions set on chmod as a root user prashsharma Linux - Security 2 01-05-2009 10:29 PM
NFS Root setup - can't log in as root after CHMOD ? gallwapa Linux - General 1 04-02-2007 04:31 AM
Root user unable to rm, mv or chmod Zoar Linux - Newbie 2 06-16-2004 10:23 PM
chmod a-rwx dinesh_p_v cat > dinesh_p_v I am a root user murugesan Linux - Software 0 04-07-2004 12:58 AM


All times are GMT -5. The time now is 11:03 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