Command-line Environment

Command-line Environment #

Lecture source:

Exercises #

Job control #

  1. From what we have seen, we can use some ps aux | grep commands to get our jobs’ pids and then kill them, but there are better ways to do it. Start a sleep 10000 job in a terminal, background it with Ctrl-Z and continue its execution with bg. Now use pgrep to find its pid and pkill to kill it without ever typing the pid itself. (Hint: use the -af flags).

    $ sleep 10000
    [1]  + 78090 suspended  sleep 10000
    $ bg %1
    [1]  + 78090 continued  sleep 10000
    $ jobs
    [1]  + running    sleep 10000
    $ pgrep sleep
    $ pkill -af sleep
    [1]  + 78090 terminated  sleep 10000
  2. Say you don’t want to start a process until another completes. How would you go about it? In this exercise, our limiting process will always be sleep 60 &. One way to achieve this is to use the wait command. Try launching the sleep command and having an ls wait until the background process finishes.

    However, this strategy will fail if we start in a different bash session, since wait only works for child processes. One feature we did not discuss in the notes is that the kill command’s exit status will be zero on success and nonzero otherwise. kill -0 does not send a signal but will give a nonzero exit status if the process does not exist. Write a bash function called pidwait that takes a pid and waits until the given process completes. You should use sleep to avoid wasting CPU unnecessarily.

    $ sleep 60 &
    [1] 81347
    $ wait
    [1]  + 81347 done       sleep 60

Terminal multiplexer #

  1. Follow this tmux tutorial and then learn how to do some basic customizations following these steps.

    My final tmux config:

    # remap prefix from 'C-b' to 'C-a'
    unbind C-b
    set-option -g prefix C-a
    bind-key C-a send-prefix
    # split panes using '|' and '-'
    bind | split-window -h
    bind - split-window -v
    # reserve the '"' and '%' for split panes
    # unbind '"'
    # unbind %
    # reload config file (press '<prefix> r' to reload tmux config)
    bind r source-file ~/.tmux.conf
    # switch panes using Alt-arrow without prefix
    bind -n M-Left select-pane -L
    bind -n M-Right select-pane -R
    bind -n M-Up select-pane -U
    bind -n M-Down select-pane -D
    # Enable mouse mode (tmux 2.1 and above)
    set -g mouse on
    # don't rename windows automatically
    set-option -g allow-rename off

Aliases #

  1. Create an alias dc that resolves to cd for when you type it wrongly.
  2. Run history | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 10 to get your top 10 most used commands and consider writing shorter aliases for them. Note: this works for Bash; if you’re using ZSH, use history 1 instead of just history.

Dotfiles #

Let’s get you up to speed with dotfiles.

  1. Create a folder for your dotfiles and set up version control.

  2. Add a configuration for at least one program, e.g. your shell, with some customization (to start off, it can be something as simple as customizing your shell prompt by setting $PS1).

  3. Set up a method to install your dotfiles quickly (and without manual effort) on a new machine. This can be as simple as a shell script that calls ln -s for each file, or you could use a specialized utility.

  4. Test your installation script on a fresh virtual machine.

  5. Migrate all of your current tool configurations to your dotfiles repository.

  6. Publish your dotfiles on GitHub.

    TODO: use chezmoi .

Remote Machines #

Install a Linux virtual machine (or use an already existing one) for this exercise. If you are not familiar with virtual machines check out this tutorial for installing one.

  1. Go to ~/.ssh/ and check if you have a pair of SSH keys there. If not, generate them with ssh-keygen -o -a 100 -t ed25519. It is recommended that you use a password and use ssh-agent , more info here.

    $ ssh-keygen -o -a 100 -t ed25519
    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/Users/triplez/.ssh/id_ed25519):
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/triplez/.ssh/id_ed25519
    Your public key has been saved in /Users/triplez/.ssh/
    The key fingerprint is:
    SHA256:zmrVc/qNg57fv66gRpEUHUxipG/I7QdYq4hqgsOOAZE triplez@TripleZs-MacBook-Pro
    The key's randomart image is:
    +--[ED25519 256]--+
    |        .==o.    |
    | .      o..o     |
    |E      ....      |
    | .    . *o.      |
    |.      +SB.      |
    |.   . .o=.+ .    |
    |+  . . o+. B     |
    |=+.   .. .=.o+   |
    |=+   .. .ooo+o=+o|
  2. Edit .ssh/config to have an entry as follows

     Host vm
         User username_goes_here
         HostName ip_goes_here
         IdentityFile ~/.ssh/id_ed25519
         LocalForward 9999 localhost:8888
  3. Use ssh-copy-id vm to copy your ssh key to the server.

    $ ssh-copy-id vm
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/triplez/.ssh/"
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    Number of key(s) added:        1
    Now try logging into the machine, with:   "ssh 'vm'"
    and check to make sure that only the key(s) you wanted were added.
  4. Start a webserver in your VM by executing python -m http.server 8888. Access the VM webserver by navigating to http://localhost:9999 in your machine.

    $ ssh vm python3 -m http.server 8888
    # open a new shell session
    $ curl -I http://localhost:9999
    HTTP/1.0 200 OK
    Content-Length: 1130
    Connection: keep-alive
    Content-Type: text/html; charset=utf-8
    Date: Mon, 14 Mar 2022 15:59:41 GMT
    Keep-Alive: timeout=4
    Proxy-Connection: keep-alive
    Server: SimpleHTTP/0.6 Python/3.6.9
  5. Edit your SSH server config by doing sudo vim /etc/ssh/sshd_config and disable password authentication by editing the value of PasswordAuthentication. Disable root login by editing the value of PermitRootLogin. Restart the ssh service with sudo service sshd restart. Try sshing in again.

  6. (Challenge) Install mosh in the VM and establish a connection. Then disconnect the network adapter of the server/VM. Can mosh properly recover from it?

    TODO, don’t know what the mosh is providing.

  7. (Challenge) Look into what the -N and -f flags do in ssh and figure out a command to achieve background port forwarding.

    -N      Do not execute a remote command.  This is useful for just forwarding ports.
    -f      Requests ssh to go to background just before command execution.  This is
            useful if ssh is going to ask for passwords or passphrases, but the user wants
            it in the background.  This implies -n.  The recommended way to start X11
            programs at a remote site is with something like ssh -f host xterm.
    # on the server
    $ python3 -m http.server 10010
    Serving HTTP on port 10010 ( ...
    # on the client
    $ ssh -fN -L 19999: root@<your_server_address>
    $ curl -I http://localhost:19999
    HTTP/1.0 200 OK
    Content-Length: 1130
    Connection: keep-alive
    Content-Type: text/html; charset=utf-8
    Date: Mon, 14 Mar 2022 16:25:57 GMT
    Keep-Alive: timeout=4
    Proxy-Connection: keep-alive
    Server: SimpleHTTP/0.6 Python/3.6.9