no such file or directory: mount_smbfs {mount point} {local directory} - linux

How can I get mount_smbfs to work on a Mac zsh terminal from within a loop?
The identical commands work when run one by one, but they throw an error when issued from within a loop: no such file or directory: mount_smbfs {mount point} {local directory}.
So, when I source something like this (the export commands actually live in their own script that gets sourced first):
export H_DIR="/mnt/${USER}"
export I_DIR="/mnt/I"
export DUM_HOST="dummy.host.ninja"
declare -A mounts
mounts[${H_DIR}]="//${USER}#${DUM_HOST}/${USER}"
mounts[${I_DIR}]="//${DUM_HOST}/I"
for dir mount_point in ${(kv)mounts}; do
if [ ! -d $dir ]; then
echo "Making $dir and intermediates."
mkdir -p $dir
#else
#command="diskutil unmount $dir"
#echo "command: $command"
#$command
fi
command="mount_smbfs $mount_point $dir"
echo "command: $command"
$command
ls -alt $dir
done
I get something like this:
command: mount_smbfs //me#dummy.host.ninja/me /mnt/me
/Users/me/.zsh_setup/mount_all.zsh:24: no such file or directory: mount_smbfs ///me#dummy.host.ninja/me /mnt/me
total 0
drwxr-xr-x 3 me staff 96 Dec 20 14:30 ..
drwxr-xr-x 2 me staff 64 Dec 20 14:30 .
command: mount_smbfs //dummy.host.ninja/I /mnt/I
/Users/me/.zsh_setup/mount_all.zsh:24: no such file or directory: mount_smbfs ///dummy.host.ninja/I /mnt/I
total 0
drwxr-xr-x 3 me staff 96 Dec 20 14:30 ..
drwxr-xr-x 2 me staff 64 Dec 20 14:30 .
But, I can just paste the same command that was run in the loop, and it works.
me#my_mac ~ % mount_smbfs //me#dummy.host.ninja/me /mnt/me
Password for dummy.host.ninja:
me#my_mac ~ %
me#my_mac ~ % ls -alt /mnt/me
total 1499808
drwxr-xr-x 3 me staff 96 Dec 20 14:30 ..
drwx------ 1 me staff 16384 Dec 20 14:02 .
-rwx------ 1 me staff 10621 Dec 20 14:02 .bash_history
... A bunch more folders that live on the mounted host ...
me#my_mac ~ %
me#my_mac ~ %
me#my_mac ~ % mount_smbfs //dummy.host.ninja/I /mnt/I
me#my_mac ~ %
me#my_mac ~ % ls -alt /mnt/I
total 960
drwx------ 1 me staff 16384 Dec 20 18:33 .snapshot
drwxr-xr-x 7 me staff 224 Dec 20 14:30 ..
drwx------ 1 me staff 16384 Sep 21 11:22 .
... A bunch more folders that live on the mounted host ...
me#my_mac~ %
For context:
me#my_mac ~ % ls -l /
total 9
drwxrwxr-x 21 root admin 672 Dec 17 11:03 Applications
drwxr-xr-x 67 root wheel 2144 Dec 16 10:03 Library
drwxr-xr-x# 9 root wheel 288 Oct 12 23:06 System
drwxr-xr-x 6 root admin 192 Dec 16 09:45 Users
drwxr-xr-x 3 root wheel 96 Dec 20 12:38 Volumes
drwxr-xr-x# 38 root wheel 1216 Oct 12 23:06 bin
drwxr-xr-x 2 root wheel 64 Oct 12 23:06 cores
dr-xr-xr-x 3 root wheel 4507 Dec 20 12:38 dev
lrwxr-xr-x# 1 root wheel 11 Oct 12 23:06 etc -> private/etc
lrwxr-xr-x 1 root wheel 25 Dec 20 12:38 home -> /System/Volumes/Data/home
lrwxr-xr-x 1 root wheel 26 Dec 20 12:38 mnt -> Users/me/mounts
... and so on ...
me#my_mac ~ %
me#my_mac ~ % ls -l /Users/me/mounts
total 32
drwx------ 1 me staff 16384 Sep 21 11:22 I
drwxr-xr-x 3 me staff 96 Dec 20 14:30 me
me#my_mac ~ %
I need to enter my password once for the first mount, but not again because I use ssh keys. If I mount one manually to open the ssh key then run the for loop with other locations, that still won't work, so I don't think that's the issue.

As #GordonDavisson mentioned in the comments, the issue isn't with mount_smbfs but with how the command is parsed when assigned to a string variable. The best solution is to not do this, as seen here: Assigning command to a variable in Zsh. There are some suggestions for how to use a string variable as your actual command call, but I opted for just not doing it.
Making this minimum change works:
command="mount_smbfs $mount_point $dir"
echo "command: $command"
# Replace ...
# $command
# ... with the actual command:
mount_smbfs $mount_point $dir
Assigning the full command to a string was just for the convenience of echoing the command to the console before calling it, for logging and debugging. You can still do this, but it just won't be so tightly associated with the command itself, which isn't a real problem in this context where the command string variable is only used once right before calling the actual command. There are some suggestions in the linked post for how to get around this if you really want to use a string variable as your actual command call.
Also as mentioned in the comments and the linked post, it is perhaps best to wrap the command in a function. I tend to disagree in cases like this where it is a one-line command that is essentially function call itself. I'm not doing anything but calling it, no extra validation or anything. Wrapping it just adds an unnecessary layer of convolution. However, I haven't sought out a full explanation for why it might still be best practice.
That said, I will re-wrap the full script (minus the exports) back up into a set of functions as I originally did before troubleshooting and posting here. That's a good case for a function encapsulating a command or set of commands.

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
/ #

Cannot run ANY shell scripts even when root [duplicate]

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

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...

Can't CD to directory inside of root

I am currently creating an application that requires separate users running duplicate programs. They cannot run under root because of security reasons, so they are initiated by a Java app that I am working on, and I am starting them with runuser -l. However, I cannot cd into a directory, even though it is owned by the user, and the user has 770 permissions in the folder.
Here's what I'm running:
runuser -l lp1 -c 'java \-jar /root/Desktop/workspace/LitePanel/servers/server1/server.jar \-Xms1024M nogui'
And the output of this is:
runuser: warning: cannot change directory to /root/Desktop/workspace/LitePanel/bin/servers/server1/: Permission denied
Here's an ls -all:
drwxr-xr-x. 3 root root 4096 Jan 30 14:03 .
drwxr-xr-x. 7 root root 4096 Jan 30 14:02 ..
drwxrwx---. 2 lp1 lp1 4096 Jan 31 03:07 server1
Inside the directory:
drwxrwx---. 2 lp1 lp1 4096 Jan 31 03:07 .
drwxr-xr-x. 3 root root 4096 Jan 30 14:03 ..
-rwxrwx---. 1 lp1 lp1 9170551 Jan 31 03:07 server.jar
And here's /etc/passwd:
lp1:x:501:501::/root/Desktop/workspace/LitePanel/bin/servers/server1/:/bin/false
Anyone know why this is happening? It looks like the user has the necessary permissions to do this.
You have said that the directory itself has permissions 770 and is owned by the user, but what about its parents? I believe the cd command will need at least read access (and possibly execute) on the parent directories.

scp file not setting correct owner

Does SCP have a problem setting file permissions or have I misconfiguration my server?
Use case:
There is a file on a server that I want to edit called "importantFile.txt". The file has owner and group of "master":
ls -l importantFile.txt:
-rw-rw-r-- 1 master master 7 Mar 18 08:11 importantFile.txt
I am called "slave" but luckily, I am in group "master" so I can edit the file as I see fit. However, I'm a lazy slave and can't be bothered to edit the file on the server, I'd prefer to edit the file on my local machine and SCP it to the server:
echo "bored slave info" > importantFile.txt
scp importantFile.txt slave#theServerAddress:/pathToFile/importantFile.txt
If I do this, the contents of the file on the server are uploaded fine and the timestamp of the file is updated but the permissions of the file don't change, the file is still owned by "master". This is a problem because if "slave" uploaded bad content, no one would know it was "slave" who caused the problem, "master" would look guilty.
Perhaps I have to set a umask? if so where? I tried .bash_profile without success and haven't found anything on Google about umask in /etc/ssh/sshd_config.
That's nothing special about scp - try logging on to the server as slave, and editing the file using your favourite text editor... You'll find the same behaviour occurs... Writing to a file does not make you the owner of the file.
Example:
as root
#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 0 2009-03-19 10:30 testfile
#echo foo > testfile
#ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
as user (in vboxusers group)
>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
>echo bar >> testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:31 ..
-rw-rw-r-- 1 root vboxusers 12 2009-03-19 10:31 testfile
>cat testfile
foo
bar
baz
You have to delete the file to overwrite it. Wether you are able to do that depends on the directory's permissions and ownership. Hijacking ownership of an already existing file is not possible. The write permission you have is only applied on the contents of the file.
It seems you can configure how Emacs deals with this through the backup-by-copying-when-mismatch variable (see the Emacs Manual or type C-h-v backup-by-copying-when-mismatch in Emacs).
I actually filed a bug report about this, because I thought it was a bug in Tramp.
I had misunderstood the way files work, modifying file contents do not change ownership or group.
Why the confusion? EMACS - Whenever I was editing a file I was using Emacs and Emacs does change the owner and group to the current user. It does this because it makes a backup file at save time by moving the "filename" to "filename~" and creating a new file called "filename" - because it's a new file, it has the current users file permissions. I guess this is 1up to VI fans?

Resources