Now,
what about if we wanted to connect from the Windows client to a Linux
device instead? So let's go over to our Linux machine and make the same
kind of adjustments to allow remoting to happen.
So here we are on the Linux machine, and the first thing we have to
do is ensure that a couple of things are installed, and I'm going to
go ahead and just run the installs.
The first is the openssh‑client, which needs to be installed.
It's obviously going to ask me for my password because I'm using sudo.
It's going to go ahead, and then I'm also going to change openssh to be
server, run the same process, and click Yes.
And this will go ahead and install the server components for
openssh, as well as the client pieces.
The next thing we have to do is actually edit a specific configuration file
that is loaded into a specific directory in the /etc directory, and it's
called ssh. Now, from here in the ssh directory,
there's an sshd_config file that I can then open up, and you'll see right now
it says it's unwriteable, but what I want to do is go down and show you some of
the information in here before we make a change.
So you can see we have addresses, logging information, authentication,
things like that that allow us to connect to this one.
So, for example, where it says PasswordAuthentication is yes,
we're obviosly going to change that because we do want to use passwords.
We're going to do public authentication using keys,
we're going to allow PowerShell as an option.
So there's lots of options available to us in here.
So let's kind of come out of that, and what we'll do is we'll do the same
command, but this time we'll say sudo so we can make it writable. And then what
we'll do is scroll down looking for the various options.
The first one here is going to be a PasswordAuthentication option.
So, well, first we can do public authentication key option.
I'm going to scroll a bit further down, look for PasswordAuthentication,
which is yes. And then what I want to do is add what's called
subsystem commands to allow PowerShell to actually be one of
the options that are utilized.
So you'll see here where it says Subsystem sftp, so I'm going to say
Subsystem, and then in here I'm going to say powershell, and then
I'll say /usr/bin/pwsh ‑sshs ‑NoLogo, and then we'll just enter that
so you've got a bit more space. Okay. So I can say exit and yes, and
I'll say that there.
Now, the next thing we have to do at this point is we can reboot the device, or
we can simply just restart the actual daemon service itself,
the sshd service.
Now there are multiple ways of doing this, but an easy option
is just to stay sudo systemctl restart ssh, and then that will
simply restart the ssh service.
Okay, so now that we have that configured,
what we should be able to do is now go across over to our Windows machine and
then make a connection coming back the other way. So let's flick to our
Windows machine. So here I am on the Windows 10 machine ready to connect to
Linux. So the first thing we'll do is create some session properties, and
we'll put these into a variable that we can then use when we do, for example,
like an invoke command.
So the first thing we're going to set here is the HostName. So the
HostName is effectively the IP address. So I have an IP address of
192.168.127.129 for the server. I'm then going to say UserName =
"trainer," and then I'm going to use a static option that's available
here called SSHTransport, which just takes a true or a false value, and
then I'm going to populate there.
What I can then do is say sessionprops, and you'll see I've got my values
now connected. Now, of course, the next thing to do here is to utilize that
within a connection. So I'm going to say Invoke‑Command @sessionprops, and
then we can use a ScriptBlock command, which effectively is just going to
run whatever PowerShell option that we initially set. So I'm going to say
Get‑Process. Now, you notice the first thing that happened here, it's asking
me for the password for the account. Now, if you haven't connected
previously,
you'll probably get asked for the fingerprint or to accept the key, and then
you can type in the password for that account. And then this will go ahead
and connect to that device and then return whatever the values are from the
command that you initially sent over.
Now, as you can see, it looks like it's working, but it's kind of just
sitting there, so let me just come out of that one.
So how do we troubleshoot this?
What kind of things need to be done?
Well,
the first thing that we have available to us is we
have an extra command in Windows,
so I'm going to say Enable‑SSHRemoting, which, notice doesn't come
through by default. So what we can do here is use Install‑Module ‑Name,
and then we're going to say Microsoft.PowerShell.RemotingTools, like so.
This is now going to go and check the gallery for a specific module
called Microsoft PowerShell Remoting Tools. If it's successful in finding
that, it will say yes, add the repository, and then try to find it.
What I can then do is once that's done, I can say Import‑Module,
so that makes it available. Once that's done,
you'll see the Enable‑SSHRemoting is available. And of course, I can say
verbose, so this will go through and it will say okay,
so hold on a minute, we've got something else that's not quite right, and
that's because we need to add the capabilities to the device for openssh. So
let's clear this. And it might look like we're going through a bunch of steps
just kind of in reverse order, but I wanted to kind of show that the way of
diagnosing. So I'm going to say Add‑WindowsCapability. I'm going to say
‑Online ‑Name, and this will be OpenSSH.Client, like so, and then there'll be
a version number, etc. Now, the actual way of doing it is we have to use
these, so there's normally 4 of them, and then we'll say 0.0.1.0. Now, if
you're not 100% sure of what that would be, I can come out of this, and we can
say Get‑WindowsCapability. I can say ‑Online. Then I can pipe that object out
and say Where‑Object Name ‑like, and then I can say OpenSSH*, like so, and
this will go and look for the capabilities.
And sure enough you can now see that format. So there's the client
with those funny squiggles, and then all the way through. So let me
go back here, and we'll say let me do the Add‑WindowsCapability,
Add‑WindowsCapability ‑Online ‑Name.
And then what you can do is just come here and say I'll copy that.
So I've got it just right. And then I can paste that here. This is going
to go ahead and add that. And then I can also run the same thing again,
this time just change the word Client to Server, and it will say Running.
This might take a little bit longer.
The client tends to be really quick.
The server bit takes a little bit longer.
Okay,
next thing to do, we'll say Start‑Service sshd, and then we can do a few
other bits and pieces where we could set it to be automatic,
we could do a whole list of other things. So now that we have that set,
and if we rerun the Enable‑SSHRemoting option with VERBOSE now, what you
should see is it's now starting to do the configuration for us, so I can
say yes, and then it will say, okay, you need to restart the sshd
service, so I'm going to go and change my start option to be
restart‑Service, and that will restart sshd. So, now we have all of the
pieces working,
we can now go ahead and initiate that connection to that specific machine.
So let's go back over here. We'll say session, and we'll do it this
way, just like we did before, we can say New‑PSSession ‑HostName
192.168.127.129 ‑UserName "trainer." Okay,
ask me for the password, and then this will then go ahead and try to
connect across to the other device. Now, if for whatever reason you have
issues here and the initial configuration didn't quite take, then if we
flick back to our Linux machine,
what we can do here is go in and search for our PowerShell, launch the
PowerShell 7 window that we had, and just like we did in the Windows
device, we can run the Install‑Module, and we can say
Microsoft.PowerShell.RemotingTools, like so. We can do the install of the
same module that we just did on the Windows device.
What we can then do is then run SSHRemoting, exactly the same command, and
this is now going to go through and obviously ask me for my password, and
it will then start to go through and say, yes, I'd like to make changes,
and then it will say do you wish to restart? Yeah, I can say sudo service
ssh restart. So now that we have that available and that went through and
made all of the changes that were needed based on maybe we missed
something,
maybe we didn't, so let's go back to the Windows machine and re‑initialize that
connection. So we're back here. Let's issue that new session option. Password
in, and then you'll see it's now created a session. So sometimes when we do the
configuration it doesn't always take,
sometimes we miss something, so there are ways for us to do that. I can
now say enter the PSSession that we created, which is here, and you'll
see that we now connected to the Linux machine. And from here, I can then
start to execute certain commands.
So, for example, I did Get‑Process, and you can see clearly xdg‑desktop‑portal,
things like that, sshd.
These are all Linux processes.
We can also obviously exit the PSSession as well. At any point, we
can view the session, and we can, of course, remove the session as
well. So that's how we can actually connect in from a Windows
machine to a Linux machine, utilizing SSH from both the clients.