Cannot run ANY shell scripts even when root [duplicate] - linux

This question already has an answer here:
bash: /bin/myscript: permission denied
(1 answer)
Closed 8 years ago.
when trying to run a teamspeak server and a minecraft server on a newly rented VPS I ran into some big troubles. Whenever I try to run a shell script even when root it does not work.
One script: spigot.sh
#!/bin/sh
BINDIR=$(dirname "$(readlink -fn "$0")")
cd "$BINDIR"
java -Xms5G -Xmx7G -XX:MaxPermSize=128M -jar spigot.jar
Error after trying to use this as root
root#vps23946:/home/user/minecraft# ./spigot.sh
-bash: ./spigot.sh: Permission denied
Error after trying to use this as user
user#vps23946:~/minecraft$ ./spigot.sh
-bash: ./spigot.sh: Permission denied
Results from ls -l
root#vps23946:/home/user/minecraft# ls -l
total 22616
drwxr-xr-x 16 user root 4096 Jun 6 22:39 backups
-rw-r--r-- 1 user root 2 Jun 7 13:54 banned-ips.json
-rw-r--r-- 1 user root 110 May 25 17:32 banned-ips.txt.converted
-rw-r--r-- 1 user root 229 Jun 7 13:54 banned-players.json
-rw-r--r-- 1 user root 267 May 25 17:32 banned-players.txt.converted
-rw-r--r-- 1 user root 1474 Jun 7 13:54 bukkit.yml
-rw-r--r-- 1 user root 610 Jun 7 13:54 commands.yml
drwxr-xr-x 2 user root 4096 Jun 6 19:56 crash-reports
drwxr-xr-x 2 user root 4096 Jun 7 13:54 C:\Users\Rory Finnegan\Desktop\Prep server\backups
drwxr-xr-x 6 user root 4096 Jun 7 14:25 flat
-rw-r--r-- 1 user root 2576 Apr 3 16:04 help.yml
drwxr-xr-x 2 user root 4096 Jun 7 13:54 logs
-rw-r--r-- 1 user root 415 Jun 7 13:54 ops.json
-rw-r--r-- 1 user root 191 May 28 19:02 ops.txt.converted
-rw-r--r-- 1 user root 0 Apr 3 16:05 permissions.yml
drwxr-xr-x 27 user root 4096 Jun 6 22:39 plugins
-rw-r--r-- 1 user root 768 Jun 7 13:54 server.properties
-rw-r--r-- 1 user root 23053543 May 30 15:48 spigot.jar
-rw-r--r-- 1 user root 122 Jun 7 13:36 spigot.sh
-rw-r--r-- 1 user root 2749 Jun 7 13:54 spigot.yml
-rw-r--r-- 1 user root 2404 Jun 7 14:07 usercache.json
-rw-r--r-- 1 user root 1588 Apr 3 16:04 wepif.yml
-rw-r--r-- 1 user root 783 Jun 6 16:21 whitelist.json
-rw-r--r-- 1 user root 250 May 3 19:31 white-list.txt.converted
drwxr-xr-x 7 user root 4096 Jun 7 14:25 world
drwxr-xr-x 6 user root 4096 Jun 7 14:25 world_nether
drwxr-xr-x 6 user root 4096 Jun 7 14:25 world_the_end
Second Script: ts3server_minimal_runscript.sh
#!/bin/sh
export LD_LIBRARY_PATH=".:$LD_LIBRARY_PATH"
D1=$(readlink -f "$0")
D2=$(dirname "${D1}")
cd "${D2}"
if [ -e ts3server_linux_x86 ]; then
if [ -z "`uname | grep Linux`" -o ! -z "`uname -m | grep 64`" ]; then
echo "Do you have the right TS3 Server package for your system? You have: ` uname` `uname -m`, not Linux i386."
fi
./ts3server_linux_x86 $#
elif [ -e ts3server_linux_amd64 ]; then
if [ -z "`uname | grep Linux`" -o -z "`uname -m | grep 64`" ]; then
echo "Do you have the right TS3 Server package for your system? You have: ` uname` `uname -m`, not Linux x86_64."
fi
./ts3server_linux_amd64 $#
elif [ -e ts3server_freebsd_x86 ]; then
if [ ! -z "`uname | grep Linux`" -o ! -z "`uname -m | grep 64`" ]; then
#
With these I get the same errors.
I am running Ubuntu Server 14.04

Scripts and programs must be executable to be invoked by name. Either use chmod to add the executable permission to the file (chmod a+x ./spigot.sh) or invoke an executable interpreter and pass in the script, e.g. /bin/sh ./spigot.sh

Try
chmod +x spigot.sh
and that will enable the script to be executed

Related

linux prevent user read other directory

I created a user which using the /bin/rbash, so it cannot execute some commands, like 'cd' and 'ls'. But it still can browser other directory when enter some path like '/bin/', then using tab the shell will show the files under 'bin'. And this user only allowed to login through serial port. How can I restrict the user only work in it's home dirctory, and not read other directories.
Doing a quick search I have found this couple of questions that I think may fit your requirements
Create ssh user which can only access home directory
Give user read/write access to only one directory
put
set disable-completion on
string in ~/.inputrc and restart your shell. it will disable completion at all.
this can solve my questions
It is possible to use chroot to implement a user that does not see other directories.
This might be quite crazy solution, and not recommended way to do it.
Create a script that makes chroot
#!/bin/sh
exec /usr/sbin/chroot /home/test /bin/sh
Use the script as login shell (/etc/passwd):
test:x:0:0:Linux User,,,:/:/usr/sbin/chrootsh.sh
Copy all needed files to home directory of the user. You need at least shell and libraries that are needed for the shell:
~ # ls -lR /home/test/
/home/test/:
total 2
drwxr-xr-x 2 root test 1024 Aug 21 13:54 bin
drwxr-xr-x 2 root test 1024 Aug 21 13:54 lib
/home/test/bin:
total 1776
-rwxr-xr-x 1 root test 908672 Aug 21 13:54 ls
-rwxr-xr-x 1 root test 908672 Aug 21 13:54 sh
/home/test/lib:
total 1972
-rwxr-xr-x 1 root test 134316 Aug 21 13:54 ld-linux.so.3
-rwxr-xr-x 1 root test 1242640 Aug 21 13:54 libc.so.6
-rwxr-xr-x 1 root test 640480 Aug 21 13:54 libm.so.6
~ #
Ready. Then login as the user:
~ # su - test
/ # pwd
/
/ # ls -lR /
/:
total 2
drwxr-xr-x 2 0 1000 1024 Aug 21 13:54 bin
drwxr-xr-x 2 0 1000 1024 Aug 21 13:54 lib
/bin:
total 1776
-rwxr-xr-x 1 0 1000 908672 Aug 21 13:54 ls
-rwxr-xr-x 1 0 1000 908672 Aug 21 13:54 sh
/lib:
total 1972
-rwxr-xr-x 1 0 1000 134316 Aug 21 13:54 ld-linux.so.3
-rwxr-xr-x 1 0 1000 1242640 Aug 21 13:54 libc.so.6
-rwxr-xr-x 1 0 1000 640480 Aug 21 13:54 libm.so.6
/ #

Tomcat installed folder access is denied on linux

Please find below set of cmds & outputs
[ec2-user#ip-172-31-38-218 opt]$ ls -al
total 9416
drwxr-xr-x 3 root root 4096 Nov 7 13:52 .
dr-xr-xr-x 25 root root 4096 Nov 7 13:16 ..
-rw-r--r-- 1 root root 9625824 Sep 4 22:52 apache-tomcat-8.5.34.tar.gz
drwxr-xr-x 5 root root 4096 Aug 11 01:26 aws
[ec2-user#ip-172-31-38-218 opt]$ sudo groupadd tomcatgrp
[ec2-user#ip-172-31-38-218 opt]$ sudo useradd -s /bin/false -g tomcatgrp -d /opt/tomcatfolder tomcatuser
[ec2-user#ip-172-31-38-218 opt]$ ls -al
total 9420
drwxr-xr-x 4 root root 4096 Nov 7 13:53 .
dr-xr-xr-x 25 root root 4096 Nov 7 13:16 ..
-rw-r--r-- 1 root root 9625824 Sep 4 22:52 apache-tomcat-8.5.34.tar.gz
drwxr-xr-x 5 root root 4096 Aug 11 01:26 aws
drwx------ 2 tomcatuser tomcatgrp 4096 Nov 7 13:53 tomcatfolder
Now, when I run the cmd I am getting Permission denied as follows
[ec2-user#ip-172-31-38-218 opt]$ cd /opt/tomcatfolder/
-bash: cd: /opt/tomcatfolder/: Permission denied
Can you please help in resolving this above issue..?
I followed instructions of installing Tomcat from this link how-to-install-apache-tomcat-8-on-centos-7
It seems you are the ec2-user, but the directory is owned by tomcatuser and the permissions on the directory do now allow other users or groups to read it.
Try sudo chmod 755 /opt/tomcatfolder/ and then cd into it.
That will add read and execute permissions to the directory.

Laravel Messed up Homestead Permissions

I seem to have messed up Homestead's Linux Permissions, my app is throwing exceptions like not being able to find views that are there and "Impossible to create Root Directory".
I did do a recursive chmod a little while ago which i think is the cause of the problem, the issue is i don't quite know how to revert that.
In my after.sh Homestead script i'm setting the following Provisioning rules
sudo chown -R www-data:www-data /home/vagrant/Code/myapp
sudo usermod -a -G www-data vagrant
sudo find /home/vagrant/Code/myapp -type f -exec chmod 644 {} \;
sudo find /home/vagrant/Code/myapp -type d -exec chmod 755 {} \;
sudo chgrp -R www-data /home/vagrant/Code/myapp/storage /home/vagrant/Code/myapp/bootstrap/cache
sudo chmod -R ug+rwx /home/vagrant/Code/myapp/storage /home/vagrant/Code/myapp/bootstrap/cache
I execute those and nothing happens, it's still getting the same errors, so i went and checked NGINXs nginx.conf file and it's referencing vagrant as it's user so that's correct...
And calling ls -lsa on my root project folder (inside of the Vagrant VM) returns the following information. Any help would be much appreciated, thank you.
0 drwxr-xr-x 1 vagrant vagrant 1280 Dec 18 13:59 .
4 drwxr-xr-x 3 root root 4096 Dec 18 13:29 ..
4 -rw-r--r-- 1 vagrant vagrant 1771 Dec 18 13:59 after.sh
8 -rw-r--r-- 1 vagrant vagrant 7314 Nov 21 09:24 aliases
0 drwxr-xr-x 1 vagrant vagrant 320 Dec 17 17:39 app
4 -rw-r--r-- 1 vagrant vagrant 1646 Nov 21 09:24 artisan
0 drwxr-xr-x 1 vagrant vagrant 160 Dec 12 17:01 bootstrap
4 -rw-r--r-- 1 vagrant vagrant 1707 Dec 17 17:51 composer.json
172 -rw-r--r-- 1 vagrant vagrant 175864 Dec 18 13:24 composer.lock
0 drwxr-xr-x 1 vagrant vagrant 544 Dec 18 13:50 config
4 -rw-r--r-- 1 vagrant vagrant 1534 Nov 21 09:24 CONTRIBUTING.md
8 -rw-r--r-- 1 vagrant vagrant 7970 Dec 16 20:33 _custom_ide_helper.php
0 drwxr-xr-x 1 vagrant vagrant 192 Dec 12 16:40 database
8 -rw-r--r-- 1 vagrant vagrant 6148 Dec 14 16:31 .DS_Store
4 -rw-r--r-- 1 vagrant vagrant 627 Dec 4 12:34 .env
4 -rw-r--r-- 1 vagrant vagrant 499 Dec 4 17:29 .env.example
0 drwxr-xr-x 1 vagrant vagrant 480 Dec 18 14:02 .git
4 -rw-r--r-- 1 vagrant vagrant 61 Nov 21 09:24 .gitattributes
4 -rw-r--r-- 1 vagrant vagrant 154 Nov 21 09:24 .gitignore
4 -rw-r--r-- 1 vagrant vagrant 1278 Dec 14 08:58 .gitlab-ci.yml
4 -rw-r--r-- 1 vagrant vagrant 2389 Dec 18 00:10 gruntfile.js
4 -rw-r--r-- 1 vagrant vagrant 335 Dec 12 10:26 Homestead.yaml
0 drwxr-xr-x 1 vagrant vagrant 544 Dec 18 14:01 .idea
512 -rw-r--r-- 1 vagrant vagrant 464272 Dec 18 13:53 _ide_helper.php
0 drwxr-xr-x 1 vagrant vagrant 24704 Dec 18 00:03 node_modules
4 -rw-r--r-- 1 vagrant vagrant 2164 Dec 18 00:03 package.json
380 -rw-r--r-- 1 vagrant vagrant 385087 Dec 18 00:03 package-lock.json
128 -rw-r--r-- 1 vagrant vagrant 93326 Dec 18 13:53 .phpstorm.meta.php
4 -rw-r--r-- 1 vagrant vagrant 1190 Dec 15 10:28 phpunit.xml
0 drwxr-xr-x 1 vagrant vagrant 320 Dec 12 12:34 public
4 -rw-r--r-- 1 vagrant vagrant 855 Dec 12 10:26 readme.md
0 drwxr-xr-x 1 vagrant vagrant 160 Nov 21 09:24 resources
0 drwxr-xr-x 1 vagrant vagrant 128 Dec 17 16:57 routes
4 -rw-r--r-- 1 vagrant vagrant 563 Nov 21 09:24 server.php
0 drwxrwxr-x 1 vagrant vagrant 256 Dec 18 13:50 storage
0 drwxr-xr-x 1 vagrant vagrant 224 Dec 17 13:42 tests
0 drwxr-xr-x 1 vagrant vagrant 96 Dec 18 13:27 .vagrant
4 -rw-r--r-- 1 vagrant vagrant 1376 Nov 21 09:24 Vagrantfile
0 drwxr-xr-x 1 vagrant vagrant 1280 Dec 18 13:25 vendor
204 -rw-r--r-- 1 vagrant vagrant 208270 Dec 18 13:50 yarn.lock
I've figured it out, for some reason in my /bootstrap/cache directory, laravel was referencing paths on my host machine instead of my vagrant box, clearing this directory did the trick for me. For some reason php artisan cache:clear didn't clear these files.
I've also set up a provisioning script to delete these files for me everytime i provision my Vagrant Box.
# Remove Cache Files (because old cache files can cause issues from time to time) [Keep .gitignore in all Directories]
sudo find "$APPPATH/bootstrap/cache" ! -name '.gitignore' -type f -exec rm -f {} +
sudo find "$APPPATH/storage/framework/cache" ! -name '.gitignore' -type f -exec rm -f {} +
sudo find "$APPPATH/storage/framework/sessions" ! -name '.gitignore' -type f -exec rm -f {} +
sudo find "$APPPATH/storage/framework/testing" ! -name '.gitignore' -type f -exec rm -f {} +
sudo find "$APPPATH/storage/framework/views" ! -name '.gitignore' -type f -exec rm -f {} +

Touch command. permission denied

I was able to connect to my school server via SSH. I had an assignment in which I was supposed to use the touch command to create a new file. Yet it keeps returning permission denied. Others were able to do the same thing. Though why do I keep getting this error?
Below is what was the input from the terminal.
Last login: Tue Aug 23 09:16:18 on ttys000
Dominiks-Air:~ fsociety95$ ssh djaneka1#navajo.dtcc.edu
djaneka1#navajo.dtcc.edu's password:
Last login: Tue Aug 23 09:16:35 2016 from pool-72-94-210-193.phlapa.fios.verizon.net
Navajo is Linux shell server provided to staff, faculty, and students. The
operating system is RedHat Enterprise Linux 5.
Alpine, a Pine replacement, has been provided as a mail client. Run "pine"
at the command prompt.
This server also provides web space to users. Web pages can be stored in
the ~/www directory. This is also accessible by mapping a drive in Windows
to \navajo\homepage. The URL for your homepage is
http://user.dtcc.edu/~username/.
Your home directory is also accessible in Windows by mapping to
\navajo\.
If something appears broken or missing, please email path#dtcc.edu.
Could not chdir to home directory /u/d/j/djaneka1: No such file or directory
-bash-3.2$ touch today
touch: cannot touch `today': Permission denied
-bash-3.2$ pwd
/
-bash-3.2$ touch today
touch: cannot touch `today': Permission denied
-bash-3.2$
Edit: here is the result of ls -al
-bash-3.2$ ls -al
total 204
drwxr-xr-x 25 root root 4096 Aug 22 16:50 .
drwxr-xr-x 25 root root 4096 Aug 22 16:50 ..
-rw-r--r-- 1 root root 0 Aug 3 14:01 .autofsck
-rw-r--r-- 1 root root 0 Jan 30 2009 .autorelabel
-rw------- 1 root root 2050 Aug 3 14:00 .bash_history
drwxr-xr-x 2 root root 4096 May 4 04:14 bin
drwxr-xr-x 4 root root 3072 Aug 3 13:57 boot
drwxr-xr-x 11 root root 4060 Aug 3 14:02 dev
drwxr-xr-x 87 root root 12288 Aug 23 10:05 etc
drwxr-xr-x 3 root root 4096 Oct 1 2009 home
drwxr-xr-x 13 root root 12288 Jun 1 04:09 lib
drwx------ 2 root root 16384 Mar 24 2008 lost+found
drwxr-xr-x 3 root root 4096 Oct 1 2009 media
drwxr-xr-x 2 root root 0 Aug 3 14:02 misc
drwxr-xr-x 4 root root 4096 May 26 2012 mnt
drwxr-xr-x 2 root root 0 Aug 3 14:02 net
drwxr-xr-x 9 root root 4096 Jan 5 2009 nsr
drwxrwxr-x 3 root root 4096 Oct 12 2015 opt
dr-xr-xr-x 219 root root 0 Aug 3 14:01 proc
drwxr-x--- 12 root root 4096 Apr 22 10:06 root
drwxr-xr-x 2 root root 12288 Aug 4 04:02 sbin
drwxr-xr-x 2 root root 4096 Oct 1 2009 selinux
drwxr-xr-x 2 root root 4096 Oct 1 2009 srv
drwxr-xr-x 11 root root 0 Aug 3 14:01 sys
drwxrwxrwt 38 root root 4096 Aug 23 10:07 tmp
drwxr-xr-x 34 root root 4096 Jun 21 08:29 u
drwxr-xr-x 14 root root 4096 Apr 16 2010 usr
drwxr-xr-x 24 root root 4096 Apr 16 2010 var
-rw------- 1 root root 2865 Dec 16 2008 .viminfo
-bash-3.2$
EDIT:
Here is what I see after trying touch today in /home
So to try and create a new document in the root directory you need to be recognised as root. That means using the sudo command.
However for that you would need a password that you may not have. If you do perfect. But in any case I would not recommend adding files to the root directory.
Instead try the following:
cd home
touch today
This should work just fine and answer your question.
Still if you need/want to create today in your root directory try the following
sudo touch today
You will then be prompted for the root password that you can type (if you have it obviously)
In any case I suggest reading this which may be very helpful for you.
I wonder if this was ever truly answered.
If I was looking at it, I would try to see what the system thinks is the home directory of djaneka1, since it may have been setup partway and not completed, leaving stuff owned by root that should have been owned by djaneka1.
If you use the pwd command, and get back the "/" (root) directory there is something wrong with your setup.
The message: Could not chdir to home directory /u/d/j/djaneka1: No such file or directory
tells you it can't find your home directory.
-bash-3.2$ pwd
/
the command "pwd" revealing "/" is just an artifact of the system not being able to find your home directory.
To find what the system thinks is one's home directory,
one can search the file named '/etc/passwd' for one's login name.
I expect this is a possible result if you do that:
$ fgrep 'djaneka1' /etc/passwd
djaneka1:x:1505:1506::/u/d/j/djaneka1:/bin/bash
since it complained that it couldn't find that directory.
This needs to be fixed by someone who has more rights to the system, like root.
there is nothing djaneka1 can do a

rotation of file in shell script

I need to write rotation of files shell script. I have following format data in a target directory(/backup/store_id/dates_folders)
Like :
cd /backup/
drwxr-xr-x 5 root root 4096 Mar 25 12:30 44
drwxr-xr-x 3 root root 4096 Mar 25 12:30 45
drwxr-xr-x 4 root root 4096 Mar 25 12:30 48
drwxr-xr-x 3 root root 4096 Mar 25 12:30 49
cd /backup/44/
drwxr-xr-x 2 root root 4096 Mar 25 12:30 22032014
drwxr-xr-x 2 root root 4096 Mar 25 12:30 23032014
drwxr-xr-x 2 root root 4096 Mar 25 12:30 24032014
drwxr-xr-x 2 root root 4096 Mar 25 12:30 25032014
now 44 (store_id) contain four dates folders. I want each store_id( like 44 folder) contain only three recent dates folder like 23,24,25 & 22 should be deleted. Please help me how to write in shell script. Please give me some hint
This should work:
cd /backup && ls -d */ | while read storeId; do rm -r `ls -r $storeId | tail -3`; done
I assume here that directory names are more important than their timestamps...
If that is not the case, you should use ls -tr instead of ls -r, to let ls command sort on timestamps...

Resources