21st March, 2023
https://hckr.cc/ht-unix-slides
SSH stands for Secure SHell. It is intended as a secure replacement for telnet, rsh, rlogin, as well as ftp. SSH also can serve as a secure conduit through which other services can be encrypted.
It has become more and more common for programmers to use remote servers in their everyday work. If you need to use remote servers in order to deploy backend software or you need a server with higher computational capabilities, you will end up using SSH.
To SSH
into a server you execute a command as
follows
ssh foo@stu.comp.nus.edu
we are trying to login to our host with hostname:
stu.comp.nus.edu
user foo
Flags | Details |
---|---|
-4 | Forces IPv4 |
-6 | Forces IPv6 |
-i | Specify identity file |
-p | Specify port |
You can very easily configure your ssh details in a
~/.ssh/config
mkdir -p ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/config && chmod 600 ~/.ssh/config
The config file follows this format:
Host hostname1
SSH_OPTION value
SSH_OPTION value
Host hostname2
SSH_OPTION value
Host *
SSH_OPTION value
Keyword | Meaning |
---|---|
Host | Host name you want to use |
HostName | Real host name to connect to |
User | User name |
Port | Port number |
ProxyCommand | Command used to connect to server |
ProxyJump | Covered later |
Character | Meaning | Examples |
---|---|---|
* | Matches 0 or more characters | Host * matches all hosts, 192.168.0.* matches from 0-255 |
? | Matches exactly one char | Host 10.10.0.? matches 10.10.0.[0-9] ONLY. |
! | Negates the match | !10.10.0.5 matches anything not 10.10.0.5 |
Char | Meaning |
---|---|
%h | Hostname |
%p | Port |
%r | Username |
Suppose you are part of a university/entreprise and need to connect to a school server. However, you are required to login via a login node or VPN. Let’s set this up in our config.
Host stu
HostName %h.comp.nus.edu.sg
User <enter user>
Host pe11? pe120
HostName %h.comp.nus.edu.sg
ProxyJump stu
User <enter user>
Since %h is the Host
, we can quickly append it to our
HostName so that we have a shorter alias!
Without going into details into public/private key, it basically gives you a way to identify yourself to server without a password
- ssh-keygen -t rsa -b 4096 // default
- ssh-keygen -t dsa
- ssh-keygen -t ecdsa -b 521
- ssh-keygen -t ed25519
You can then give your public keys to trusted servers to allow you to connect to them without password auth.
ssh-copy-id -i ~/.ssh/tatu-key-ecdsa user@host
scp
The scp
command is a special command that uses
ssh
to securely copy files between local and host
machines.
// Copy from remote location to local directory
scp [options] username@source_host:directory/filename1 <local directory>
// Copy from local directory to remote location
scp [options] <local directory> username@source_host:directory/filename1
Before we move on, let’s quickly get some practice:
scp
instead of ssh-copy-id!)Congrats! Now that you have a remote to ssh to, you now have a working environment wherever you go!
Great! We have a nice environment we can work with! How do take this further?
are open-source terminal multiplexers.
It allows multiple terminal sessions to be accessed simultaneously in a single window.
It is useful for running more than one command-line program at the same time.
Tmux/screen are programs that allow you to:
To install either tmux
or screen
, just
install them from your package manager!
For the purposes of this demonstration, we’ll be using tmux, but they are both interchangeable.
tmux
tmux ls
tmux a
tmux kill-ses
Ctrl+b
.Ctrl+a
in the config.There are two main concepts in tmux, windows and panes. For this workshop, we’ll focus on panes (because they are much cooler).
Ctrl+b %
- Open a new vertical paneCtrl+b "
- Open a new horizontal paneCtrl+b x
- Kill paneCtrl+b <arrow key>
- Navigate to paneCtrl+b+<arrow key>
- Resize panetail -F
and disconnect from tmux and
run some things to update the log.ls
output?total 5416
drwxr-xr-x 2 chun chun 4096 Mar 17 11:19 .
drwxr-xr-x 12 chun chun 4096 Mar 3 02:38 ..
-rw-r--r-- 1 chun chun 5472298 Mar 17 11:19 f2c69c43-8b85-4ae0-8575-3f34a1e36587.pdf
-rw-r--r-- 1 chun chun 19217 Mar 16 02:44 fs-layout.png
-rw-r--r-- 1 chun chun 21615 Mar 21 08:58 index.html
-rw-r--r-- 1 chun chun 351 Mar 3 02:44 Makefile
-rw-r--r-- 1 chun chun 8895 Mar 21 08:58 slides.md
d
initially indicates that it is a
directory/folder. Format: rwxrwxrwx
Example: rwxr-xr-x
Category: UUUGGGOOO
chmod
chmod u=rw,og=r new_file.txt
chmod a+x new_script.sh
chmod
Example
Perms: rwx r-x r-x
Binary: 111 101 101
Decimal: 7 5 5
rwx rwx rwx
111 111 111
7 7 7
chmod 777 <filename>
to change the
permissions as such./sys
. sysfs exposes a number of kernel parameters as files,
so that you can easily reconfigure the kernel on the fly without
specialized tools./proc
file system is another psuedo-filesystem
which provides an interface to kernel data structuresIf you have a laptop running Unix system locally, you probably have a /sys/class system file!
Play with it, see if you can:
Using only files from /sys/class!
ln -s <target> <link>
cron
The crontab command allows you to install, view , or open a crontab file for editing:
crontab -e
- Edit crontab file, or create one if it
doesn’t already exist.crontab -l
- Display crontab file contents.crontab -r
- Remove your current crontab file.crontab -i
- Remove your current crontab file with a
prompt before removal.crontab -u
Syntax:
* * * * * command(s)
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sun=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Example:
*/5 * * * * /path/to/script.sh # Run every 5 minutes
* * * * * date
00 08-16 * * * date
top
to start the program.PID: Shows task’s unique process id.
PR: The process’s priority. The lower the number, the higher the priority.
VIRT: Total virtual memory used by the task.
USER: User name of owner of task.
%CPU: Represents the CPU usage.
TIME+: CPU Time, the same as ‘TIME’, but reflecting more granularity through hundredths of a second.
SHR: Represents the Shared Memory size (kb) used by a task.
NI: Represents a Nice Value of task. A Negative nice value implies higher priority, and positive Nice value means lower priority.
%MEM: Shows the Memory usage of task.
RES: How much physical RAM the process is using, measured in kilobytes.
COMMAND: The name of the command that started the process.
(Warning: Kill processes on your machine at your own risk! Make sure you know what you’re killing)
top
and see what you can find out about
your system!top
-> Shift + P
top -> Shift + M
-> k
top
-> f
-> PID
->
s
-> q
-> Shift + R
https://hckr.cc/links
https://hckr.cc/hs2023-cosplay