Command-line Environment #
Lecture source: https://missing.csail.mit.edu/2020/command-line/
Exercises #
Job control #
- 
From what we have seen, we can use some ps aux | grepcommands to get our jobs’ pids and then kill them, but there are better ways to do it. Start asleep 10000job in a terminal, background it withCtrl-Zand continue its execution withbg. Now usepgrepto find its pid andpkillto kill it without ever typing the pid itself. (Hint: use the-afflags).
 $ sleep 10000 ^Z [1] + 78090 suspended sleep 10000 $ bg %1 [1] + 78090 continued sleep 10000 $ jobs [1] + running sleep 10000 $ pgrep sleep 78090 $ pkill -af sleep [1] + 78090 terminated sleep 10000
- 
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 thewaitcommand. Try launching the sleep command and having anlswait until the background process finishes.However, this strategy will fail if we start in a different bash session, since waitonly works for child processes. One feature we did not discuss in the notes is that thekillcommand’s exit status will be zero on success and nonzero otherwise.kill -0does not send a signal but will give a nonzero exit status if the process does not exist. Write a bash function calledpidwaitthat takes a pid and waits until the given process completes. You should usesleepto avoid wasting CPU unnecessarily.
 $ sleep 60 & [1] 81347 $ wait ls [1] + 81347 done sleep 60
Terminal multiplexer #
- 
Follow this tmuxtutorial 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 #
- Create an alias dcthat resolves tocdfor when you type it wrongly.
- Run history | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 10to get your top 10 most used commands and consider writing shorter aliases for them. Note: this works for Bash; if you’re using ZSH, usehistory 1instead of justhistory.
Dotfiles #
Let’s get you up to speed with dotfiles.
- 
Create a folder for your dotfiles and set up version control. 
- 
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).
- 
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 -sfor each file, or you could use a specialized utility.
- 
Test your installation script on a fresh virtual machine. 
- 
Migrate all of your current tool configurations to your dotfiles repository. 
- 
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.
- 
Go to ~/.ssh/and check if you have a pair of SSH keys there. If not, generate them withssh-keygen -o -a 100 -t ed25519. It is recommended that you use a password and usessh-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/id_ed25519.pub 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| +----[SHA256]-----+
- 
Edit .ssh/configto have an entry as followsHost vm User username_goes_here HostName ip_goes_here IdentityFile ~/.ssh/id_ed25519 LocalForward 9999 localhost:8888
- 
Use ssh-copy-id vmto 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/id_ed25519.pub" /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.
- 
Start a webserver in your VM by executing python -m http.server 8888. Access the VM webserver by navigating tohttp://localhost:9999in 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
- 
Edit your SSH server config by doing sudo vim /etc/ssh/sshd_configand disable password authentication by editing the value ofPasswordAuthentication. Disable root login by editing the value ofPermitRootLogin. Restart thesshservice withsudo service sshd restart. Try sshing in again.
- 
(Challenge) Install moshin 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 moshis providing.
- 
(Challenge) Look into what the -Nand-fflags do insshand 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 0.0.0.0 port 10010 (http://0.0.0.0:10010/) ... # on the client $ ssh -fN -L 19999:0.0.0.0:10010 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