owned this note
owned this note
Published
Linked with GitHub
---
tags: 1091, lsa
---
- Book mode https://hackmd.io/@ncnu-opensource/book
Week 04(2020/10/15)
===
:::info
## 預習方向
- password
- `/etc/passwd`
- `/etc/shadow`
- `/use/bin/passwd`
- 權限, setuid, setgid
- `passwd -S` 密碼狀態
- 多工執行 / 依序執行
- 套件管理
- `apt`
- `aptitude`
- 文字編輯
- vi, vim, nano
- `ssh`, `telnet`
- `ifconfig`
- OSI 七層架構 / TCP/IP 四層架構
- nginx
:::
## 上週作業
- 網頁: /var/www
- 信件 :
- /var/mail
- /var/spool
- /var/log :
- 記錄檔(純文字檔)
- 每隔一段時間作壓縮,防止過度膨脹
- /etc/ 不重要
> 當你覺得設定檔很重要,裡面涵蓋了許多你的特殊設定,不想在其他磁區壞掉時影響到此磁區,便可以做切割(保護作用),但不宜切割太大(ex:2T)
- swap : 1,20G左右。不用多大 ( 200、300G ) 、也不能太小 ( 3、4G )
## 基本指令(續10/08)
### 設定密碼
- `passwd`: 更改登入中使用者的密碼
- `/etc/passwd`:儲存關於帳號相關資訊
- name
- passward = x
- uid(user id): 對應使用者有什麼權限
- uid 是 1000 開始,
- gid(group id): 對應使用者有什麼權限
- `/etc/shadow`
- 因是加密後的文件,要加 `sudo`,不然會是亂碼
- `id + "使用者名稱"`: 列出使用者的 uid 以及他屬於哪些群組

- `apt`: ubuntu 下管理套件的工具
- 有 `apt` 、 `apt-get` 、 `/apt/cache`
- 一般安裝時使用 `apt-get` , `apt` 使用其他套件管理功能
- `sudo apt update`: 更新軟體源source list
- `sudo apt upgrade`:更新套件
- `sudo apt list --upgradable`: 查看可更新的套件資訊
- `sudo apt install "要安裝套件名"`: 安裝套件
- 安裝套件前建議先更新軟體源
- `sudo apt remove "要移除的套件名"`: 移除不要的套件
- `aptitude`: 提供圖形化介面
- 不只在安裝時會自動先執行 `apt-get update` 來更新套件資料庫之外,還會把相依的套件一起給裝起來,解決相依性的問題
<!-- - 會自動更新軟體源(`apt update`) 跟前一行相同 -->
- `aptitude` 指令可以做各種事,不用像 `apt-get` 要分成 `apt-get`、`apt-cache` 等不同用途的指令
- 安裝: `sudo aptitude install `
- 開啟: `aptitude`
- 退出: `q`
## 文字編輯器
### vim
- 安裝: `sudo apt install vim`
- `vim "文件名稱"`: 編寫文件,如果沒有打檔名可以稍後再儲存
- 使用指令: 按`esc`接著打下表指令
|指令|用處|
|---|---|
|:w|存檔|
|:w!|在唯獨模式下強制存檔,不過,到底能不能寫入, 還是跟你對該檔案的檔案權限有關。|
|:q|離開|
|:q!|通常有編輯之後如果直接離開,會失敗,這時候就要用強制離開|
|:wq|存檔後離開|
|:e!|還原到剛開啟檔案的時候|
|ZZ|檔案沒有更動,則不儲存離開,若檔案已經經過更動,則儲存後離開|
- `vimtutor`:可以練習所有指令(指令教學 通常花1hr)
#### 一般模式
- `hjkl` : 上下左右
> 如果你要假裝自己很厲害,這個一定要背起來 [name=郭子緯]
- `i`: insert 當前開始編輯
- 編輯是以小白框前面那格開始
- `I`:在開頭打字
- `a`: 下一格開始打
- `A`:在結尾打字
- `u` : 復原
- `x` : 刪除
- `數字 + 大寫G` : 移到數字那行
- `:行數`: 也可以使用
- `$`: 該行最後面
- `yy`:複製
- `p` : 貼上
- `/你要找的東西` : 搜尋
- `n`: 往下找
- `N`: 往上找
- 正規表示法
- `^`: 起頭為你要找的字母..的那行
- `$`: 規定結尾
[[Linux] vi 與 vim 指令一覽表整理](https://code.yidas.com/linux-vi-vim-command/)
#### vim 設定
<!--
- `~/.vimrc`: vim 設定檔
- `set cursorline`: 標示編輯中的那行
- `set nu` : 顯示行數
- `set tabstop=4`: tab 設定為空 4 格
- `set shiftwidth`: 縮排格數
- `expand`: -->
##### 我有用的參數
|說明|指令|
|---|---|
|set cursorline|光標所在的那一行會有底線,幫助尋找光標位置|
set nocompatible|讓 VIM 工作在「非相容模式」下|
set nu|顯示行號
set encoding=utf-8|編輯器使用 UTF8|
set t_Co=256|256 色的 Vim
syntax on|語法上色顯示
set shiftwidth=4|設定縮排寬度 = 4
set tabstop=4|tab 的字元數
set expandtab|用 space 代替 tab
[練習網站](https://regex101.com/)
[視覺化練習網站](https://ihateregex.io/playground)
### nano
- 指令都會在下方,不像 vim 一樣要背指令
- `^` : 這東西是ctrl
- `ctrl + x`:離開
## 網路
### 網路等級
- `sudo apt install net-tools`:
- `ifconfig`: 查看電腦 ip
- 或 `ip address`, 可以縮寫 `ip a`
- `inet`: ipv4
- `netmask`: 可以拿來算是否在同個網段
- `broadcast`: 廣播位置
- 尋找網路下有哪個 ip 可以提供什麼服務
- `inet6`: ipv6
- IP 有分 A, B, C, D, E 五等級

- 計算net ID: ip 跟 mask 做 & 運算
:pencil2: 計算題
ip : 192.168.1.11,子網路遮罩 : 255.255.255.0
轉成二進位
11000000.10101000.00000001.00001011
11111111.11111111.11111111.00000000
---AND------------------------------------------
11000000.10101000.00000001.00000000
網域: 192.168.1.0 (0 跟 255 有特殊用途不能用)
- 實際分配是 1-254
-> IP 位於 192.168.1.1 ~ 192.168.1.254 都在同一個網域下
- 子網路遮罩: 可以用 `/` 接在ip後面表示該 ip 所在網域
> example <!-- 我不會換行QQ -->
> 上圖 `192.168.1.11` ip 可將其網域寫成`192.168.1.11/24`
- 補充特殊用途
```
xxx.xxx.xxx.0 整個網段(並非router)
xxx.xxx.xxx.1 通常用做DNS
xxx.xxx.xxx.254 當gateway
xxx.xxx.xxx.255 當broadcast
```
:::info
### 0.0.0.0
In the Internet Protocol Version 4, the address 0.0.0.0 is a **==non-routable meta-address==**
used to designate an invalid, unknown or non-applicable target.
To give a special meaning to an otherwise invalid piece of data is an application of in-band signaling.
In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine".
If a host has two IP addresses, 192.168.1.1 and 10.1.2.1,
and a server running on the host listens on 0.0.0.0,
it will be reachable at both of those IPs.
In the context of routing, 0.0.0.0 usually means the default route,
i.e. the route which leads to "the rest of" the internet instead of somewhere on the local network.
### 255.255.255.255
**==A broadcast address==** is a logical address at which all devices connected to
a multiple-access communications network are enabled to receive datagrams.
A message sent to a broadcast address is typically received by all network-attached hosts,
rather than by a specific host.
A special definition exists for the IP broadcast address 255.255.255.255.
It is the broadcast address of the zero network or 0.0.0.0,
which in Internet Protocol standards stands for this network, i.e. the local network.
Transmission to this address is limited by definition,
in that it is never forwarded by the routers connecting the local network to other networks.
:::
### TCP/IP四層模型

- http、https 都算 Application layer
- Transport: TCP UDP
- Internet: IP
- Link: 網路卡屬於這層
## 遠端連線伺服器
- 登入類型
- telnet、rsh
- 明文傳輸、未加密
- 連線目的地伺服器,然後輸入帳號和密碼以驗證身分
- 容易被偽裝,竄改資料
- ssh(secure shell安全的殼)
- 可用 `ifconfig` 查 ip(inet)
- windows 是用 `ipconfig`
- Xdmcp、VCP、RDP
- 圖形介面
### SSH
- 全名 : Secure Shell 安全外殼協定
- 解決telnet不安全的問題
- 通常是22port
- 技術簡介
<br>使用非對稱金鑰系統,透過兩把公、私鑰進行加解密
<br>用對方的公鑰加密,對方收到檔案後用自己的私鑰解密

- ssh連線
<br>
- 遠端連本機: `ssh <userName>@localhost` 或是 `ssh 127.0.0.1`
- `sudo apt install ssh` 安裝ssh server版
- 預設: client版
> 用 `ssh` 最好有個好習慣在後面順手加 ` -v` ,可以看到 debug 訊息。\
> 如果用指令不知道他在運行什麼,可以加` -v`,就可以看到它的運行過程了。[name=BlueT]
- 離開: `exit`
- [免密登入](https://xenby.com/b/220-%E6%95%99%E5%AD%B8-%E7%94%A2%E7%94%9Fssh-key%E4%B8%A6%E4%B8%94%E9%80%8F%E9%81%8Ekey%E9%80%B2%E8%A1%8C%E5%85%8D%E5%AF%86%E7%A2%BC%E7%99%BB%E5%85%A5)
- `ssh-keygen -t rsa` 生成金鑰
- `ssh -v [username]@localhost` 使用ssh連本機
- `ssh -v localhost` 使用ssh連本機,並自動填入username
- `ssh-copy-id -i id_rsa.pub [username]@localhost` 複製金鑰到遠端本機
- 公鑰位置
- `~/.ssh/authorized_keys` 別人連進來
- 
- 開源SSH
- openSSH、OSSH
- SSH 的安全驗證兩種級別
- 第一種級別(基於密碼的安全驗證)
- man in middle attack
> ssh 可以也不可以避免中間人攻擊
- 第二種級別(基於金鑰的安全驗證)
<!-- - scp、ftp、sftp -->
### DNS 網域名稱系統
- IP 是一串數字,不好記憶,因此需要有 Domain name
- DNS 可以將 Domain name 對應到 IP
- DNS tree

- 世界上有 13 台 root DNS IP ,如果有必要重新查找IP,會由這 13 個root去開始查找
- `dig +trace <domain name>`: 列出DNS的查找IP流程
- `dig +trace @8.8.8.8 www.google.com`
- `hostnamectl`: host name control
- `hostnamectl set-hostname <要修改的hostname>`: 更改 hostname
## Q & A
Q: 為什麼 upgradable 前面是兩個 dash ?
- 參數規定 ~ 可用 `man` 來看如何下參數
- 像是部份指令 `--help` 可以得知如何下指令一樣 ~~沒有道理XD~~
<!-- 10.2.54.3/25
192.168.1.0/25 -->