SCP File command as non-root user on the server - shell

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

Related

Chmod command over remote server using ssh NOT working

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.

sudoless passwordless script

I want to make a sudoless passwordless script which doesn't prompt for password to any user executing that script.
Problem: my script contains rsync utility to send files to a backup server, but i always get permission denied error when the folder which needs to be send contains some files for which the access is set to none. whereas other files are also owned by root but their access is set to read only. I'm using public key authentication to send files to destination/backup so that it doesn't prompt for password input but it cannot send access protected files like above without sudo and password input.
Tries: I tried https://unix.stackexchange.com/a/229653/332764 this solution but it is not working. Still same error is there.
EDIT: sudoers file
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
ortega ALL=(ALL) NOPASSWD: /home/usr/path/transmit_ckpnt.sh
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
You have a few options:
Setup a root crontab job to do a chown (Have root do a job periodically)
Use docker privilege escalation to chmod your file in a container with a mounted volume.
Devise a non-root task equivalent to the root one you are doing
1. Root crontab
A crontab schedules commands execution at Specified time or time interval. Here you could create a root crontab with the command sudo crontab -u root -e. This will open you into an editor to write your crontab: best practices
2. Docker privilege escalation
This method requires either already being root or being part of a machines docker group. As a non-root user who is a member of the docker group you have the ability to mount any directory as a volume within a docker container. Within the container you are root, and changes you make to the mounted volume will persist. Simply chmod the file in the container.
File Permissions
Use chown and chmod to change file-system permissions.
Password-less rsync
The rsync command supports using ssh-keys which allow you to log into other machines password-less after a quick one time key-exchange.
If you cannot chown or chmod a file because of a permission error, that is because a user with more permissions than you (root) will need to help you change the file permissions.
I use this Sudoless no password bash script
1. Open the /etc/sudoers file (as root, of course!) by running:
sudo nano /etc/sudoers
2. At the end of the /etc/sudoers file add this line:
username ALL=(ALL) NOPASSWD:ALL

unable to excute command without sudo root

remote user:ab
escalated user: UNIX
when i am doing copy module to /etc/profile.d/.its throwing error permission denied.
but with shell and command module.
sudo cp myscript.sh /etc/profile.d/
its working from UNIX user.i want to use ansible module rather than shell or command.here issue with sudo from UNIX user to execute command with sudo privileged.Become user i can't use root directly. Dont have access through unix user i can use sudo.
already used below details.
become=yes
become_method=sudo
become_user=unix
become_ask_pass=false
sudo cp means that it is running as root, not the user unix.
Try removing the line
become_user=unix

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

Get permission to work with files on my Amazon EC2 Ubuntu server using FTP-WinSCP

I have an Ubuntu server on Amazon, I installed everything for working with it (php, mysql, phpmyadmin, apache..), The problem is that I cant move around or edit files using the FTP.
I get the error message:
Permission denied.
Error code: 3
Error message from server: Permission denied
from my WINSCP.
The only way I can trancefer/edit file is using 'putty' with the sudo/nano command.
I found a lot of information about this on google, but there are no updated soulution I can find. Linux isn't my usual work-space.
How do I get the permissions working for the WINSCP ?
A lot of answers say to change permissions on /var/www/ ... however AWS shortcuts out of the WWW folder and into the /var/app/current/ directory. If all else fails try:
sudo chown -R -v ec2-user /var/app/current/
On Ubuntu image in AWS, default user is ubuntu. Instead of using system folders like /var/... better to use /home/your_folder. Then change the ownership using {chown} command. For eg.
$ sudo chown -R -v ubuntu /home/your_folder/
This will change the ownership of 'your_folder' and contents inside it from 'root' user to 'ubuntu'. Then Winscp should be able to upload/delete/create files using SFTP/FTP etc..
I had the same issues. The solution is: You can solve this by changing WordPress ownership type:
sudo chown -R www-data:ubuntu /var/www/wordpress
Then, Change the right permission for the particular files and directories, type command
sudo find /var/www/wordpress/ -type d -exec chmod 755 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 644 {} \;
Apart from this, set the following important files to chmod 600 so that only the owner can fully read and write access to these files:
sudo chmod 600 /var/www/wordpress/wp-config.php
sudo chmod 600 /var/www/wordpress/.htaccess
Then everything will work. You will be able to access your wordpress files
for me using free tier from cloud providers will not give you root access, to fix this you have to change folder permission from SSH or try this solution
OS: windows 10 Pro
Open WinSCP instead of SFTP you select SCP go to advance settings click on SCP/Shell and change the shell option to sudo su -
Now you will be able to get permission.
Change file permision by command chmod 777 /var/app/current/ and transfer files to the directory

Resources