LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-15-2022, 07:25 PM   #1
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

Rep: Reputation: 1
Remote ssh to another server as www-data


I have a bash script that is called from a PHP generated webpage that needs to call the program "sensors" on another server. The obvious problem I'm facing is how to ssh to the remote server as user www-data in order to do a passwordless command.

I know all about ssh-keygen and ssh-copy-id but as www-data isn't a real user, what are my options?
 
Old 04-15-2022, 07:35 PM   #2
suramya
Member
 
Registered: Jan 2022
Location: Earth
Distribution: Debian
Posts: 249

Rep: Reputation: 102Reputation: 102
You can create a new user on the remote server with passwordless login, configure it so that you can run 'sensors' and then use that in your script.
 
1 members found this post helpful.
Old 04-15-2022, 07:53 PM   #3
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

Original Poster
Rep: Reputation: 1
But since the webserver runs as user www-data and that user is a no login user, how does that work?
 
Old 04-16-2022, 02:28 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
It would be done with a specially crafted line in /etc/sudoers and maybe a shell script server side.

Edit: See the presentation "sudo: You're Doing It Wrong" for an in-depth explanation of how sudo works.

Edit 2: I got the direction wrong. You can go from www-data on the web server to another account on another machine just by specifying the remote account.

Last edited by Turbocapitalist; 04-16-2022 at 09:13 AM.
 
Old 04-16-2022, 03:28 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by KenHorse View Post
But since the webserver runs as user www-data and that user is a no login user, how does that work?
That's irrelevant (though I will add that what you are attempting there is potentially dangerous).
You should get generally more aquainted with ssh before doing this sort of stuff.

You can configure the user you login as both on the command line and in ssh's config file.

There's no need to login as www-data.

Suggested reading:
Code:
man ssh_config
man ssh

Last edited by ondoho; 04-18-2022 at 01:01 AM.
 
Old 04-16-2022, 08:04 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,749

Rep: Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928Reputation: 5928
The client user that connects to the server does not to be the same user on the server itself.

You can create a key for www-data as long as it has the proper permissions for both the files and the directory. Where it is located in the filesystem does not matter.

You can create a special user on the server to run just the sensors command by adding it to the authorized_keys file or limit the user to a ssh chroot jail.

I've never tried using ssh from php.
https://www.php.net/manual/en/function.ssh2-connect.php

Last edited by michaelk; 04-16-2022 at 03:45 PM.
 
Old 04-16-2022, 03:41 PM   #7
KenHorse
LQ Newbie
 
Registered: Dec 2004
Posts: 21

Original Poster
Rep: Reputation: 1
As this is a private network with no internet access, I opted to go with the PHP libssh2 library. Let's me ssh to the other server as root (even if called by www-data) so problem solved.

Thanks for the responses
 
Old 04-17-2022, 08:20 AM   #8
suramya
Member
 
Registered: Jan 2022
Location: Earth
Distribution: Debian
Posts: 249

Rep: Reputation: 102Reputation: 102
Allowing connections to SSH over root is a bad idea especially when the connection can be initiated from a web server. If your web server get breached they also get root access to your second server.

You are ignoring the possibility of insider threats, so even though the server is not connected to the internet you should secure it.
 
1 members found this post helpful.
Old 04-18-2022, 01:03 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by KenHorse View Post
As this is a private network with no internet access, I opted to go with the PHP libssh2 library. Let's me ssh to the other server as root (even if called by www-data) so problem solved.
Well, I still think that allowing root ssh login is always a bad idea...
Anyhow, I still don't see the need. Please look again at my previous post #5, I added some emphasis. Or post #4.
 
1 members found this post helpful.
Old 05-02-2022, 11:42 AM   #10
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by KenHorse View Post
As this is a private network with no internet access, I opted to go with the PHP libssh2 library. Let's me ssh to the other server as root (even if called by www-data) so problem solved.

Thanks for the responses
sensors does not require root level access to run. no reason to give root access to that ssh connection if you are only needing data from sensors
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SCP command for copying .txt files from one remote server to another remote server for last 10 days Sahib Sadman Linux - Newbie 4 04-25-2020 02:57 PM
scp : -l option (bandwidth limiting) doesn't work when scp-ing from a remote server to another remote server stoorky Linux - Server 10 03-05-2018 03:28 AM
Work on a remote project with Eclipse via SSH and SVN+SSH (2 remote Linux) alwaystudent Linux - Software 5 08-12-2015 11:40 AM
SSH shell disabled + Remote desktop NX (or another remote desktop software) zoltan32 Linux - Software 2 04-12-2011 03:36 AM
data shift from 1 remote machine to another remote machine raheelch General 7 03-09-2010 08:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration