[
142
]
At this point, you can copy and paste a command you've used previously from this
list to run it again. In fact, there's an even easier way. Do you notice the number on
the left of each command? We can utilize that number to quickly recall a previously
used command. In my screenshot, item
563
is where I ran
sudo apt update
. If I
wanted to run that same command again, I can simply enter the following command:
!563
In this case, I typed just four characters, and I was able to recall the previously used
command, which performs the same action as typing this:
sudo apt update
That saves a lot of typing, which is great because we administrators want to type as
little as possible (unless we're writing a book).
Let's look at a few additional history commands we can use. First, if we want to
delete something from the history, we can simply do this:
history -d 563
In this example, we deleted item
563
from Bash's history. To delete a different history
entry, simple replace
563
with whatever the number is for the item we would want to
remove. You may be wondering why deleting something from the history would be
necessary. The answer to that is simple: sometimes we make mistakes. Perhaps we
mistyped something, and we don't want a junior administrator to look at the history
and rerun an invalid command. Worse, if we accidentally saved a password to the
history, it will be there for all to see. We would definitely want to remove that item
so that the password isn't saved in plain text in the history file. One very common
example of this is with MySQL or MariaDB. When you enter the MySQL or MariaDB
shell, you can use the
-p
option and type the password in one line. It would look
something like this:
mariadb -u root -pSuperSecretPassword
That command may appear useful, because in one command you'd be logged in to
your database server as
root
. However, this is one of my pet peeves—I really don't
like it when people run commands that include a password in the clear. Having
the
root
password in your shell's history is a HUGE security risk. This is just
one example of something you won't want in our Bash history, though. My main
goal here is to demonstrate that you should think about security when entering
commands. If you have a potentially sensitive item in your command history, you
should remove it. In fact, you can actually enter a command and not have it saved in
the history at all. Simply prefix the command with a space. If you do, it will not be
recorded in the history file. Go ahead, give it a try, and see for yourself.
Chapter 6
Do'stlaringiz bilan baham: |