Now if you're using the command visudo to make your edit,
then it's going to open up in the default editor.
Now this might be vi, it might be vim, it might be nano,
depending on the system that you are using.
If you want to use a particular editor,
then we can go through and make sure that our environment
variable editor is passed through with sudo.
By default,
the environment is reset and only certain elements
of the environment flow through.
We can then go through and set our editor variable.
Then as we go through and use visudo,
in this case we're going to be using nano rather than the default of vi.
We're going to move out to our command line now so we can see how this can work.
We'll see how we can use a different editor in two ways.
One way is to open up a new shell, sudo ‑i,
set our variable in the shell, and then make our edits.
Whereas if we want the environment variable to pass through,
we'll also show how we set that.
And hello, and a big warm welcome to the command line of my AlmaLinux 8 system.
We're going to take a look at how we can gain full
administrative rights using sudo ‑i,
so we give ourselves an interactive root shell.
We can do this so long as we're able to run all commands on
the system as an administrator using sudo.
Now using this, then, we have already got our full rights,
so I don't need to use sudo in front of my yum command.
If I wanted to install the text editor nano,
I can just run straight ahead and do a yum install nano.
But having installed nano,
it's not going to change any of the default settings with visudo.
So if I went through now and use visudo,
again I don't need to use sudo in front of it,
we'll just use the default file,
but it's going to open up in the default vi text editor.
Now if I wanted to use, let's say, nano as my text editor,
then we're going to see there's a little bit of a problem,
but it can pick up on your environment variable.
So if I export EDITOR, the variable name that I need to set is EDITOR,
and I can have this configured to use nano.
Going back now to look at my visudo command,
we're going to be able to see that it will open up using the nano text editor.
Again, I'm not going to make any changes,
but we can see then that it is working the way that I would want.
But if I tried this as a standard user,
even a user who had rights to use the sudo command,
we're going to find that our environment variable is not carried through.
We as administrators must allow which environment variables are allowed through.
We can do this by creating a configuration file.
Again, I'm just as a root at the moment, so I'm going to use visudo.
Specifying then the configuration file that I want to write to,
I'm going to make my own little drop‑in file,
so /etc/sudoers.d, and we can call it, let's say,
defaults because that's one of the defaults that I am changing.
With the editor open, making sure that I write in the correct case,
it has to be uppercase D for Defaults, and then env_keep,
we want to keep the EDITOR variable, but we're appending to an existing list,
so you use += "EDITOR".
I can go through and save my changes.
The same syntax check is going to be carried out,
so I know everything is correct.
Now with that glorious work complete,
I can use the exit command to return to the more comfortable vagrant account,
and I can export my same variable, creating the environment variable for EDITOR,
pointing through to nano.
With that done and dusted, we can clear out of my screen.
And then if I use sudo and visudo,
we can see now that I'm able to edit the sudoers
file using the editor of choice.
Now that might be nano in your case, it might be vim in your case,
but this way we know then our variable is going to be passed through.
This could be really important where you've got
administrators who are familiar with one type of editor
but maybe not familiar with the default,
and we can keep consistency across different systems.
If we check, though, the settings that have been made,
so sudo ‑l as the vagrant user, we can see the env_keep+=EDITOR, so we know this is being passed through.