How to specify local machine when already sshed into another computer? - linux

It's easy to simply scp from another computer to the local one or vice versa. However, how to do it when you already sshed into another computer?
user#anothercomp:~/$ssh ~/path/file (how to specify local computer?):~/localpath/file
Note that my computer is a personal laptop instead of a server, so answers here Download files to local drive when sshed do not apply since it requires to specify the domain name, and my laptop is no server.
So what's the correct way to specify my local computer? (Please don't say exit ssh and then scp, since it will possibly lose temporary data when working)

You don't need to exit ssh, you can run scp from another virtual terminal in your laptop.
For convenience, you may want to try connection sharing, to avoid the need to enter passwords again; you may either use ssh -M or modify ~/.ssh/config. See man ssh and man ssh_config for details.

By SSH'ing into the second machine you lose the context of your first. For the purpose of transferring files the second machine would need to scp the files to the first, or could use a different (yet similar) approach such as sshfs.
If the issue is that the second machine does not have network access to the first, then tunneling is the likely solution. However, this just enabled scp or sshfs to connect--it doesn't let your ssh session access paths or variables from your local machine.

While your computer does not have a domain name, it still has an IP address, so you can connect to the IP address. You can retrieve the address from the SSH_CLIENT variable. See How to get the IP address from $SSH_CLIENT.
Obviously to ssh or scp to your computer, you need to be running an SSH server there. What is not common, if you are running Windows, in particular.

Related

Remote SSH/SFTP with Netbeans

I know you can set up remote connection in Netbeans or as others have suggested, mount the remote file system locally and let Netbeans read that.
This current set-up I have to SSH into a remote server and then SSH again into another one.
Is it possible in Netbeans to access the filesystem on the 2nd server hop?
I think you can setup a SSH local port forwarding on your first remote server to archive what you want here. Once ssh connection established with the first server, it will be tunnel to second server. Also, will be much more convenient if using SSH public key as authentication method.
Run below command on your local (Need to keep this running)
ssh -NL 2222:server2.example.com:22 server1.example.com
Test connection with ssh locally
ssh localhost 2222
So in your case, Netbeans just need to connect to localhost port 2222.
Ref: https://www.ssh.com/ssh/tunneling/example
That isn't answer you expect, but to big to put it in comment.
I think best solution is configure autostart some service which will create tunnel to first server on your login and bind local port (let's say 2222) to second remote server ssh port. This is basical feature of ssh client. When you can configure Netbeans to connect to localhost:2222 and login right into second server. You might need to use keys authentication for first server.
Where are examples to make such thing. Like is underneath link on article
http://www.pc-freak.net/blog/start-ssh-tunnel-pc-boots-windows-alwaysup/
Since you can get ssh access via a daisy-chain, you can very likely use SSHFS:
https://github.com/libfuse/sshfs
Available in Debian (and therefore, likely many other distributions):
https://packages.debian.org/jessie/sshfs
As long as you have ssh keys set up properly on each server, you can daisy-chain mounting via SSHFS to the machine where your NetBeans app is running.
https://en.wikipedia.org/wiki/SSHFS
NOTE: There are some caveats, including that SSHFS expects that it is the only mechanism used to update the filesystem, and that there is only one writer at a time. Undesirable behavior can occur if you allow other mechanisms to change the files or directories on the target server, including data loss and/or corruption.

How To Find Credentials of Host Machine in VM?

I thought I was sshing into an old server, but it turns out to be a VM into which I ssh'd. I don't know the credentials of the host machine and would like to get this to connect to it directly. Is there a way I can find this through the VM?
EDIT:
Suppose I just want whatever I can get - in this case, perhaps just the IP, as robmathers notes.
dmidecode | egrep -i 'system-product-name'
gives me "VMware Virtual Platform".
No. Passwords in Linux are stored in encrypted form and you can't read them. But if you can change the password by mounting the virtual hard drive and modifying /etc/shadow (unless the drive is encrypted). There are many guides how to do that.

How to copy files on remote Windows to local Linux on a Linux machine?

Currently, I'm working on a local Linux machine. I'm trying to use scp or similar Linux command-line tools to copy files from a remote Windows machine to my local Linux. I did some searching and found that most of the solutions are for local Windows cases (like putty and winscp), which don't really help.
Please advise.Thank you.
[UPDATE] Solved by installing cygwin's sshd service on Windows.
If you really want to use SCP, you will need an SCP server (actually an SSH server) on the Windows machine.
For example freeSSHd.
You will need to choose one of the options based on your own needs, there are a number of similar tools and freeSSHd was the first in the list on Google. I've used the Bitvise SSH Server in the past but it is only free for non-commercial use.
They are usually very easy to set up. You install them the usual way and run them for the first time. Depending on the tool, they may pick up your existing Windows users or you may need to manually create some users with passwords within the tool. Then, armed with your PC's IP address, you should be able to connect to the PC using SSH from the Linux command line.
If the windows system has a shared folder you should be able access that with smbclient which is part of the terrific samba project.
Usually somthing like:
smbclient //winmachine/share
Possibly using the -U username options to specifiy the username on the windows box.
Once connected, you can use cd to change folders, and get to retrieve files.
If there is no file share.... I dunno. Create one?
Syntax for copying from remote Windows 10 machine with built-in SSH server. Note forward slashes and drive style. Domain is not necessary.
scp user#domain#example.com:c:/path/to/file.txt .

SSHFS through multiple connections

I have to occasionally work remotely. In order to do so, I have to connect to a gateway server, through which I can then connect to the development server. At work I can connect directly to the dev server. I use SSHFS to map the remote folder to a local one (in Ubuntu). My colleagues don't seem to mind using vim for all of their work, but I really prefer and IDE. I know that using just ssh I can tunnel through multiple connections (ssh -t server1 ssh -t server2), but I'd like to do the same with SSHFS. Does anyone know how this could be accomplished?
Yes, it can be done. For this, the remote server has to support local port forwarding (which might be disabled for security reasons). What you bascially do, you instruct the remote server to open a tunnel to a server in the other network for you:
ssh -fL 127.0.0.1:someport:host.in.the.remote.net:22 proxy.host
someport should be an unused port on your machine (for example 2222), host.in.the.remote.net should be the hostname or IP of the development machine you eventually want to connect to, from within the network of the proxy.host, which is the intermediate server you have to go through. -f instructs ssh to detach from the terminal after the connection is established.
You run this command, and after it is in the background, the remote machine's ssh port can be referred to as 127.0.0.1:someport and can be used as such by sshfs. I don't know of a way to automate this though, but you should easily be able to script it.
Make sure you clear this action with the remote administrator beforehands if you're unsure about the policies. You may need to change the 22 in the first command if the development machine serves ssh on a different port.

Remotely Programming

I'm doing my development work on my Windows machine, but my compiling on a remote Linux machine. What I currently do is start an X server on Windows, ssh into the Linux machine, then do the development remotely.
What I'd like to do is edit my source on the Windows machine, and have it automatically copy files over to the Linux system when I save. I'd also like for my built-in compilation commands to perform a build on the remote system.
If it makes a difference, the source is all in C, using GCC. In descending order of preference, I have Emacs, Vi, and Netbeans on my desktop, and am willing to install another IDE for a last resort.
This is certainly doable in vim. You can use the scp:// protocol within vim to edit remote files, and set up a command that writes a local copy. You can also change what program vim uses for :make to do an ssh make instead on your server.
You'll need to set up your ssh-keys to keep this painless (otherwise you'll be entering your password all the time) but that's fairly easy.
Another alternative would be to push to a remote repos as part of your make command, instead of editing remotely.
EDIT:
First, using the scp:// protocol within vim. From :help netrw-start (or down the page from :help scp)
Netrw supports "transparent" editing of files on other machines using urls
(see |netrw-transparent|). As an example of this, let's assume you have an
account on some other machine; if you can use scp, try:
vim scp://hostname/path/to/file
Want to make ssh/scp easier to use? Check out |netrw-ssh-hack|!
You can also use scp:// paths in :edit commands, or really anywhere that you could use a normal path.
And, from the mentioned :help netrw-ssh-hack, instructions on how to set up your ssh keys:
IMPROVING BROWSING *netrw-listhack* *netrw-ssh-hack* {{{2
Especially with the remote directory browser, constantly entering the password
is tedious.
For Linux/Unix systems, the book "Linux Server Hacks - 100 industrial strength
tips & tools" by Rob Flickenger (O'Reilly, ISBN 0-596-00461-3) gives a tip
for setting up no-password ssh and scp and discusses associated security
issues. It used to be available at http://hacks.oreilly.com/pub/h/66 ,
but apparently that address is now being redirected to some "hackzine".
I'll attempt a summary based on that article and on a communication from
Ben Schmidt:
(1) Generate a public/private key pair on the local machine
(ssh client):
ssh-keygen -t rsa
(saving the file in ~/.ssh/id_rsa as prompted)
(2) Just hit the when asked for passphrase (twice) for no
passphrase. If you do use a passphrase, you will also need to use
ssh-agent so you only have to type the passphrase once per session.
If you don't use a passphrase, simply logging onto your local
computer or getting access to the keyfile in any way will suffice
to access any ssh servers which have that key authorized for login.
(3) This creates two files:
~/.ssh/id\_rsa
~/.ssh/id\_rsa.pub
(4) On the target machine (ssh server):
cd
mkdir -p .ssh
chmod 0700 .ssh
(5) On your local machine (ssh client): (one line)
ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys2' < ~/.ssh/id_rsa.pub
or, for OpenSSH, (one line)
ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys' < ~/.ssh/id_rsa.pub
You can test it out with
ssh {serverhostname}
and you should be log onto the server machine without further need to type
anything.
If you decided to use a passphrase, do:
ssh-agent $SHELL
ssh-add
ssh {serverhostname}
You will be prompted for your key passphrase when you use ssh-add, but not
subsequently when you use ssh. For use with vim, you can use
ssh-agent vim
and, when next within vim, use
:!ssh-add
Alternatively, you can apply ssh-agent to the terminal you're planning on
running vim in:
ssh-agent xterm &
and do ssh-add whenever you need.
For Windows, folks on the vim mailing list have mentioned that Pageant helps
with avoiding the constant need to enter the password.
Kingston Fung wrote about another way to avoid constantly needing to enter
passwords:
In order to avoid the need to type in the password for scp each time, you
provide a hack in the docs to set up a non password ssh account. I found a
better way to do that: I can use a regular ssh account which uses a
password to access the material without the need to key-in the password
each time. It's good for security and convenience. I tried ssh public key
authorization + ssh-agent, implementing this, and it works! Here are two
links with instructions:
http://www.ibm.com/developerworks/library/l-keyc2/
http://sial.org/howto/openssh/publickey-auth/
For making on remote systems, you need to set your makeprg variable to
do an ssh make. From :help makeprg
Program to use for the ":make" command. See |:make_makeprg|.
This option may contain '%' and '#' characters, which are expanded to
the current and alternate file name. |:_%| |:_#|
Environment variables are expanded |:set_env|. See |option-backslash|
about including spaces and backslashes.
Note that a '|' must be escaped twice: once for ":set" and once for
the interpretation of a command. When you use a filter called
"myfilter" do it like this:
:set makeprg=gmake\ \\\|\ myfilter
The placeholder "$*" can be given (even multiple times) to specify
where the arguments will be included, for example:
:set makeprg=latex\ \\\\nonstopmode\ \\\\input\\{$*}
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.
One option is to use the TRAMP remote-editing package (built into Emacs 22 and newer, and you can install it into older versions). Every time you save your file, Emacs sends its contents over ssh (by default; of course every detail is totally configurable) to the Linux machine. Commands like M-x compile and M-x grep are TRAMP-aware and execute on the remote host.
I would look into continuous integration for your environment. This way you can commit the changes to source control, and have the linux box act as a build server. You can have tests associated and other related interesting stuff you want to be run on the builds.
Update 1: Also this might work for you: http://metamod-p.sourceforge.net/cross-compiling.on.windows.for.linux.html (it is also worth a try doing some searches on similar tools)
Other have suggested SAMBA which may not be feasible on your Linux box. A good alternative is to use Dokan SSHFS on your Windows box to mount a remote directory over SSH.
You could try sharing a disk between your Linux and Windows machines using Samba or something like that. Then you could edit on your local machines and the files would be visible immediately on the remote machine since the drive would be visible to both.
Where I work we have all files on NFS that is accessible from all Linux machines and Windows machines. I don't know how hard it is to set that up since I work in a large corporation and IT is abstracted away from me, but simple disk sharing should be pretty straightforward.
Why do you start an X server on Windows? Personally, I would set up a Linux VM with VMware or whatever your favorite VM technology is (VMware is free and works well). Then choose any Linux distribution you want. You just need very basic functions, mostly the standard "toolchain." You could pick Centos, Ubuntu, Fedora, Debian, whatever. I usually use Centos or Debian. Set it up, and just use PuTTY into your VM. From there, you can scp files to your remote server and so forth. This way you don't have to bother with cygwin or an X server or any of that.
Can you just use a samba share to save the files directly on the remote machine? I often do PHP this way.
Then just have a putty window open to run commands on the remote box.
Or am I missing something?
Set a source control system and use it. Then you can just make a commit after saving in your IDE, and on server you can have something happening on-commit.
This can trigger tests, build, mail any errors to you...
One solution might be to have some sort of polling app that checks the timestamp on the files to see if they have changed. If they have then get it to save and then compile. Kinda hackish this way but it would be workable.
I personally use XMing with PuTTY. I ssh using PuTTY while XMing is running. I can open up any editor (gvim, emacs, gedit, etc) and it will appear.
You will need to do some setup on PuTTY though:
Expand Connection
Expand SSH
Click on X11
Check the "Enable X11 Forwarding"
In the text field for display location, enter (without quotes): "localhost:0"
Save session and connect.

Resources