< Return to Video

Lecture 5: Command-line Environment (2020)

  • 0:00 - 0:02
    Okay, can everyone hear me okay?
  • 0:04 - 0:06
    Okay, so welcome back.
  • 0:06 - 0:10
    I'm gonna address a couple of items
    in kind of the administratrivia.
  • 0:11 - 0:13
    With the end of the first week,
  • 0:13 - 0:16
    we sent an email, noticing you that
  • 0:17 - 0:20
    we have uploaded the videos for the first
    week, so you can now find them online.
  • 0:20 - 0:27
    They have all the screen recordings for the things
    that we were doing, so you can go back to them.
  • 0:27 - 0:31
    Look if you're were confused about if
    we did something quick and, again,
  • 0:31 - 0:38
    feel free to ask us any questions if anything in the
    lecture notes is not clear. We also sent you a
  • 0:38 - 0:42
    survey so you can give us feedback
    about what was not clear,
  • 0:42 - 0:46
    what items you would want a
    more thorough explanation or
  • 0:47 - 0:52
    just any other item, if you're finding
    the exercises too hard, too easy,
  • 0:52 - 0:55
    go into that URL and we'll really
  • 0:56 - 1:00
    appreciate getting that feedback, because
    that will make the course better
  • 1:00 - 1:04
    for the remaining lectures and for
    future iterations of the course.
  • 1:05 - 1:07
    With that out of the way
  • 1:07 - 1:11
    Oh, and we're gonna try to upload the
    videos in a more timely manner.
  • 1:11 - 1:16
    We don't want to kind of wait until the end of
    the week for that. So keep tuned for that.
  • 1:19 - 1:20
    That out of the way,
  • 1:20 - 1:21
    now I'm gonna
  • 1:21 - 1:25
    This lecture's called command-line
    environment and we're
  • 1:25 - 1:28
    going to cover a few different topics. So the
  • 1:29 - 1:31
    main topics we're gonna
  • 1:32 - 1:35
    cover, so you can keep track,
  • 1:35 - 1:36
    it's probably better here,
  • 1:36 - 1:38
    keep track of what I'm talking.
  • 1:38 - 1:42
    The first is gonna be job control.
  • 1:42 - 1:44
    The second one is going to be
  • 1:45 - 1:47
    terminal multiplexers.
  • 1:52 - 1:57
    Then I'm going to explain what dotfiles
    are and how to configure your shell.
  • 1:57 - 2:03
    And lastly, how to efficiently work with
    remote machines. So if things are not
  • 2:05 - 2:08
    fully clear, kind of keep the structure.
  • 2:08 - 2:12
    They all kind of interact in some
    way, of how you use your terminal,
  • 2:13 - 2:17
    but they are somewhat separate
    topics, so keep that in mind.
  • 2:18 - 2:24
    So let's go with job control. So far we have
    been using the shell in a very, kind of
  • 2:25 - 2:28
    mono-command way. Like, you
    execute a command and then
  • 2:28 - 2:32
    the command executes, then you get some output,
    and that's all about what you can do.
  • 2:32 - 2:37
    And if you want to run several
    things, it's not clear
  • 2:37 - 2:41
    how you will do it. Or if you want to stop
    the execution of a program, it's again,
  • 2:41 - 2:44
    like how do I know how to stop a program?
  • 2:45 - 2:48
    Let's showcase this with a command called sleep.
  • 2:48 - 2:50
    Sleep is a command that takes an argument,
  • 2:50 - 2:54
    and that argument is going to be an
    integer number, and it will sleep.
  • 2:54 - 2:58
    It will just kind of be there, on the
    background, for that many seconds.
  • 2:58 - 3:04
    So if we do something like sleep 20, this process
    is gonna be sleeping for 20 seconds.
  • 3:04 - 3:08
    But we don't want to wait 20 seconds
    for the command to complete.
  • 3:08 - 3:11
    So what we can do is type "Ctrl+C".
  • 3:11 - 3:13
    By typing "Ctrl+C"
  • 3:13 - 3:18
    We can see that, here, the terminal let us know,
  • 3:19 - 3:23
    and it's part of the syntax that we covered
    in the editors / Vim lecture,
  • 3:23 - 3:27
    that we typed "Ctrl+C" and it stopped
    the execution of the process.
  • 3:28 - 3:30
    What is actually going on here
  • 3:30 - 3:35
    is that this is using a UNIX communication
    mechanism called signals.
  • 3:35 - 3:37
    When we type "Ctrl+C",
  • 3:38 - 3:42
    what the terminal did for us,
    or the shell did for us,
  • 3:42 - 3:46
    is send a signal called SIGINT,
  • 3:46 - 3:51
    that stands for SIGnal INTerrupt, that
    tells the program to stop itself.
  • 3:52 - 3:58
    And there are many, many, many signals
    of this kind. If you do man signal,
  • 3:59 - 4:05
    and just go down a little bit,
    here you have a list of them.
  • 4:05 - 4:07
    They all have number identifiers,
  • 4:08 - 4:11
    they have kind of a short name
    and you can find a description.
  • 4:11 - 4:16
    So for example, the one I have just
    described is here, number 2, SIGINT.
  • 4:17 - 4:22
    This is the signal that a terminal will send to a
    program when it wants to interrupt its execution.
  • 4:23 - 4:26
    A few more to be familiar with
  • 4:26 - 4:29
    is SIGQUIT, this is
  • 4:29 - 4:34
    again, if you work from a terminal and you
    want to quit the execution of a program.
  • 4:34 - 4:38
    For most programs it will do the same thing,
  • 4:38 - 4:41
    but we're gonna showcase now a program
    which will be different,
  • 4:41 - 4:44
    and this is the signal that will be sent.
  • 4:45 - 4:49
    It can be confusing sometimes. Looking at
    these signals, for example, the SIGTERM is
  • 4:50 - 4:54
    for most cases equivalent to SIGINT and SIGQUIT
  • 4:54 - 4:58
    but it's just when it's not
    sent through a terminal.
  • 5:00 - 5:02
    A few more that we're gonna
  • 5:02 - 5:06
    cover is SIGHUP, it's when there's
    like a hang-up in the terminal.
  • 5:06 - 5:10
    So for example, when you are in your
    terminal, if you close your terminal
  • 5:10 - 5:13
    and there are still things
    running in the terminal,
  • 5:13 - 5:17
    that's the signal that the program is gonna send
  • 5:17 - 5:20
    to all the processes to tell
    that they should close,
  • 5:20 - 5:25
    like there was a hang-up in the
    command line communication
  • 5:25 - 5:27
    and they should close now.
  • 5:28 - 5:34
    Signals can do more things than just stopping, interrupting
    programs and asking them to finish.
  • 5:34 - 5:37
    You can for example use the
  • 5:38 - 5:44
    SIGSTOP to pause the execution of the
    program, and then you can use the
  • 5:44 - 5:50
    SIGCONT command for continuing, to continue the execution
    of the program at a point later in time.
  • 5:51 - 5:55
    Since all of this might be slightly too
    abstract, let's see a few examples.
  • 5:58 - 6:01
    First, let's showcase a
  • 6:02 - 6:06
    Python program. I'm going to very
    quickly go through the program.
  • 6:06 - 6:08
    This is a Python program,
  • 6:09 - 6:11
    that like most python programs,
  • 6:12 - 6:14
    is importing this signal library and
  • 6:15 - 6:20
    is defining this handler here.
    And this handler is writing,
  • 6:20 - 6:23
    "Oh, I got a SIGINT, but
    I'm not gonna stop here".
  • 6:23 - 6:25
    And after that,
  • 6:25 - 6:31
    we tell Python that we want this program,
    when it gets a SIGINT, to stop.
  • 6:31 - 6:35
    The rest of the program is a very silly program
    that is just going to be printing numbers.
  • 6:35 - 6:38
    So let's see this in action.
  • 6:38 - 6:40
    We do Python SIGINT.
  • 6:40 - 6:45
    And it's counting. We try doing
    "Ctrl+C", this sends a SIGINT,
  • 6:45 - 6:50
    but the program didn't actually stop. This
    is because we have a way in the program of
  • 6:50 - 6:55
    dealing with this exception,
    and we didn't want to exit.
  • 6:55 - 6:58
    If we send a SIGQUIT, which is done through
  • 6:58 - 7:04
    "Ctrl+\", here, we can see that since the program
    doesn't have a way of dealing with SIGQUIT,
  • 7:04 - 7:06
    it does the default operation, which is
  • 7:07 - 7:09
    terminate the program.
  • 7:09 - 7:11
    And you could use this, for example,
  • 7:12 - 7:16
    if someone Ctrl+C's your program, and your
    program is supposed to do something,
  • 7:16 - 7:19
    like you maybe want to save the intermediate
    state of your program
  • 7:19 - 7:22
    to a file, so you can recover it for later.
  • 7:22 - 7:26
    This is how you could write a handler like this.
  • 7:30 - 7:31
    Can you repeat the question?
  • 7:31 - 7:32
    What did you type right now, when it stopped?
  • 7:32 - 7:34
    So I...
  • 7:35 - 7:39
    So what I typed is, I type
    "Ctrl+C" to try to stop it
  • 7:39 - 7:43
    but it didn't, because SIGINT is captured
    by the program. Then I type
  • 7:43 - 7:48
    "Ctrl+\", which sends a SIGQUIT,
    which is a different signal,
  • 7:49 - 7:52
    and this signal is not captured by the program.
  • 7:52 - 7:55
    It's also worth mentioning
    that there is a couple of
  • 7:55 - 8:00
    signals that cannot be captured by software.
    There is a couple of signals
  • 8:01 - 8:03
    like SIGKILL
  • 8:04 - 8:07
    that cannot be captured. Like that, it will
  • 8:07 - 8:09
    terminate the execution of the
    process, no matter what.
  • 8:09 - 8:12
    And it can be sometimes harmful.
    You do not want to be using it by
  • 8:12 - 8:16
    default, because this can leave for example an
    orphan child, orphaned children processes.
  • 8:16 - 8:21
    Like if a process has other small children
    processes that it started, and you
  • 8:21 - 8:25
    SIGKILL it, all of those will
    keep running in there,
  • 8:26 - 8:31
    but they won't have a parent, and you can maybe
    have a really weird behavior going on.
  • 8:32 - 8:36
    What signal is given to the
    program if we log off?
  • 8:36 - 8:37
    If you log off?
  • 8:38 - 8:42
    That would be... so for example, if you're in an
    SSH connection and you close the connection,
  • 8:42 - 8:46
    that is the hang-up signal,
  • 8:46 - 8:51
    SIGHUP, which I'm gonna cover in an example.
    So this is what would be sent up.
  • 8:52 - 8:56
    And you could write for example, if you want
    the process to keep working even if you close
  • 8:57 - 9:03
    that, you can write a wrapper around
    that to ignore that signal.
  • 9:05 - 9:10
    Let's display what we could do
    with the stop and continue.
  • 9:10 - 9:16
    So, for example, we can start a really long process.
    Let's sleep a thousand, we're gonna take forever.
  • 9:17 - 9:19
    We can control-c,
  • 9:19 - 9:20
    "Ctrl+Z", sorry,
  • 9:20 - 9:25
    and if we do "Ctrl+Z" we can see that the
    terminal is saying "it's suspended".
  • 9:25 - 9:32
    What this actually meant is that this process
    was sent a SIGSTOP signal and now is
  • 9:32 - 9:37
    still there, you could continue its execution, but right
    now it's completely stopped and in the background
  • 9:39 - 9:42
    and we can launch a different program.
  • 9:42 - 9:44
    When we try to run this program,
  • 9:44 - 9:47
    please notice that I have included
    an "&" at the end.
  • 9:47 - 9:52
    This tells bash that I want this program
    to start running in the background.
  • 9:53 - 9:56
    This is kind of related to all these
  • 9:56 - 10:00
    concepts of running programs in
    the shell, but backgrounded.
  • 10:00 - 10:04
    And what is gonna happen is
    the program is gonna start
  • 10:05 - 10:08
    but it's not gonna take over my prompt.
  • 10:08 - 10:12
    If I just ran this command without
    this, I could not do anything.
  • 10:12 - 10:16
    I would have no access to the prompt
    until the command either finished
  • 10:16 - 10:19
    or I ended it abruptly. But if I do this,
  • 10:20 - 10:23
    it's saying "there's a new
    process which is this".
  • 10:23 - 10:25
    This is the process identifying number,
  • 10:25 - 10:27
    we can ignore this for now.
  • 10:28 - 10:33
    If I type the command "jobs", I get the
    output that I have a suspended job
  • 10:33 - 10:36
    that is the "sleep 1000" job.
  • 10:36 - 10:38
    And then I have another running job,
  • 10:38 - 10:42
    which is this "NOHUP sleep 2000".
  • 10:43 - 10:46
    Say I want to continue the first job.
  • 10:46 - 10:49
    The first job is suspended,
    it's not executing anymore.
  • 10:49 - 10:53
    I can continue that doing "BG %1"
  • 10:54 - 10:58
    That "%" is referring to the fact that
    I want to refer to this specific
  • 11:00 - 11:04
    process. And now, if I do that
    and I look at the jobs,
  • 11:04 - 11:06
    now this job is running again. Now
  • 11:06 - 11:09
    both of them are running.
  • 11:09 - 11:14
    If I wanted to stop these all,
    I can use the kill command.
  • 11:14 - 11:16
    The kill command
  • 11:16 - 11:19
    is for killing jobs,
  • 11:19 - 11:22
    which is just stopping them, intuitively,
  • 11:22 - 11:24
    but actually it's really useful.
  • 11:24 - 11:28
    The kill command just allows you
    to send any sort of Unix signal.
  • 11:28 - 11:32
    So here for example, instead
    of killing it completely,
  • 11:32 - 11:35
    we just send a stop signal.
  • 11:35 - 11:39
    Here I'm gonna send a stop signal, which
    is gonna pause the process again.
  • 11:39 - 11:41
    I still have to include the identifier,
  • 11:42 - 11:46
    because without the identifier the shell wouldn't know
    whether to stop the first one or the second one.
  • 11:47 - 11:52
    Now it's said this has been suspended,
    because there was a signal sent.
  • 11:53 - 11:57
    If I do "jobs", again, we can see
    that the second one is running
  • 11:57 - 12:01
    and the first one has been stopped.
  • 12:01 - 12:04
    Going back to one of the questions,
  • 12:04 - 12:07
    what happens when you close
    the cell, for example,
  • 12:07 - 12:13
    and why sometimes people will say that
    you should use this NOHUP command
  • 12:13 - 12:16
    before your run jobs in a remote session.
  • 12:16 - 12:23
    This is because if we try to send
    a hung up command to the first job
  • 12:24 - 12:28
    it's gonna, in a similar fashion
    as the other signals,
  • 12:28 - 12:32
    it's gonna hang it up and that's
    gonna terminate the job.
  • 12:33 - 12:36
    And the first job isn't there anymore
  • 12:36 - 12:39
    whereas we have still the second job running.
  • 12:39 - 12:43
    However, if we try to send the
    signal to the second job
  • 12:43 - 12:46
    what will happen if we close
    our terminal right now
  • 12:47 - 12:49
    is it's still running.
  • 12:49 - 12:52
    Like NOHUP, what it's doing
    is kind of encapsulating
  • 12:52 - 12:54
    whatever command you're executing and
  • 12:55 - 12:59
    ignoring wherever you get a hang up signal,
  • 12:59 - 13:04
    and just ignoring that so it can keep running.
  • 13:05 - 13:08
    And if we send the "kill"
    signal to the second job,
  • 13:08 - 13:13
    that one can't be ignored and that
    will kill the job, no matter what.
  • 13:13 - 13:16
    And we don't have any jobs anymore.
  • 13:17 - 13:23
    That kind of completes the
    section on job control.
  • 13:23 - 13:27
    Any questions so far? Anything
    that wasn't fully clear?
  • 13:29 - 13:30
    What does BG do?
  • 13:31 - 13:32
    So BG...
  • 13:32 - 13:37
    There are like two commands. Whenever you
    have a command that has been backgrounded
  • 13:37 - 13:42
    and is stopped you can use
    BG (short for background)
  • 13:42 - 13:44
    to continue that process running
    on the background.
  • 13:44 - 13:47
    That's equivalent of just kind of sending it
  • 13:48 - 13:51
    a continue signal, so it keeps running.
  • 13:51 - 13:55
    And then there's another one which
    is called FG, if you want to
  • 13:55 - 14:00
    recover it to the foreground and you want
    to reattach your standard output.
  • 14:05 - 14:07
    Okay, good.
  • 14:07 - 14:11
    Jobs are useful and in general, I
    think knowing about signals can be
  • 14:11 - 14:14
    really beneficial when dealing
    with some part of Unix
  • 14:14 - 14:19
    but most of the time what you actually want
    to do is something along the lines of
  • 14:20 - 14:24
    having your editor in one side and then
    the program in another, and maybe
  • 14:25 - 14:28
    monitoring what the resource
    consumption is in our tab.
  • 14:29 - 14:34
    We could achieve this using probably
    what you have seen a lot of the time,
  • 14:34 - 14:35
    which is just opening more windows.
  • 14:35 - 14:37
    We can keep opening terminal windows.
  • 14:37 - 14:41
    But the fact is there are kind of more
    convenient solutions to this and
  • 14:41 - 14:44
    this is what a terminal multiplexer does.
  • 14:44 - 14:49
    A terminal multiplexer like tmux
  • 14:49 - 14:52
    will let you create different workspaces
    that you can work in,
  • 14:53 - 14:54
    and quickly kind of,
  • 14:54 - 14:57
    this has a huge variety of functionality,
  • 14:57 - 15:03
    It will let you rearrange the environment and
    it will let you have different sessions.
  • 15:03 - 15:05
    There's another more...
  • 15:06 - 15:08
    older command, which is called "screen",
  • 15:08 - 15:09
    that might be more readily available.
  • 15:09 - 15:12
    But I think the concept kind
    of extrapolates to both.
  • 15:13 - 15:15
    We recommend tmux, that you go and learn it.
  • 15:15 - 15:17
    And in fact, we have exercises on it.
  • 15:17 - 15:20
    I'm gonna showcase a different
    scenario right now.
  • 15:20 - 15:22
    So whenever I talked...
  • 15:22 - 15:25
    Oh, let me make a quick note.
  • 15:25 - 15:29
    There are kind of three core concepts
    in tmux, that I'm gonna go through and
  • 15:30 - 15:33
    the main idea is that there are what is called
  • 15:35 - 15:37
    "sessions".
  • 15:38 - 15:41
    Sessions have "windows" and
  • 15:42 - 15:44
    windows have "panes".
  • 15:46 - 15:50
    It's gonna be kind of useful to
    keep this hierarchy in mind.
  • 15:51 - 15:57
    You can pretty much equate "windows" to what
    "tabs" are in other editors and others,
  • 15:57 - 16:01
    like for example your web browser.
  • 16:01 - 16:06
    I'm gonna go through the features, mainly
    what you can do at the different levels.
  • 16:07 - 16:10
    So first, when we do tmux, that starts a session.
  • 16:11 - 16:15
    And here right now it seems like nothing changed
  • 16:15 - 16:20
    but what's happening right now is we're within a shell
    that is different from the one we started before.
  • 16:21 - 16:25
    So in our shell we started
    a process, that is tmux
  • 16:25 - 16:29
    and that tmux started a different process,
    which is the shell we're currently in.
  • 16:29 - 16:30
    And the nice thing about this is that
  • 16:31 - 16:35
    that tmux process is separate from
    the original shell process.
  • 16:35 - 16:37
    So
  • 16:41 - 16:44
    here, we can do things.
  • 16:44 - 16:49
    We can do "ls -la", for example, to
    tell us what is going on in here.
  • 16:49 - 16:54
    And then we can start running our program,
    and it will start running in there
  • 16:54 - 16:58
    and we can do "Ctrl+A d", for example, to detach
  • 17:13 - 17:16
    to detach from the session.
  • 17:16 - 17:19
    And if we do "tmux a"
  • 17:19 - 17:22
    that's gonna reattach us to the session.
  • 17:22 - 17:22
    So the process,
  • 17:22 - 17:25
    we abandon the process counting numbers.
  • 17:25 - 17:28
    This really silly Python program
    that was just counting numbers,
  • 17:28 - 17:30
    we left it running there.
  • 17:30 - 17:32
    And if we tmux...
  • 17:32 - 17:34
    Hey, the process is still running there.
  • 17:34 - 17:38
    And we could close this entire
    terminal and open a new one and
  • 17:38 - 17:42
    we could still reattach because this
    tmux session is still running.
  • 17:43 - 17:45
    Again, we can...
  • 17:47 - 17:49
    Before I go any further.
  • 17:49 - 17:54
    Pretty much... Unlike Vim, where
    you have this notion of modes,
  • 17:54 - 17:58
    tmux will work in a more emacsy way, which is
  • 17:58 - 18:04
    every command, pretty much every command in tmux,
  • 18:04 - 18:06
    you could enter it through the...
  • 18:06 - 18:08
    it has a command line, that we could use.
  • 18:08 - 18:11
    But I recommend you to get familiar
    with the key bindings.
  • 18:12 - 18:15
    It can be somehow non intuitive at first,
  • 18:15 - 18:18
    but once you get used to them...
  • 18:22 - 18:23
    "Ctrl+C", yeah
  • 18:24 - 18:31
    When you get familiar with them, you will be much faster
    just using the key bindings than using the commands.
  • 18:31 - 18:36
    One note about the key bindings: all the
    key bindings have a form that is like
  • 18:36 - 18:40
    you type a prefix and then some key.
  • 18:40 - 18:44
    So for example, to detach we
    do "Ctrl+A" and then "D".
  • 18:44 - 18:50
    This means you press "Ctrl+A" first, you release
    that, and then press "D" to detach.
  • 18:50 - 18:54
    On default tmux, the prefix is "Ctrl+B",
  • 18:54 - 18:59
    but you will find that most people
    will have this remapped to "Ctrl+A"
  • 18:59 - 19:03
    because it's a much more ergonomic
    type on the keyboard.
  • 19:03 - 19:06
    You can find more about how to do these
    things in one of the exercises,
  • 19:07 - 19:13
    where we link you to the basics and how to do some
    kind of quality of life modifications to tmux.
  • 19:13 - 19:17
    Going back to the concept of sessions,
  • 19:17 - 19:22
    we can create a new session just
    doing something like tmux new
  • 19:22 - 19:25
    and we can give sessions names.
  • 19:25 - 19:27
    So we can do like "tmux new -t foobar"
  • 19:27 - 19:31
    and this is a completely different
    session, that we have started.
  • 19:32 - 19:36
    We can work here, we can detach from it.
  • 19:36 - 19:40
    "tmux ls" will tell us that we
    have two different sessions:
  • 19:40 - 19:43
    the first one is named "0", because
    I didn't give it a name,
  • 19:44 - 19:46
    and the second one is called "foobar".
  • 19:47 - 19:51
    I can attach the foobar session
  • 19:51 - 19:54
    and I can end it.
  • 19:55 - 19:56
    And it's really nice because
  • 19:56 - 20:00
    having this you can kind of work
    in completely different projects.
  • 20:00 - 20:04
    For example, having two different
    tmux sessions and different
  • 20:04 - 20:08
    editor sessions, different processes running...
  • 20:10 - 20:15
    When you are within a session, we
    start with the concept of windows.
  • 20:15 - 20:21
    Here we have a single window, but we
    can use "Ctrl+A c" (for "create")
  • 20:21 - 20:24
    to open a new window.
  • 20:24 - 20:26
    And here nothing is executing.
  • 20:26 - 20:29
    What it's doing is, tmux has
    opened a new shell for us
  • 20:30 - 20:35
    and we can start running another
    one of these programs here.
  • 20:35 - 20:42
    And to quickly jump between the tabs,
    we can do "Ctrl+A" and "previous",
  • 20:42 - 20:45
    "p" for "previous",
  • 20:45 - 20:48
    and that will go up to the previous window.
  • 20:48 - 20:51
    "Ctrl+A" "next", to go to the next window.
  • 20:51 - 20:56
    You can also use the numbers. So if we
    start opening a lot of these tabs,
  • 20:56 - 21:00
    we could use "Ctrl+A 1", to
    specifically jump to the
  • 21:00 - 21:04
    to the window that is number "1".
  • 21:05 - 21:09
    And, lastly, it's also pretty
    useful to know sometimes
  • 21:09 - 21:10
    that you can rename them.
  • 21:10 - 21:13
    For example here I'm executing
    this Python process,
  • 21:14 - 21:17
    but that might not be really
    informative and I want...
  • 21:17 - 21:21
    I maybe want to have something like
    execution or something like that and
  • 21:22 - 21:27
    that will rename the name of that window so
    you can have this really neatly organized.
  • 21:27 - 21:34
    This still doesn't solve the need when you want to
    have two things at the same time in your terminal,
  • 21:34 - 21:36
    like in the same display.
  • 21:36 - 21:38
    This is what panes are for. Right now, here
  • 21:38 - 21:40
    we have a window with a single pane
  • 21:40 - 21:44
    (all the windows that we have opened
    so far have a single pane).
  • 21:44 - 21:51
    But if we do 'Ctrl+A "'
  • 21:51 - 21:57
    this will split the current display
    into two different panes.
  • 21:57 - 22:01
    So, you see, the one we open below is a different
    shell from the one we have above,
  • 22:02 - 22:05
    and we can run any process that we want here.
  • 22:06 - 22:10
    We can keep splitting this, if we do "Ctrl+A %"
  • 22:10 - 22:15
    that will split vertically. And you can kind of
  • 22:15 - 22:18
    rearrange these tabs using a
    lot of different commands.
  • 22:18 - 22:23
    One that I find very useful, when you are
    starting and it's kind of frustrating,
  • 22:24 - 22:26
    rearranging them.
  • 22:26 - 22:30
    Before I explain that, to move
    through these panes, which is
  • 22:30 - 22:32
    something you want to be doing all the time
  • 22:32 - 22:37
    You just do "Ctrl+A" and the arrow
    keys, and that will let you quickly
  • 22:37 - 22:44
    navigate through the different
    windows, and execute again...
  • 22:44 - 22:46
    I'm doing a lot of "ls -a"
  • 22:47 - 22:53
    I can do "HTOP", that we'll explain in
    the debugging and profiling lecture.
  • 22:54 - 22:56
    And we can just navigate through them, again
  • 22:56 - 22:59
    like to rearrange there's
    another slew of commands,
  • 22:59 - 23:01
    you will go through some in the Exercises
  • 23:02 - 23:07
    "Ctrl+A" space is pretty neat, because it
    will kind of equispace the current ones
  • 23:07 - 23:10
    and let you through different layouts.
  • 23:11 - 23:14
    Some of them are too small for my current
  • 23:15 - 23:19
    terminal config, but that covers,
    I think, most of it.
  • 23:19 - 23:21
    Oh, there's also,
  • 23:23 - 23:29
    here, for example, this Vim execution
    that we have started,
  • 23:29 - 23:33
    is too small for what the current tmux pane is.
  • 23:34 - 23:38
    So one of the things that really is
    much more convenient to do in tmux,
  • 23:39 - 23:42
    in contrast to having multiple
    terminal windows, is that
  • 23:43 - 23:48
    you can zoom into this, you can ask
    by doing "Ctrl+A z", for "zoom".
  • 23:48 - 23:53
    It will expand the pane to
    take over all the space,
  • 23:53 - 23:57
    and then "Ctrl+A z", again will go back to it.
  • 24:03 - 24:08
    Any questions for terminal multiplexers,
    or like, tmux concretely?
  • 24:14 - 24:17
    Is it running all the same thing?
  • 24:19 - 24:23
    Like, is there any difference in execution
    between running it in different windows?
  • 24:25 - 24:29
    Is it really just doing it all the
    same, so that you can see it?
  • 24:29 - 24:35
    Yeah, it wouldn't be any different from having
    two terminal windows open in your computer.
  • 24:35 - 24:39
    Like both of them are gonna be running.
    Of course, when it gets to the CPU,
  • 24:39 - 24:41
    this is gonna be multiplexed again.
  • 24:41 - 24:44
    Like there's like a timesharing
    mechanism going there
  • 24:44 - 24:46
    but there's no difference.
  • 24:46 - 24:52
    tmux is just making this much more convenient
    to use by giving you this visual layout
  • 24:53 - 24:55
    that you can quickly manipulate through.
  • 24:55 - 25:00
    And one of the main advantages will come
    when we reach the remote machines
  • 25:00 - 25:05
    because you can leave one of these, we can
    detach from one of these tmux systems,
  • 25:05 - 25:09
    close the connection and even
    if we close the connection and
  • 25:09 - 25:12
    and the terminal is gonna send a hang-up signal,
  • 25:12 - 25:15
    that's not gonna close all the
    tmux's that have been started.
  • 25:17 - 25:19
    Any other questions?
  • 25:24 - 25:28
    Let me disable the key-caster.
  • 25:34 - 25:38
    So now we're gonna move into the topic
    of dotfiles and, in general,
  • 25:38 - 25:42
    how to kind of configure your shell
    to do the things you want to do
  • 25:42 - 25:46
    and mainly how to do them quicker
    and in a more convenient way.
  • 25:46 - 25:49
    I'm gonna motivate this using aliases first.
  • 25:49 - 25:51
    So what an alias is,
  • 25:51 - 25:54
    is that by now, you might be
    starting to do something like
  • 25:55 - 26:02
    a lot of the time, I just want to LS a directory and
    I want to display all the contents into a list format
  • 26:02 - 26:05
    and in a human readable thing.
  • 26:05 - 26:07
    And it's fine. Like it's not
    that long of a command.
  • 26:07 - 26:10
    But as you start building longer
    and longer commands,
  • 26:10 - 26:14
    it can become kind of bothersome having
    to retype them again and again.
  • 26:14 - 26:18
    This is one of the reasons
    why aliases are useful.
  • 26:18 - 26:22
    Alias is a command that will
    be a built-in in your shell,
  • 26:22 - 26:24
    and what it will do is
  • 26:24 - 26:28
    it will remap a short sequence of
    characters to a longer sequence.
  • 26:28 - 26:32
    So if I do, for example, here
  • 26:32 - 26:37
    alias ll="ls -lah"
  • 26:37 - 26:43
    If I execute this command, this is gonna call
    the "alias" command with this argument
  • 26:43 - 26:44
    and the LS is going to update
  • 26:45 - 26:49
    the environment in my shell
    to be aware of this mapping.
  • 26:49 - 26:53
    So if I now do LL,
  • 26:53 - 26:58
    it's executing that command without me
    having to type the entire command.
  • 26:58 - 27:01
    It can be really handy for many, many reasons.
  • 27:01 - 27:05
    One thing to note before I go any further is that
  • 27:05 - 27:10
    here, alias is not anything special
    compared to other commands,
  • 27:10 - 27:11
    it's just taking a single argument.
  • 27:12 - 27:16
    And there is no space around
    this equals and that's
  • 27:16 - 27:19
    because alias takes a single argument
  • 27:19 - 27:22
    and if you try doing
  • 27:22 - 27:25
    something like this, that's giving
    it more than one argument
  • 27:25 - 27:28
    and that's not gonna work because
    that's not the format it expects.
  • 27:30 - 27:34
    So other use cases that work for aliases,
  • 27:35 - 27:37
    as I was saying,
  • 27:37 - 27:40
    for some things it may be much more convenient,
  • 27:40 - 27:41
    like
  • 27:41 - 27:43
    one of my favorites is git status.
  • 27:43 - 27:48
    It's extremely long, and I don't like typing
    that long of a command every so often,
  • 27:48 - 27:49
    because you end up taking a lot of time.
  • 27:49 - 27:53
    So GS will replace for doing the git status
  • 27:54 - 27:59
    You can also use them to alias
    things that you mistype often,
  • 27:59 - 28:01
    so you can do "sl=ls",
  • 28:01 - 28:03
    that will work.
  • 28:06 - 28:11
    Other useful mappings are,
  • 28:11 - 28:15
    you might want to alias a command to itself
  • 28:16 - 28:18
    but with a default flag.
  • 28:18 - 28:21
    So here what is going on is I'm creating an alias
  • 28:21 - 28:23
    which is an alias for the move command,
  • 28:23 - 28:30
    which is MV and I'm aliasing it to the
    same command but adding the "-i" flag.
  • 28:30 - 28:34
    And this "-i" flag, if you go through the man page
    and look at it, it stands for "interactive".
  • 28:35 - 28:40
    And what it will do is it will prompt
    me before I do an overwrite.
  • 28:40 - 28:44
    So once I have executed this,
    I can do something like
  • 28:45 - 28:47
    I want to move "aliases" into "case".
  • 28:48 - 28:53
    By default "move" won't ask, and if "case"
    already exists, it will be over.
  • 28:53 - 28:56
    That's fine, I'm going to overwrite
    whatever that's there.
  • 28:56 - 28:59
    But here it's now expanded,
  • 28:59 - 29:02
    "move" has been expanded into this "move -i"
  • 29:02 - 29:04
    and it's using that to ask me
  • 29:04 - 29:07
    "Oh, are you sure you want to overwrite this?"
  • 29:08 - 29:12
    And I can say no, I don't want to lose that file.
  • 29:12 - 29:16
    Lastly, you can use "alias move"
  • 29:16 - 29:19
    to ask for what this alias stands for.
  • 29:19 - 29:22
    So it will tell you so you can quickly make sure
  • 29:22 - 29:25
    what the command that you
    are executing actually is.
  • 29:27 - 29:31
    One inconvenient part about, for example,
    having aliases is how will you go about
  • 29:32 - 29:35
    persisting them into your current environment?
  • 29:36 - 29:38
    Like, if I were to close this terminal now,
  • 29:38 - 29:40
    all these aliases will go away.
  • 29:40 - 29:43
    And you don't want to be kind
    of retyping these commands
  • 29:43 - 29:47
    and more generally, if you start configuring
    your shell more and more,
  • 29:47 - 29:51
    you want some way of bootstrapping
    all this configuration.
  • 29:51 - 29:57
    You will find that most shell command programs
  • 29:57 - 30:01
    will use some sort of text
    based configuration file.
  • 30:01 - 30:07
    And this is what we usually call "dotfiles", because
    they start with a dot for historical reasons.
  • 30:07 - 30:13
    So for bash in our case, which is a shell,
  • 30:13 - 30:16
    we can look at the bashrc.
  • 30:16 - 30:20
    For demonstration purposes,
    here I have been using ZSH,
  • 30:20 - 30:24
    which is a different shell, and I'm going
    to be configuring bash, and starting bash.
  • 30:25 - 30:30
    So if I create an entry here and I say
  • 30:30 - 30:32
    SL maps to LS
  • 30:33 - 30:36
    And I have modified that, and now I start bash.
  • 30:37 - 30:41
    Bash is kind of completely unconfigured,
    but now if I do SL...
  • 30:41 - 30:44
    Hm, that's unexpected.
  • 30:46 - 30:48
    Oh, good. Good getting that.
  • 30:48 - 30:52
    So it matters where you config file is,
  • 30:52 - 30:55
    your config file needs to be in your home folder.
  • 30:56 - 31:01
    So your configuration file for
    bash will live in that "~",
  • 31:01 - 31:04
    which will expand to your home directory,
  • 31:04 - 31:06
    and then bashrc.
  • 31:06 - 31:09
    And here we can create the alias
  • 31:12 - 31:16
    and now we start a bash session and we do SL.
  • 31:16 - 31:22
    Now it has been loaded, and this is
    loaded at the beginning when this
  • 31:22 - 31:24
    bash program is started.
  • 31:25 - 31:31
    All this configuration is loaded and you can, not only use
    aliases, they can have a lot of parts of configuration.
  • 31:31 - 31:36
    So for example here, I have a prompt
    which is fairly useless.
  • 31:36 - 31:38
    It has just given me the name
    of the shell, which is bash,
  • 31:39 - 31:44
    and the version, which is 5.0. I don't
    want this to be displayed and
  • 31:44 - 31:49
    as with many things in your shell, this
    is just an environment variable.
  • 31:49 - 31:53
    So the "PS1" is just the prompt string
  • 31:54 - 31:55
    for your prompt and
  • 31:55 - 32:03
    we can actually modify this
    to just be a "> " symbol.
  • 32:03 - 32:08
    and now that has been modified, and we have
    that. But if we exit and call bash again,
  • 32:09 - 32:15
    that was lost. However, if we add this
    entry and say, oh we want "PS1"
  • 32:16 - 32:17
    to be
  • 32:17 - 32:19
    this and
  • 32:19 - 32:25
    we call bash again, this has been persisted.
    And we can keep modifying this configuration.
  • 32:25 - 32:27
    So maybe we want to include
  • 32:28 - 32:30
    where the
  • 32:30 - 32:33
    working directory that we are is in, and
  • 32:34 - 32:37
    that's telling us the same information
    that we had in the other shell.
  • 32:37 - 32:40
    And there are many, many options,
  • 32:41 - 32:45
    shells are highly, highly configurable, and
  • 32:46 - 32:50
    it's not only cells that are configured
    through these files,
  • 32:51 - 32:56
    there are many other programs. As we saw for
    example in the editors lecture, Vim is also
  • 32:56 - 33:03
    configured this way. We gave you this vimrc
    file and told you to put it under your
  • 33:03 - 33:06
    home/.vimrc
  • 33:06 - 33:12
    and this is the same concept, but just
    for Vim. It's just giving it a set of
  • 33:12 - 33:18
    instructions that it should load when it's started,
    so you can keep a configuration that you want.
  • 33:19 - 33:21
    And even non...
  • 33:22 - 33:27
    kind of a lot of programs will support this. For instance,
    my terminal emulator, which is another concept,
  • 33:27 - 33:30
    which is the program that is
  • 33:30 - 33:35
    running the shell, in a way, and displaying
    this into the screen in my computer.
  • 33:36 - 33:39
    It can also be configured this way, so
  • 33:40 - 33:44
    if I modify this I can
  • 33:47 - 33:53
    change the size of the font. Like right now, for
    example, I have increased the font size a lot
  • 33:53 - 33:56
    for demonstration purposes, but
  • 33:56 - 34:00
    if I change this entry and make it for example
  • 34:01 - 34:07
    28 and write this value, you see that
    the size of the font has changed,
  • 34:07 - 34:13
    because I edited this text file that specifies
    how my terminal emulator should work.
  • 34:19 - 34:21
    Any questions so far?
  • 34:21 - 34:22
    With dotfiles.
  • 34:28 - 34:36
    Okay, it can be a bit daunting knowing that there
    is like this endless wall of configurations,
  • 34:36 - 34:41
    and how do you go about learning
    about what can be configured?
  • 34:42 - 34:44
    The good news is that
  • 34:45 - 34:49
    we have linked you to really good
    resources in the lecture notes.
  • 34:49 - 34:56
    But the main idea is that a lot of people really like
    just configuring these tools and have uploaded
  • 34:57 - 35:01
    their configuration files to GitHub, another
    different kind of repositories online.
  • 35:01 - 35:03
    So for example, here we are on GitHub,
  • 35:03 - 35:07
    we search for dotfiles, and
    can see that there are like
  • 35:07 - 35:13
    thousands of repositories of people sharing
    their configuration files. We have also...
  • 35:13 - 35:15
    Like, the class instructors
    have linked our dotfiles.
  • 35:15 - 35:19
    So if you really want to know how
    any part of our setup is working
  • 35:19 - 35:22
    you can go through it and try to figure it out.
  • 35:22 - 35:24
    You can also feel free to ask us.
  • 35:24 - 35:27
    If we go for example to this repository here
  • 35:27 - 35:31
    we can see that there's many, many
    files that you can configure.
  • 35:31 - 35:38
    For example, there is one for bash, the first couple of ones
    are for git, that will be probably be covered in the
  • 35:39 - 35:41
    version control lecture tomorrow.
  • 35:41 - 35:48
    If we go for example to the bash profile, which is
    a different form of what we saw in the bashrc,
  • 35:49 - 35:53
    it can be really useful because
    you can learn through
  • 35:54 - 35:58
    just looking at the manual page, but the
    manual pages is, a lot of the time
  • 35:58 - 36:04
    just kind of like a descriptive explanation
    of all the different options
  • 36:04 - 36:05
    and sometimes it's more helpful
  • 36:05 - 36:10
    going through examples of what people have done
    and trying to understand why they did it
  • 36:10 - 36:12
    and how it's helping their workflow.
  • 36:13 - 36:17
    We can say here that this person has
    done case-insensitive globbing.
  • 36:17 - 36:21
    We covered globbing as this
    kind of filename expansion
  • 36:22 - 36:26
    trick in the shell scripting and tools.
  • 36:26 - 36:29
    And here you say no, I don't want this to matter,
  • 36:29 - 36:31
    whether using uppercase and lowercase,
  • 36:31 - 36:33
    and just setting this option in the shell
    for these things to work this way
  • 36:35 - 36:38
    Similarly, there is for example aliases.
  • 36:38 - 36:42
    Here you can see a lot of aliases that this
    person is doing. For example, "d" for
  • 36:44 - 36:47
    "d" for "Dropbox", sorry, because
    that's just much shorter.
  • 36:47 - 36:49
    "g" for "git"...
  • 36:50 - 36:55
    Say we go, for example, with vimrc. It
    can be actually very, very informative,
  • 36:55 - 36:59
    going through this and trying
    to extract useful information.
  • 36:59 - 37:06
    We do not recommend just kind of getting one huge blob
    of this and copying this into your config files,
  • 37:07 - 37:12
    because maybe things are prettier, but you might
    not really understand what is going on.
  • 37:15 - 37:20
    Lastly one thing I want to mention
    about dotfiles is that
  • 37:20 - 37:23
    people not only try to push these
  • 37:25 - 37:29
    files into GitHub just so other
    people can read it, that's
  • 37:29 - 37:33
    one reason. They also make really sure they can
  • 37:34 - 37:39
    reproduce their setup. And to do that
    they use a slew of different tools.
  • 37:39 - 37:41
    Oops, went a little too far.
  • 37:41 - 37:45
    So GNU Stow is, for example, one of them
  • 37:46 - 37:49
    and the trick that they are doing is
  • 37:50 - 37:55
    they are kind of putting all their
    dotfiles in a folder and they are
  • 37:55 - 38:00
    faking to the system, using
    a tool called symlinks,
  • 38:00 - 38:02
    that they are actually what
    they're not. I'm gonna
  • 38:03 - 38:05
    draw really quick what I mean by that.
  • 38:06 - 38:11
    So a common folder structure might look
    like you have your home folder and
  • 38:12 - 38:14
    in this home folder you might have your
  • 38:16 - 38:21
    bashrc, that contains your bash configuration,
    you might have your vimrc and
  • 38:22 - 38:26
    it would be really great if you could
    keep this under version control.
  • 38:27 - 38:29
    But the thing is, you might not
    want to have a git repository,
  • 38:29 - 38:31
    which will be covered tomorrow,
  • 38:31 - 38:32
    in your home folder.
  • 38:32 - 38:37
    So what people usually do is they
    create a dotfiles repository,
  • 38:38 - 38:42
    and then they have entries here for their
  • 38:43 - 38:47
    bashrc and their vimrc. And
    this is where actually
  • 38:48 - 38:50
    the files are
  • 38:50 - 38:52
    and what they are doing is they're just
  • 38:53 - 38:57
    telling the OS to forward, whenever anyone
  • 38:57 - 39:02
    wants to read this file or write to this file,
    just forward this to this other file.
  • 39:03 - 39:06
    This is a concept called symlinks
  • 39:07 - 39:09
    and it's useful in this scenario,
  • 39:09 - 39:13
    but it in general it's a really
    useful tool in UNIX
  • 39:13 - 39:15
    that we haven't covered so far in the lectures
  • 39:15 - 39:17
    but you might be...
  • 39:17 - 39:19
    that you should be familiar with.
  • 39:19 - 39:23
    And in general, the syntax will be "ln -s"
  • 39:23 - 39:30
    for specifying a symbolic link and then
    you will put the path to the file
  • 39:31 - 39:33
    that you want to create and then the
  • 39:34 - 39:36
    symlink that you want to create.
  • 39:39 - 39:41
    And
  • 39:42 - 39:46
    All these all these kind of fancy tools
    that we're seeing here listed,
  • 39:46 - 39:52
    they all amount to doing some sort of this trick, so
    that you can have all your dotfiles neat and tidy
  • 39:53 - 39:58
    into a folder, and then they can be
    version-controlled, and they can be
  • 39:58 - 40:03
    symlinked so the rest of the programs can
    find them in their default locations.
  • 40:07 - 40:09
    Any questions regarding dotfiles?
  • 40:13 - 40:20
    Do you need to have the dotfiles in your home folder,
    and then also dotfiles in the version control folder?
  • 40:21 - 40:25
    So what you will have is,
    pretty much every program,
  • 40:25 - 40:26
    for example bash,
  • 40:26 - 40:30
    will always look for "home/.bashrc".
  • 40:30 - 40:33
    That's where the program is going to look for.
  • 40:34 - 40:40
    What you do when you do a symlink
    is, you place your "home/.bashrc"
  • 40:40 - 40:45
    it's just a file that is kind
    of a special file in UNIX,
  • 40:45 - 40:50
    that says oh, whenever you want to read
    this file go to this other file.
  • 40:52 - 40:53
    There's no content, like there is no...
  • 40:54 - 40:58
    your aliases are not part of this dotfile. That file
    is just kind of like a pointer, saying now you should
  • 40:58 - 40:59
    go that other way.
  • 40:59 - 41:03
    And by doing that you can have your other file
  • 41:03 - 41:04
    in that other folder.
  • 41:05 - 41:06
    If version controlling is not useful, think about
  • 41:06 - 41:11
    what if you want to have them in your Dropbox
    folder, so they're synced to the cloud,
  • 41:11 - 41:15
    for example. That's kind of another use case
    where like symlinks could be really useful
  • 41:16 - 41:21
    So you don't need the folder dotfiles
    to be in the home directory, right?
  • 41:21 - 41:24
    Because you can just use the symlink,
    that points somewhere else.
  • 41:24 - 41:30
    As long as you have a way for the default path
    to resolve wherever you have it, yeah.
  • 41:35 - 41:38
    Last thing I want to cover in the lecture...
  • 41:38 - 41:40
    Oh, sorry, any other questions about dotfiles?
  • 41:49 - 41:53
    Last thing I want to cover in the lecture
    is working with remote machines,
  • 41:53 - 41:56
    which is a thing that you will run into,
  • 41:56 - 41:57
    sooner or later.
  • 41:57 - 42:02
    And there are a few things that will make your life
    much easier when dealing with remote machines
  • 42:03 - 42:05
    if you know about them.
  • 42:05 - 42:08
    Right now maybe because you are
    using the Athena cluster,
  • 42:08 - 42:11
    but later on, during your programming career,
  • 42:11 - 42:12
    it's pretty sure that
  • 42:12 - 42:15
    there is a fairly ubiquitous
    concept of having your
  • 42:15 - 42:20
    local working environment and then having some
    production server that is actually running the
  • 42:21 - 42:23
    code, so it is really good to get familiar
  • 42:24 - 42:27
    about how to work in/with remote machines.
  • 42:27 - 42:35
    So the main command for working
    with remote machines is SSH.
  • 42:38 - 42:44
    SSH is just like a secure shell, it's
    just gonna take the responsibility for
  • 42:44 - 42:47
    reaching wherever we want or tell it to go
  • 42:48 - 42:51
    and trying to open a session there.
  • 42:51 - 42:52
    So here the syntax is:
  • 42:53 - 42:57
    "JJGO" is the user that I want
    to use in the remote machine,
  • 42:57 - 42:58
    and this is because the user is
  • 42:59 - 43:03
    different from the one I have my local machine,
    which will be the case a lot of the time,
  • 43:03 - 43:07
    then the "@" is telling the
    terminal that this separates
  • 43:07 - 43:13
    what the user is from what the address is.
  • 43:13 - 43:17
    And here I'm using an IP address because
    what I'm actually doing is
  • 43:17 - 43:20
    I have a virtual machine in my computer,
  • 43:20 - 43:23
    that is the one that is remote right now.
  • 43:23 - 43:26
    And I'm gonna be SSH'ing into it. This is the
  • 43:27 - 43:28
    URL that I'm using,
  • 43:28 - 43:30
    sorry, the IP that I'm using,
  • 43:30 - 43:32
    but you might also see things like
  • 43:32 - 43:37
    oh I want to SSH as "JJGO"
  • 43:37 - 43:40
    at "foobar.mit.edu"
  • 43:40 - 43:43
    That's probably something more
    common, if you are using some
  • 43:43 - 43:47
    remote server that has a DNS name.
  • 43:48 - 43:52
    So going back to a regular command,
  • 43:53 - 43:57
    we try to SSH, it asks us for a password,
  • 43:57 - 43:58
    really common thing.
  • 43:58 - 43:59
    And now we're there. We have...
  • 43:59 - 44:03
    we're still in our same terminal emulator
  • 44:03 - 44:10
    but right now SSH is kind of forwarding the
    entire virtual display to display what the
  • 44:10 - 44:14
    remote shell is displaying. And
    we can execute commands here and
  • 44:16 - 44:18
    we'll see the remote files
  • 44:18 - 44:23
    A couple of handy things to know about
    SSH, that were briefly covered in the
  • 44:23 - 44:27
    data wrangling lecture, is that
    SSH is not only good for just
  • 44:28 - 44:34
    opening connections. It will also let
    you just execute commands remotely.
  • 44:34 - 44:37
    So for example, if I do that, it's gonna ask me
  • 44:38 - 44:39
    what is my password?, again.
  • 44:39 - 44:41
    And it's executing this command
  • 44:41 - 44:43
    then coming back to my terminal
  • 44:43 - 44:47
    and piping the output of what that
    command was, in the remote machine,
  • 44:47 - 44:50
    through the standard output in my current cell.
  • 44:50 - 44:54
    And I could have this in...
  • 44:58 - 45:00
    I could have this in a pipe, and
  • 45:01 - 45:04
    this will work and we'll just
  • 45:04 - 45:06
    drop all this output and then have a local pipe
  • 45:06 - 45:08
    where I can keep working.
  • 45:09 - 45:12
    So far, it has been kind of inconvenient,
    having to type our password.
  • 45:13 - 45:15
    There's one really good trick for this.
  • 45:15 - 45:17
    It's we can use something called "SSH keys".
  • 45:17 - 45:21
    SSH keys just use public key encryption
  • 45:21 - 45:25
    to create a pair of SSH keys, a public
    key and a private key, and then
  • 45:25 - 45:29
    you can give the server the
    public part of the key.
  • 45:29 - 45:33
    So you copy the public key and
    then whenever you try to
  • 45:33 - 45:37
    authenticate instead of using your password,
    it's gonna use the private key to
  • 45:38 - 45:41
    prove to the server that you are
    actually who you say you are.
  • 45:44 - 45:48
    We can quickly showcase how you will go
  • 45:48 - 45:49
    about doing this.
  • 45:49 - 45:53
    Right now I don't have any SSH keys,
    so I'm gonna create a couple of them.
  • 45:54 - 45:58
    First thing, it's just gonna ask
    me where I want this key to live.
  • 45:59 - 46:01
    Unsurprisingly, it's doing this.
  • 46:01 - 46:05
    This is my home folder and then
    it's using this ".ssh" path,
  • 46:05 - 46:09
    which refers back to the same concept
    that we covered earlier about having
  • 46:09 - 46:12
    dotfiles. Like ".ssh" is a folder
    that contains a lot of the
  • 46:13 - 46:17
    configuration files for how
    you want SSH to behave.
  • 46:17 - 46:19
    So it will ask us a passphrase.
  • 46:20 - 46:23
    The passphrase is to encrypt
    the private part of the key
  • 46:23 - 46:27
    because if someone gets your private key,
    if you don't have a password protected
  • 46:28 - 46:30
    private key, if they get that key
  • 46:30 - 46:32
    they can use that key to impersonate
    you in any server.
  • 46:32 - 46:34
    Whereas if you add a passphrase,
  • 46:34 - 46:38
    they will have to know what the passphrase
    is to actually use the key.
  • 46:41 - 46:52
    It has created a keeper. We can check that
    these two files are now under ssh.
  • 46:52 - 46:54
    And we can see...
  • 46:58 - 47:03
    We have these two files: we have
    the 25519 and the public key.
  • 47:03 - 47:06
    And if we "cat" through the output,
  • 47:06 - 47:10
    that key is actually not like
    any fancy binary file, it's
  • 47:15 - 47:21
    just a text file that has the contents
    of the public key and some
  • 47:23 - 47:27
    alias name for it, so we can
    know what this public key is.
  • 47:27 - 47:32
    The way we can tell the server that
    we're authorized to SSH there
  • 47:32 - 47:38
    is by just actually copying this file,
    like copying this string into a file,
  • 47:38 - 47:42
    that is ".ssh/authorized_keys".
  • 47:42 - 47:46
    So here what I'm doing is I'm
  • 47:47 - 47:50
    catting the output of this file
  • 47:50 - 47:54
    which is just this line of
    text that we want to copy
  • 47:54 - 47:57
    and I'm piping that into SSH and then remotely
  • 47:58 - 48:02
    I'm asking "tee" to dump the contents
    of the standard input
  • 48:02 - 48:05
    into ".ssh/authorized_keys".
  • 48:05 - 48:10
    And if we do that, obviously it's
    gonna ask us for a password.
  • 48:15 - 48:19
    It was copied, and now we
    can check that if we try
  • 48:20 - 48:22
    to SSH again,
  • 48:22 - 48:25
    It's going to first ask us for a passphrase
  • 48:25 - 48:29
    but you can arrange that so that
    it's saved in the session
  • 48:29 - 48:35
    and we didn't actually have to
    type the key for the server.
  • 48:35 - 48:37
    And I can kind of show that again.
  • 48:46 - 48:48
    More things that are useful.
  • 48:48 - 48:49
    Oh, we can do...
  • 48:49 - 48:52
    If that command seemed a little bit janky,
  • 48:52 - 48:55
    you can actually use this command
    that is built for this,
  • 48:55 - 49:01
    so you don't have to kind of
    craft this "ssh tee" command.
  • 49:01 - 49:04
    That is just called "ssh-copy-id".
  • 49:05 - 49:08
    And we can do the same
  • 49:08 - 49:10
    and it's gonna copy the key.
  • 49:10 - 49:14
    And now, if we try to SSH,
  • 49:14 - 49:18
    we can SSH without actually
    typing any key at all,
  • 49:19 - 49:20
    or any password.
  • 49:21 - 49:22
    More things.
  • 49:22 - 49:24
    We will probably want to copy files.
  • 49:24 - 49:25
    You cannot use "CP"
  • 49:25 - 49:30
    but you can use "SCP", for "SSH copy".
  • 49:30 - 49:34
    And here we can specify that we want
    to copy this local file called notes
  • 49:34 - 49:37
    and the syntax is kind of similar.
  • 49:37 - 49:40
    We want to copy to this remote and
  • 49:40 - 49:44
    then we have a semicolon to separate
    what the path is going to be.
  • 49:44 - 49:45
    And then we have
  • 49:45 - 49:47
    oh, we want to copy this as notes
  • 49:47 - 49:51
    but we could also copy this as foobar.
  • 49:52 - 49:56
    And if we do that, it has been executed
  • 49:56 - 49:59
    and it's telling us that all the
    contents have been copied there.
  • 50:00 - 50:02
    If you're gonna be copying a lot of files,
  • 50:02 - 50:05
    there is a better command
    that you should be using
  • 50:05 - 50:08
    that is called "RSYNC". For example, here
  • 50:08 - 50:11
    just by specifying these three flags,
  • 50:11 - 50:16
    I'm telling RSYNC to kind of preserve
    all the permissions whenever possible
  • 50:16 - 50:20
    to try to check if the file
    has already been copied.
  • 50:20 - 50:24
    For example, SCP will try to copy
    files that are already there.
  • 50:24 - 50:26
    This will happen for example
    if you are trying to copy
  • 50:26 - 50:29
    and the connection interrupts
    in the middle of it.
  • 50:29 - 50:32
    SCP will start from the very beginning,
    trying to copy every file,
  • 50:32 - 50:37
    whereas RSYNC will continue
    from where it stopped.
  • 50:37 - 50:38
    And here,
  • 50:39 - 50:43
    we ask it to copy the entire folder and
  • 50:44 - 50:47
    it's just really quickly
    copied the entire folder.
  • 50:48 - 50:54
    One of the other things to know about SSH is that
  • 50:54 - 51:00
    the equivalent of the dot file
    for SSH is the "SSH config".
  • 51:00 - 51:06
    So if we edit the SSH config to be
  • 51:13 - 51:18
    If I edit the SSH config to
    look something like this,
  • 51:18 - 51:23
    instead of having to, every
    time, type "ssh jjgo",
  • 51:23 - 51:28
    having this really long string so I can
    like refer to this specific remote,
  • 51:28 - 51:30
    I want to refer, with the specific user name,
  • 51:30 - 51:33
    I can have something here that says
  • 51:33 - 51:36
    this is the username, this
    is the host name, that this
  • 51:37 - 51:41
    host is referring to and you should
    use this identity file.
  • 51:41 - 51:44
    And if I copy this,
  • 51:44 - 51:46
    this is right now in my local folder,
  • 51:46 - 51:49
    I can copy this into ssh.
  • 51:50 - 51:54
    Now, instead of having to do this really
    long command, I can just say
  • 51:54 - 51:57
    I just want to SSH into the host called VM.
  • 51:58 - 52:03
    And by doing that, it's grabbing all that
    configuration from the SSH config
  • 52:03 - 52:05
    and applying it here.
  • 52:05 - 52:10
    This solution is much better than something
    like creating an alias for SSH,
  • 52:10 - 52:13
    because other programs like SCP and RSYNC
  • 52:13 - 52:19
    also know about the dotfiles for SSH and
    will use them whenever they are there.
  • 52:23 - 52:30
    Last thing I want to cover about remote machines is
    that here, for example, we'll have tmux and we can,
  • 52:32 - 52:36
    like I was saying before, we
    can start editing some file
  • 52:39 - 52:44
    and we can start running some job.
  • 52:54 - 52:56
    For example, something like HTOP.
  • 52:56 - 52:59
    And this is running here, we can
  • 52:59 - 53:01
    detach from it,
  • 53:01 - 53:03
    close the connection and
  • 53:04 - 53:08
    then SSH back. And then, if you do "tmux a",
  • 53:08 - 53:11
    everything is as you left it, like
    nothing has really changed.
  • 53:11 - 53:15
    And if you have things executing there in
    the background, they will keep executing.
  • 53:18 - 53:23
    I think that, pretty much, ends
    all I have to say for this tool.
  • 53:23 - 53:26
    Any questions related to remote machines?
  • 53:33 - 53:37
    That's a really good question.
    So what I do for that,
  • 53:39 - 53:39
    Oh, yes, sorry.
  • 53:39 - 53:45
    So the question is, how do you deal with
    trying to use tmux in your local machine,
  • 53:45 - 53:48
    and also trying to use tmux
    in the remote machine?
  • 53:48 - 53:51
    There are a couple of tricks
    for dealing with that.
  • 53:51 - 53:53
    The first one is changing the prefix.
  • 53:53 - 53:55
    So what I do, for example, is
  • 53:55 - 54:00
    in my local machine the prefix I have
    changed from "Ctrl+B" to "Ctrl+A" and
  • 54:00 - 54:03
    then in remove machines this is still "Ctrl+B".
  • 54:03 - 54:06
    So I can kind of swap between,
  • 54:06 - 54:10
    if I want to do things to the
    local tmux I will do "Ctrl+A"
  • 54:10 - 54:13
    and if I want to do things to the
    remote tmux I would do "Ctrl+B".
  • 54:15 - 54:20
    Another thing is that you
    can have separate configs,
  • 54:20 - 54:24
    so I can do something like this, and then...
  • 54:27 - 54:31
    Ah, because I don't have my own ssh config, yeah.
  • 54:32 - 54:33
    But if you...
  • 54:33 - 54:34
    Um, I can SSH "VM".
  • 54:37 - 54:39
    Here, what you see,
  • 54:39 - 54:41
    the difference between these
    two bars, for example,
  • 54:41 - 54:44
    is because the tmux config is different.
  • 54:44 - 54:48
    As you will see in the exercises,
    the tmux configuration is in
  • 54:50 - 54:54
    the tmux.conf
  • 54:57 - 54:58
    And in tmux.conf,
  • 54:58 - 55:02
    here you can do a lot of things like changing
    the color depending on the host you are
  • 55:02 - 55:07
    so you can get like quick visual
    feedback about where you are, or
  • 55:07 - 55:10
    if you have a nested session. Also, tmux will,
  • 55:11 - 55:15
    if you're in the same host and you
    try to tmux within a tmux session,
  • 55:15 - 55:19
    it will kind of prevent you from doing
    it so you don't run into issues.
  • 55:22 - 55:25
    Any other questions related, to kind
    of all the topics we have covered.
  • 55:29 - 55:33
    Another answer to that question is
    also, if you type the prefix twice,
  • 55:33 - 55:36
    it sends it once to the underlying shell.
  • 55:36 - 55:40
    So the local binding is "Ctrl+A" and
    the remote binding is "Ctrl+A",
  • 55:40 - 55:45
    You could type "Ctrl+A", "Ctrl+A" and then "D", for
    example, detaches from the remote, basically.
  • 55:52 - 56:00
    I think that ends the class for today, there's a bunch
    of exercises related to all these main topics and
  • 56:00 - 56:05
    we're gonna be holding office hours today, too.
    So feel free to come and ask us any questions.
Title:
Lecture 5: Command-line Environment (2020)
Description:

more » « less
Video Language:
English
Duration:
56:07

English subtitles

Revisions