And now for our demonstration where we're looking at sed, we're
going to see how we can automate our edits, and as I said, really
important when we're looking at a DevOps environment. We're going to
be using sed to allow password‑based authentication through our SSH
daemon, our SSH server.
So let's go through and find ourselves a command line. And now
look at this, glorious Ubuntu. Now on our Ubuntu system, we want
to be able to use this as an SSH server, and we want to use
password‑based authentication.
However,
in the Vagrant VirtualBox that we are using, password‑based
authentication via the SSH server is disabled.
We want to be able to enable that, and we can look at how we could do this.
First of all, we're going to elevate our privileges with sudo
and run our SSH daemon with the uppercase T.
This then shows me the current running configuration. Now, of course,
there's a lot of information, but we can filter on that.
If we use the pipe symbol,
the vertical bar, we can take the output of sshd ‑T and send it through to
grep. Grep is used to search for text, and we're looking, then for
passwordauthentication, so all lowercase in our running configuration. And we
can see that password‑based authentication to the SSH daemon, our SSH server,
is currently disabled. It's set to no.
Now if we want to take a look at this from our actual
configuration file rather than the running configuration, I
can say grep ‑i to ignore case.
I do know the correct case is actually mixed‑case
PasswordAuthentication. It makes it easier to read anyway. But if
we check for that inside of our configuration file,
the /etc/ssh/sshd_config, we can see that PasswordAuthentication is disabled
within the configuration, and it's this that we want to change.
Now it would be very easy just to edit the file with one of our
editors, but we also want to look at the stream editor and look
at how we can automate the process.
So if we use sudo and sed, our Linux stream editor, we can specify ‑E for
an enhanced regular expression that will allow us to use groups in our
regular expression, ‑i is allowing us to do an in‑place edit to edit the
file. I use a single quote, so this then passes everything through to sed
without Bash looking at any special characters, s is for substitute, and
then we have forward slash to start delimiting the text that we are
searching for.
I'll write it out longhand first. So we've got PasswordAuthentication
being set to no, and we want to substitute it with PasswordAuthentication
being set to yes. Now obviously, the writing out in longhand gives me the
probability with my typing of misspelling PasswordAuthentication at least
once. We can reduce the amount of typing by using groups. Groups in a
regular expression are created using our parentheses, so our round
brackets.
So if we put round brackets or parentheses around (PasswordAuthentication),
that becomes a group.
We can reference these groups by the number in which they appear.
This is the first and, in fact, only group, so we can
make reference to it as group 1 or \1.
So we've got then PasswordAuthentication as my first group.
We repeat that first group in our replacement text, so \1. We
then go through, of course, using escape and dot. We can get
back to that last file name.
Real, real benefit of these shortcuts now that we're able to
see. Hit the big Enter key, and we're going to replace the
text. And we can see that we don't get anything coming back
because it is an in‑place edit.
But if I go back now and search using grep, we're able to
see the edit has been made. And generally, when we make
changes to configuration files,
the corresponding service is there is a background service related
to that configuration would need to be restarted.
We can use systemctl, and restart, and the service name, so sshd. With that
restarted, when we go back now and check for our original runtime
configuration, we can see now passwordauthentication is well and truly set
to yes. And we're going to be able to use this when we remotely connect to this Ubuntu system for copying files.