How can we forward a port to QEMU which has a server on the port already running? - virtual-machine

I have a python server running at port 28009:
python3 -m http.server 28009
Now, after I run the server whenever I call QEMU to forward port 28009:-
qemu-system-aarch64 -nographic -machine virt,gic-version=max -m 512M -cpu max -smp 4 \
-netdev user,id=vnet,hostfwd=tcp::28009-:28009 -device virtio-net-pci,netdev=vnet \
-drive file=ubuntu-image.img,if=none,id=drive0,cache=writeback -device virtio-blk,drive=drive0,bootindex=0 \
-drive file=flash0.img,format=raw,if=pflash -drive file=flash1.img,format=raw,if=pflash
It throws up an error saying :-
qemu-system-aarch64: Could not set up host forwarding rule 'tcp::28009-:28009'
Similarly if I run the above mentioned QEMU script first - it boots alright. However, the python server now throws up an error:
Traceback (most recent call last):
File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.6/http/server.py", line 1211, in <module>
test(HandlerClass=handler_class, port=args.port, bind=args.bind)
File "/usr/lib/python3.6/http/server.py", line 1185, in test
with ServerClass(server_address, HandlerClass) as httpd:
File "/usr/lib/python3.6/socketserver.py", line 456, in __init__
self.server_bind()
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 470, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
How can I have the server running, and then QEMU is able to access this server? I cannot use a TUN connection as I need to have localhost of the host machine talking to QEMU and vice-versa

The hostfwd option is for forwarding connections from the outside world to a server which is running on the guest. "hostfwd=tcp::HOSTPORT-:GUESTPORT" says "QEMU should listen on the host on port HOSTPORT; whenever a connection arrives there, it should forward it to the guest's port GUESTPORT (which hopefully has a server listening there)".
You seem to be running a server on the host. You can't have more than one thing listening on a particular port on one machine, so either the python3 server program can listen on port 28009 and respond to connections there, or QEMU can listen on port 28009 to respond to connections there (forwarding them to the guest), but not both at once. Whichever is started second will complain that something's already using the port.
If you want to run a server on the host and connect to it from the guest, you don't need any QEMU options at all. QEMU's 'usermode' networking will allow guest programs to make connections outwards to any IP address (including the wider internet but also directly to the host), so if you are trying to run a client on the guest and a server on the host that should just work. You can tell the guest client to connect either to the host's real IP address or you can use the special 'gateway' IP address 10.0.2.2 which is how the host machine appears on the fake network that the guest sees.

Related

Spyder -- Connect to remote kernel via proxy

I'm trying to connect to a remote kernel in Spyder, however the machine on which it is running is not directly accessible. Rather, to connect to it I must go through a bastion host / jumpbox as follows:
ssh -i ~/.ssh/id_rsa -J me#jumpbox me#remote which logs me directly into remote, automatically sending the connection through jumpbox.
I have python -m spyder-kernels.console running on remote, where I want to do my computing, but no way to connect to it directly since it's only accessible from jumpbox. I've tried setting up my ssh config with a ProxyJump entry which works for logging into the machine through ssh in the command line, but it appears that Spyder ignores the config file when setting up the remote kernel connection,
Is there a way to connect to this remote kernel? It appears there's a way to do this with IPython and I know I can do it with Jupyter Notebook, but I'm wondering if I can do this in Spyder.
(Related: Connect spyder to a remote kernel via ssh tunnel)
I don't know if you're still looking for an answer to this, but for future people arriving here, and for my own reference:
Yes, you can. You have to create an ssh-tunnel and connect Spyder to the kernel via localhost. For you that would look something like this:
ssh -L 3336:me#jumpbox:22 me#remote
22 is for the port your ssh server at remote is listening to. This is usually 22, unless the moderator changed this. 3336 is the port at localhost to connect to, you can choose any number you like above 1024 (which are privileged ports).
Then proceed as explained in the Spyder docs, i.e., launch the spider kernel (in the environment you want) on remote
python -m spyder_kernels.console
copy the connection file (kernel-pid.json) file to your local computer:
scp -oProxyJump=me#jumpbox remote:/path/to/json-file/kernel-pid.json ~/Desktop
/path/to/json-file you have to change to the path to the connection file (which you can find by running jupyter --runtime-dir on remote in the same environment as the spyder-kernel is running) and kernel-pid.json of course to the real file name. ~/Desktop copies it to your Desktop-folder, you can change that to wherever you want.
Connect Spyder to the kernel via "Connect to existing kernel", point it to the connection file you just copied, check the This is a remote kernel (via SSH) box and enter localhost as the Hostname, and 3336 as the port (or whichever port you changed it to).
That should do it.
Note, that, as is the case for me, your jumpbox server may break your ssh connection over which you launched the Spyder kernel, which will cause your kernel to break. So you might want to use
python -m spyder_kernels.console &
to have it run in the background, or launch it in a screen session. However, note that you cannot shutdown a remote kernel with exit, and it will keep running (see here), so you have to kill it in a different way.

How to connect with sshd?

I need to connect my laptop (Mac OS) to my Virtual Box (Debian) via sshd and then test telnet in Debian. Problem is I can't seem to connect via sshd.
I get
sshd re-exec requires execution with an absolute path
when I run
sshd root#192.xxx.xx.xx
But if I use an absolute path as suggested I get this instead:
/usr/sbin/sshd root#192.xxx.xx.xxx
Extra argument root#192.xxx.xx.xxx
What is the problem and how does one connect via sshd?
sshd is the dæmon program that listens to SSH port, then let incoming connections in.
You want to use ssh (without "d") as a client to connect to a remote machine:
usr/sbin/ssh root#192.xxx.xx.xxx

Trying to get selinux to allow apache to run an executable that uses a port

I am trying to get apache to run a bash script which uses ffmpeg to take snapshots from a mp4 stream. I get an "Input/Output" error where ffmpeg is blocked from accessing port 80.
I've gotten apache to run ffmpeg, it just seems to get blocked on port access.
I assume its an selinux permission problem where ffmpeg needs special permissions to be able to access port 80 (or whatever port it is) when run by apache.
The script runs fine from command line, its just launching it remotely that dies.
Thanks for your help!
sudo semanage port -l | grep http_port
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
ls -Z /usr/bin/ffmpeg
-rwxr-xr-x. root root system_u:object_r:bin_t:s0 /usr/bin/ffmpeg
From var/log/audit/audit.log:
type=AVC msg=audit(1502245154.609:23912): avc: denied { name_connect } for pid=12043 comm="ffmpeg" dest=80 scontext=system_u:system_r:httpd_sys_script_t:s0 tcontext=system_u:object_r:http_port_t:s0 tclass=tcp_socket
type=SYSCALL msg=audit(1502245154.609:23912): arch=c000003e syscall=42 success=no exit=-13 a0=3 a1=1775f00 a2=10 a3=7ffd7a6af0d0 items=0 ppid=12041 pid=12043 auid=4294967295 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=4294967295 comm="ffmpeg" exe="/usr/bin/ffmpeg" subj=system_u:system_r:httpd_sys_script_t:s0 key=(null)
Running Red Hat Enterprise Linux 7.4
Solved using https://wiki.centos.org/HowTos/SELinux#head-faa96b3fdd922004cdb988c1989e56191c257c01
created policy specific to access requirements - Step 7 in the documentation

How to start IPython notebook remotely?

Following these instructions (Running a notebook server and Remote access to IPython Notebooks
) I proceed as follows:
On the remote server:
1) Setting NotebookApp.password()
In [1]: from IPython.lib import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
2) Create profile
user#remote_host$ ipython profile create
3) Edit ~/.ipython/profile_default/ipython_notebook_config.py
# Password to use for web authentication
c = get_config()
c.NotebookApp.password =
u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
4) Start notebook on port 8889
user#remote_host$ ipython notebook --no-browser --port=8889
and the notebook starts
[I 16:08:10.012 NotebookApp] Using MathJax from CDN:https://cdn.mathjax.org/mathjax/latest/MathJax.js
[W 16:08:10.131 NotebookApp] Terminals not available (error was No module named 'terminado')
[I 16:08:10.132 NotebookApp] Serving notebooks from local directory: /cluster/home/user
[I 16:08:10.132 NotebookApp] 0 active kernels
[I 16:08:10.132 NotebookApp] The IPython Notebook is running at: http://localhost:8889/
[I 16:08:10.132 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
On my local machine
5) SSH tunneling
user#local$ ssh -N -f -L localhost:8888:127.0.0.1:8889 username#remote_host
On the remote host (/etc/hosts) you find
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
6) Finally, I try to open localhost:8888 on my browser, and I get:
channel 2: open failed: connect failed: Connection refused
channel 2: open failed: connect failed: Connection refused
channel 2: open failed: connect failed: Connection refused
channel 2: open failed: connect failed: Connection refused
channel 2: open failed: connect failed: Connection refused
All these steps work on one server, but fail on another one.
I tried contacting the administrator and said the following:
I assume that you are using two separate SSH connections: one from
which you run ipython and one that you use to do port forwarding.
There is no guarantee that the two connections will land you on the
same login node. In the case where the two connections are on
different hosts, you will experience the exact failure you have
encountered. Therefore you should setup the port forwarding in the
connection that you use to run ipython.
How can I setup the port forwarding in the connection that I use to run ipython?
I tried using my ip address but it didn't work
$ ssh -N -f -L local_ip_address:8888:127.0.0.1:8889 user#remote_host
Finally this is how the problem was solved:
# Login to the server from your local workstation and in the same connection do the port forwarding.
user#local$ ssh -L 8888:localhost:8889 username#remote_host
user#remote_host$ ipython notebook --no-browser --port=8889
Just follow this instruction.
https://coderwall.com/p/ohk6cg/remote-access-to-ipython-notebooks-via-ssh

SSH Connection Being Refused When I'm Remote, but not Local (Port Forwarding Already Enabled)

I set up SSH on my Ubuntu server (running XMonad) and generated a key for my laptop that I used to connect to my home server with. I also went on my wireless router and forward port 22 for SSH use. I can SSH fine when I'm at home using the standard:
ssh user#ipaddress
However when I'm outside of my local network I get this error:
ssh: connect to host xxx.xx.xx.xxx port 22: Connection refused
Everything I read says I need to either a) check that my port 22 is forward (which it is) or b) check that sshd is actually running on my Ubuntu server (which it is).
Any ideas what is preventing my SSH from working when I'm remote?
Add the following line your ssh user config file if it doesn't exits You can create the config file as shown below.
vi ~/.ssh/config
Host *
ServerAliveInterval 300
Change the permission as below:
chmod 600 ~/.ssh/config
Restart the daemon. Hope this helps.
https://serverfault.com/a/371563/617303
For me this was the cause.
In your /etc/ssh/sshd_config or /etc/ssh_ssh_config check to make sure GSSAPI Auth is disabled (set to no).
GSSAPIAuthentication no
Then restart the service or machine.