Chmod command over remote server using ssh NOT working - bash

I have a bash script that copies over files from local to remote server using scp. Once all of the transfer is done, I run chmod command over the ssh to open up all of the permission. The problem is that it intermittently works and does NOT chmod 777 properly all the time. Any way I can verify that chmod command executed definitely (maybe using return code or something)? I do have my ssh keys setup so I do NOT need to enter password upon doing ssh or scp. Below is the snippet of my code
#copy over the files from local machine to remote server
scp file1.txt file2.txt file3.txt 10.111.222.333:/home/user1/fileDir/
#open up the permissions on the remote server
ssh -qX 10.111.222.333 chmod -R 777 /home/user1/fileDir

You could try:
ssh server "chmod 755 file |echo $?"
which would return 0
The |echo $? is checking the return code for the command ran.

Related

Chown directory via SSH on server using NPM script

I am trying to chown a directory via an NPM script. The script looks like the following:
chown -R 755 www-data root#XXX.XXX.XXX.XX:/var/www/test.com
But the message I get back is: chown: www-data: No such file or directory even though this exists. Any ideas much appreciated.
chown operates locally, not on remote servers. In your example, chown is attempting to operate on ./www-data and ./root#XXX.XXX.XXX.XX:/var/www/test.com, which don't exist in the directory of wherever you were when you executed the command.
You will need to execute chown as a command through ssh:
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
Fixed this with following script.
ssh root#XXX.XXX.XXX.XX chmod -R 755 /var/www/test.com/
(I needed to login to the server first).

Running part of bash script on a remote machine

I need to run some commands locally and then some command on a remote machine all using a single local bash script.
For simplicity just say I want to do the following and execute it on my local desktop machine.
#!/bin/bash
#upload some files to a remote machine
cd /tmp
./upload-files.sh
#run commands on remote machine
ssh myuser:mypass#somewhere.com
cd /tmp/uploads <--- these commands don't run in the ssh connection
./process-uploads.sh
exit
#run command locally again.
cd -
echo 'complete!'
Any ideas of how to do this?
You can use here-doc with ssh command:
#!/bin/bash
#upload some files to a remote machine
cd /tmp
./upload-files.sh
#run commands on remote machine
ssh -t -t myuser:mypass#somewhere.com<<EOF
cd /tmp/uploads
./process-uploads.sh
exit
EOF
#run command locally again.
cd -
echo 'complete!'
If you want to run only one command:
ssh myuser:mypass#somewhere.com 'cd /tmp/uploads; ./process-uploads.sh'

Call remote sh script from local sh file

I have a problem with running sh scripts on CentOS which calls remote sh file. On user#host1 I have start.sh file with next command inside
NODE1_SSH_PATH=user#host2
PROGRAM_HOME=/home/user/app
ssh $NODE1_SSH_PATH $PROGRAM_HOME/bin/run.sh > start.log
Result of this script is next:
bash: /home/user/app/bin/run.sh: Permission denied
I tried run this script with chmod like this:
ssh $NODE1_SSH_PATH chmod u+x $PROGRAM_HOME/bin/run.sh > start.log
But in this case I have't got any result, log file is empty to. Could someone help me to slow this I hope simple task?
I believe /home/user/app/bin/run.sh is not executable.
try this
ssh $NODE1_SSH_PATH /bin/bash $PROGRAM_HOME/bin/run.sh > start.log

SCP File command as non-root user on the server

I have some files to upload. Usually to edit anything while logged in the server I must precede the command with sudo. That is known.
How do I send a file then as "admin" instead of "root" when I have disabled root login.
scp path\to\file admin#myaddress.com:/var/www/sitename/public/path/
PERMISSION DENIED
In my opinion, either you should give permissions to the admin user or scp your file to /tmp/ and then sudo mv /tmp/yourfile /var/www/sitename/public/path/.
There is no sudo option when we are using scp command from local to server.
Each user will have upload permission to its own folder in home directory ex. home/xxxxuser so use as below:
scp file_source_here xxxuser#yourserver:/home/xxxuser/
Now you can move file from this folder to your destination.
I suggest these two commands as it works in a bash script.
Move the file to tmp as suggested.
scp path\to\file admin#myaddress.com:/tmp
Assuming admin user can do sudo. The ssh option -t allow you to do sudo command.
ssh -t admin#myaddress.com 'sudo chown root:root /tmp/file && sudo mv /tmp/file /var/www/sitename/public/path/'

chmod syntax in FTP-Client on all subdirectories

Which ftp client or which syntax allows easy chmod for sub-directories?
LFTP allows for recursive CHMOD if the client allows it. You can accomplish this by logging in with LFTP from a Unix/Linux CLI and then run the following:
chmod -R 0755 /www/directory/*
You could also setup a real nifty Bash script for this:
#!/bin/bash
lftp <<EOF
set ftp:ssl-allow no
set ftp:passive-mode true
set ftp:list-options -a
open -u [user],[password] [host]
chmod -R 0777 /www/directory/*
EOF
Of course LFTP does not distinguish between files and folders, for running this command on only files/folders respectively I would suggest using FileZilla. It allows this when running the command on a folder.
I'm pretty sure Filezilla does it
ncftp will support the chmod command if the FTP Server supports it.
As the answer from #Ken G suggests, this is more likely to be a question of "what does the FTP server support".
I tried ncftp (running under Cygwin on Win XP) against Sun FTP running on Solaris 10 (where chmod -R is supported by the o/s version of chmod). I got an error back:
ncftp /work1/jleffler/tmp > chmod -R g+x *
chmod g+x: server said: 'SITE CHMOD -R g+x': command not understood.
chmod *: server said: 'SITE CHMOD -R xx.pl': command not understood.
ncftp /work1/jleffler/tmp >
My suspicion is that few if any systems make it easy. It would be worth checking out whether the NCFTP server helps you.
To chmod all subdirs from where you are (recursive):
chmod -R *
chmod -R 755 {DIR}
You recurse with -R

Resources