Understanding the location, ownership, execution, and SUID of scripts in Linux

Home > Search
  by

Let's start with location. I like to think of this as the location of the script file in the file system. Common navigational commands can be used to manage the location of scripts. For example, the ls (list) command can be used to determine if a script file is in a certain directory. Notice also that the ls command displays the ownership of the example file.

[root@server1 ~]# ls -l /etc/cron.daily-rwxr-xr-x  1  root  root  311  Jan  01  2016  example

 

Likewise, the find command can be used to find a certain file.

[root@server1 ~]# find / -iname example/etc/cron.daily/example

 

You only need the read and write permission to interact with a non-script file, such as a text file.

[root@server1 ~]# ls -l /home/user1/documents-rw-rw-rw-  1  root  root  311  Jan  01  2016  example

 

If you try to run a script file, such as a BASH or SH file, and the file does not have the x (execute) permission, error "permission denied" will appear.

[root@server1 ~]# ls -l /etc/cron.daily-rw-rw-rw-  1  root  root  311  Jan  01  2016  example[root@server1 ~]# bash /etc/cron.daily/examplePermission denied

 

Adding the execute permission to the file allows the script file to be run. With the chmod command, you can use a string of numbers to add the execute permission, such as 755. You could also use +x to add the execute permission to owner, group, and other. Or, you can specify only owner by using the following:

  • chmod u+x = Add execute to user (owner)
  • chmod g+x = Add execute to group
  • chmod o+x = Add execute to other
[root@server1 ~]# chmod 755 /etc/cron.daily/example[root@server1 ~]# ls -l /home/user1/documents-rwxr-xr-x  1  root  root  311  Jan  01  2016  example[root@server1 ~]# bash /etc/cron.daily/exampleHello World

 

SUID of a file

  • When the file is executed, the person who executed the file temporarily becomes the owner of the file while the file is executing, such as when running a BASH shell script (example.sh)

SUID on a directory

  • SUID has no impact on a directory

When using the chmod (change mode) command to modify the permissions of a file, use 4 to add the SUID permission.

[root@server1 ]# chmod 4660 /path/to/file

 

Instead of using 4 for SUID, the letter "s" can be used instead. u+s adds the SUID permission, u-s removes the SUID permission.

[root@server1 ]# chmod u+s /path/to/file #Add SUIDchmod u-s /path/to/file #Remove SUID

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments