Skip to content

Commit 4cec095

Browse files
committed
main
1 parent 228c4d8 commit 4cec095

20 files changed

+297
-71
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# OpenSSH Quick Start
2+
3+
## The Authentication
4+
5+
A host machine that running a SSH server can be called as **Remote** which should store *public keys* for client hosts.
6+
A host machine(client) attempting to connect with Remote should know a *private key* and *passphrase*.
7+
This is how the authentication happen:
8+
9+
1. Client sends request to Remote.
10+
2. If the Client had never connect to the Remote, warning prompts to confirm connection implying it may be dangerous.
11+
3. If any public key on Remote matches the client, Remote requests the client for authenticator which should be calculated by *passphrase* and *private key*
12+
4. User enter the *passphrase* to decrypt the private key, SSH auto-calculates the authenticator, then send it to the Remote.
13+
14+
## Client Side
15+
16+
- Generate public key for connecting **one or more** remotes by `ssh-keygen`
17+
```sh
18+
ssh-keygen -t rsa
19+
```
20+
- Register new public key to remote by `ssh-copy-id`(may require login on remote)
21+
```sh
22+
# add public key under `~/.ssh/` to `~/.ssh/authorized_keys` on <remote>
23+
ssh-copy-id -i <pub_key_file> [<username>@]<remote>
24+
```
25+
- Managing authenticated state and **agent forwarding** by `ssh-agent`
26+
```sh
27+
ssh-agent $SHELL
28+
```
29+
- Add private key to `ssh-agent` to memorize the state of the key by `ssh-add`
30+
```sh
31+
ssh-add ~/.ssh/id_ed25519
32+
ssh-add -l # list all private keys memorized by ssh-agent
33+
ssh-add -d id_ed25519 # inform ssh-agent to forget this key
34+
ssh-add -D # forget all keys
35+
```
36+
- Managing once connected remote in `~/.ssh/known_hosts`
37+
- File transfer by `scp`
38+
- Modify *passphrase* by `ssh-keygen` if you forgot
39+
```sh
40+
ssh-keygen -p
41+
```
42+
43+
## Server Side
44+
45+
- Start SSH server by `sshd`
46+
- Manage SSH server config by `/etc/ssh/sshd_config`
47+
- Manage public key for multiple clients in `~/.ssh/authorized_keys`
48+
> [!NOTE]
49+
> Besides `ssh-copy-id`, you can directly edit `~/.ssh/authorized_keys` to add new public key for a client.
50+
51+
52+
## Create Key Pair
53+
54+
```sh
55+
ssh-keygen [-t <type>] [-f <path>] [-N <passphrase>] [-C <comment>]
56+
```
57+
58+
- `-t`: specify key type, `ed25519` by default.
59+
- `-f`: output fullname of the key, public key will have extra extension `.pub`. `ssh-keygen` will prompt for it if not specified anyway.
60+
- `-N`: specify passphrase, will ask anyway if unspecified. This may expose your passphrase to command history.
61+
- `-C`: comment for identifying the key.
62+
63+
> [!NOTE]
64+
> see `man ssh-keygen`
65+
66+
## Re-encrypt Key
67+
68+
You might want to encrypt the keys with different type and passphrase, as well as a new name.
69+
70+
```sh
71+
ssh-keygen -p [-t <type>] [-f <path>] [-P <old_passphrase>] [-N <new_passphrase>]
72+
```
73+
74+
Similar to creating a key pair, you don't have to fill all options, leave them empty and let the cli prompt for you.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Setup nix-on-droid
2+
3+
## Prerequisites
4+
5+
- Root privilege on you android phone(it's not stable in devices without root)
6+
- nix-on-droid installed.
7+
- Optional: `adb`, `pwsh`
8+
You can use `adb` to type inputs from your computer connected with your android device.
9+
The following powershell function solves the escape problem of `adb shell input text`, so you don't have to escape manually.
10+
```ps1
11+
# Use -Enter to press enter after command input
12+
function adbin([string]$Str, [switch]$Enter) {
13+
$special = @( ' ', '\|', '\$', '&', '\(', '\)', '~','\*', "\'",'"','<','>')
14+
foreach ($char in $special) {
15+
$Str = $Str -replace $char, ($char.Length -gt 1 ? $char : "\$char")
16+
}
17+
adb shell input text $Str
18+
if ($Enter) {
19+
adb shell input keyevent KEYCODE_ENTER
20+
}
21+
}
22+
```
23+
> [!NOTE]
24+
> You can wrap the same as bash function by `awk` or other text manipulation tools.
25+
26+
27+
## Init
28+
29+
- nix-on-droid may ask for url for certain file, if the url is not accessible on your phone, download it and transfer to your phone. And replace the default url as `file:///sdcard/...`
30+
- type `yes` when nix prompt for downloads for first init.
31+
- add and update channels:
32+
```sh
33+
nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager && channel
34+
```
35+
> [!TIP]
36+
> If you use the wrapper function mentioned above, would be like this:
37+
>```ps1
38+
>adbin -Enter 'nix-channel --add https://github.com/nix-community/home-manager/archive/release-24.05.tar.gz home-manager'
39+
>```
40+
41+
## Connect to nix-on-droid
42+
43+
- Install `openssh`
44+
45+
```sh
46+
nix profile install nixpkgs#openssh
47+
```
48+
49+
- create a empty `ssh_config`, `sshd` requires at least one specified. We don't specify any option in it in this guide but it's needed afterward.
50+
51+
```sh
52+
mkdir -p /etc/ssh/ && touch /etc/ssh/sshd_config
53+
```
54+
55+
- generate a host key for nix-on-droid, change the key type and passphrase as you like, they don't make too much difference.
56+
57+
```sh
58+
ssh-keygen -t rsa -b 4096 -f ssh_host_rsa_key -N "" # key is generated in pwd
59+
```
60+
61+
- create `~/.ssh/authorized_keys` and paste your public key from your computer(`gc ~/.ssh/<name>.pub`) to this file.
62+
63+
```sh
64+
mkdir -p ~/.ssh/ && touch ~/.ssh/authorized_keys && echo <pub> >> ~/.ssh/authorized_keys
65+
```
66+
67+
- start ssh daemon by `sshd`
68+
69+
```sh
70+
sshd -p <port> -h <host_key> -d
71+
```
72+
73+
`-d` is essential to know whether your port is been taken or not. See details in `man sshd`.
74+
75+
- now connect to nix-on-droid from your computer
76+
77+
```ps1
78+
ssh -l nix-on-droid -p <port> <ipaddr_of_phone>
79+
```
80+
81+
> [!NOTE]
82+
> `<ipaddr_of_phone>` can be inspected from your `Settings - About phone`
83+
84+
## Final Step
85+
86+
Finally you can type everything in your computer through SSH! So use nix as you like.

docs/document/PowerShell/docs/File System/4.Create Item.md renamed to docs/document/PowerShell/docs/File System/4.Manipulate Item.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Create Item
1+
# Manipulate Item
2+
## Create Item
23

34
Powershell uses a single cmdlet named `New-Item` to represent all kinds of creation logic for file system items like folder, file, symlinks...
45

@@ -24,7 +25,7 @@ New-Item <file_path>
2425
> [!TIP]
2526
> Use `-Force` flag to overwrite existing target.
2627
27-
## Directory
28+
### Directory
2829

2930
```ps1
3031
New-Item <dir_path> -ItemType Directory
@@ -41,19 +42,38 @@ mkdir <dir_path>
4142
> [!TIP]
4243
> Use `-Force` flag to overwrite existing target.
4344
44-
## Symbolic Link
45+
### Symbolic Link
4546

4647
```ps1
4748
New-Item <symlink_path> -Target <source> -ItemType SymbolicLink
4849
```
4950

51+
> [!NOTE]
52+
> `-Target` is an alias of `-Value`
53+
5054
> [!TIP]
5155
> Use `-Force` flag to overwrite existing target.
5256
53-
## Ignore Wildcards
57+
### Ignore Wildcards
5458

5559
`-Path` translates wildcards by default, if you do need to include special characters from wildcards syntax for your new item, use `-LiteralPath`.
5660

5761
```ps1
5862
New-Item -LiteralPath 'foo*.txt' # creates a file literally named `foo*.txt`
5963
```
64+
65+
## Delete
66+
67+
- delete file
68+
69+
```ps1
70+
ri <file>
71+
```
72+
73+
- delete folder
74+
75+
```ps1
76+
ri -rec -force <folder>
77+
```
78+
79+
### Rename

docs/document/PowerShell/docs/File System/6. Inspect File System.md

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Inspect File System
2+
3+
## List Items
4+
5+
- Recursively
6+
7+
```ps1
8+
gci -rec
9+
```
10+
11+
- Include Hidden Items
12+
13+
```ps1
14+
gci -force
15+
```
16+
17+
## Size
18+
19+
- Directory Size
20+
21+
```ps1
22+
gci -file -rec -force | measure { $_.Length / 1MB } -Sum
23+
```

docs/document/PowerShell/docs/Language/HashTable.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ $foo['Name'] # foo
5757
$foo['Name', 'Age'] # @('foo', 18)
5858
```
5959
60+
> [!NOTE]
61+
> PowerShell uses `TryGetValue` when accessing value with indexer syntax for `IDictionary` types, so it never throw.
62+
6063
`.` accessor would also works **as long as there's no duplicated Extended Property with the same name of the key you passed.**
6164

6265
```ps1

docs/document/PowerShell/docs/Object Manipulation/Compare.md renamed to docs/document/PowerShell/docs/Object Manipulation/1.Compare.md

File renamed without changes.

docs/document/PowerShell/docs/Object Manipulation/1.Overview.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

docs/document/PowerShell/docs/Object Manipulation/Measure.md renamed to docs/document/PowerShell/docs/Object Manipulation/2.Measure.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ gci | measure -Property Length -Sum -Max
3535
# -Property is positional
3636
gci | measure Length -Sum -Max
3737
38+
# measure different properties with same rule
39+
gps | measure VM,ID -Sum -Max
40+
3841
# Calculate Max in unit GB
3942
gci | measure { $_.Length / 1GB } -Max
4043
```

docs/document/PowerShell/docs/Object Manipulation/Where.md renamed to docs/document/PowerShell/docs/Object Manipulation/3.Where.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,15 @@ gci -file | where Extension -eq '.ps1'
3333
gci -file | where { $_.Extension -eq '.ps1' }
3434
```
3535

36-
> [!NOTE]
37-
> See: `help where`
36+
## Intrinsic Where
37+
38+
Intrinsic `Where` can be useful when performance matters, it provides a way to return early base on certain condition without consuming the whole iteration.
39+
40+
- First or Last items satisfy certain condition
41+
- Skip until one satisfies the condition and return all remaining items(including the one satisfies)
42+
- Return items until one not satisfies the condition(excluding the one satisfies)
43+
- Split items into two collections, one contains items satisfied the condition, the another are items remained.
44+
45+
```cs
46+
Where(scriptblock condition, WhereOperatorSelectionMode mode = 0, int? count)
47+
```

0 commit comments

Comments
 (0)