用免費免安裝的 Docker 練習基本 Ubuntu Linux 操作  -- by tsaiwn@cs.nctu.edu.tw 交大資工 蔡文能      
  iottalk.vip/pwd/ -- 這是本教學的網址,或 iottalk.vip/unix 也可,不必打 https://      
 ...   左手指月 半小時讓你變成 Linux 高手 :-)     關於 IoTtalk 使用者詳細使用手冊  
      或 http://liny.cs.nctu.edu.tw/#IoTtalk   (林一平教授網頁; 點入後在 Document 下方, 有中文版和英文版(English version))
這是 Ubuntu Linux 入門教學 -- 透過免費 Docker PWD(Play-With-Docker) !!
雖不是 Docker 教學,但還是會先教一點 Docker 基本常識和基本使用 ... 用手機也可以喔!
* Super user(root)該知道的五四三..
o 關於 docker 詳細請看Docker.com 官方文件 docs.docker.com
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o 微軟在2018年6月以75億美元收購GitHub.com(Github 2008年2月上線,但Github公司與Torvalds無關:-))
 ==>What is NFT (Non-Fungible Token) ?    (什麼是 NFT?)

PWD -- Play With Docker 簡介 -- 有瀏覽器就可以用了 !
   
o 要先有 Docker 帳號,到 https://hub.docker.com/
    進入後點 Sign Up 註冊後到Mail驗證即可使用。
o 使用 Docker 帳密就可使用 PWD : https://labs.play-with-docker.com/

==> 如果你有先登入 Docker, 再進入 PWD 首頁可看到Start,點它就會看到如下畫面:
+ADD NEW INSTANCE 就可產生一個 Linux 機器,跑的是 Alpine Linux 作業系統。
點那個板手可以最多產生包括五個機器的 Cluster (叢集;集群)
      ==> Alpine (阿爾卑斯) 這字怎麼念?
如果你對 cluster 和 Swarm 有興趣可以點那板板手選擇一次產生五個機器,
三個 manager nodes 和 二個 worker nodes.
上面這黑黑的視窗就是終端機(terminal)視窗,
    以前一台終端機要十幾萬台幣時就是長這樣,    
所以幾乎所有的模擬終端機預設都是這樣烏漆嘛黑:-)
      它是模擬 X Windows 系統的終端機 xterm
請看我工作用的 PieTTY 也是長得很像!
  (PS. PieTTY 是以前台大學生拿 PuTTY 改的)
我用它連去 iottalk.vip從美國租的 VPS 機器
怎知道終端機視窗模擬什麼 terminal ?
你可以打如下命令:(env 秀出環境變數)
env | grep TER

你會發現如下答案:
TERM=xterm
或是打如下命令: (set 秀出 Shell 變數)
set | grep TERM

也是如下答案:
TERM=xterm
表示模擬 xterm 終端機 !

** 這個PWD 的終端機視窗的 Copy/Paste 和一般不一樣:
    CTRL_Insert 是 複製
    CTRL_Shift_V 是貼上

    不過一樣是用滑鼠 Mark
    另外, ALT_ENTER 可以切換全螢幕; CTRL_L 可以清除螢幕

    CTRL_= 把終端機內的字型變大 (CTRL_= 就是 CTRL_+ 因 + 和 = 同一個鍵)
    CTRL_- 把終端機內的字型變 小 (CTRL 壓著 按 - 不要多打 _ 喔 !)
o Windows people 習慣用滑鼠點點點。
o Unix/Linux people 習慣開啟終端機視窗用鍵盤打字。
👉 常見的遠端連線模擬終端機工具有 PuttyPietty
oo Putty 除了遠端連線也可幫忙挖地道(Tunnel)
  ==>How to Set Up an SSH Tunnel With PuTTY?
  ==>What is Tunnelling in Computer Networks??
=> 也可看看我以前寫給大一同學示範"挖地道"使用遠端機器上的 Jupyter(往下捲一些有圖示)。
👉 8 best alternatives to PuTTY for SSH clients on Windows

    每次開啟 PWD 網頁可以使用四小時(左上角有倒數計時)。
+ADD NEW INSTANCE 就可產生一個 Linux Docker
最多可以產生五份 Linux Docker 在同一個網段(subnet)。
(先不要問啥是 cluster 啥是 Swarm 啥是 Manager node 啥是 Worker node !)

+ADD NEW INSTANCE 產生的機器是一個跑 Alpine Linux 的 docker container;
      ==> Alpine (阿爾卑斯) 這字怎麼念?
大部份的 Unix people 編輯文件或程式會使用 vi 或 vim 編輯器,
這個生出的 Alpine Linux 已經安裝了 vim 編輯器;
但是初學 Unix/Linux 的使用者一開始不習慣 vi 或 vim 編輯器,
有個比較簡單類似Windows上的notepad記事本的 nano 編輯器。
但是,這個 Alpine Linux 預設並沒有安裝 nano 編輯器,要用需自己安裝。
不同 Linux 通常安裝套件的命令(command)不同,
Alpine Linux 用 apk add 命令, 例如要安裝 nano 編輯器要這樣:
apk add nano
你可以打 apk --help 看看如何用 apk; 參看下圖:
上面我們打 apk --help | head
其中 | head 意思是把結果 "pipe" 給 head 這命令, 因沒給 head 參數, 會只秀出前面 10 列
可以打 head --help 查看 head 的用途和用法 !
關於安裝(Install)工具或套件
前面說了,各種 Linux 版本的套件管理工具不同,
例如剛說了 Alpine Linux 用 apk 命令。
又如 Red Hat (小紅帽) Linux 以及其相關如 CentOS 和 Fedora 等
則用 rpm (代表Red hat Package Manager) 或 yum

注意 Ubuntu 上安裝套件使用命令是 apt install
當然可以打 apt --help 看看如何用 apt 這命令?
因為需要 root (super user)才可以做安裝動作,
如果不是 root 通常使用 sudo apt install
有權限使用 sudo 的使用者稱為 sudoer(s);
這時需要先安裝 sudo 套件並設定可以 sudo 的使用者(user)。
關於設定 sudoer 通常有兩種方法:
    (1)用 usermod 把 user 加入 sudo 的 group
          usermod -aG sudo tsaiwn # now tsaiwn is an sudoer
    (2)用 visudo 修改 /etc/sudoers 或在 /etc/sudoers.d/ 目錄下的任何檔案。
      例如在 /etc/sudoers.d/hahaha 這檔案內放以下這列: tsaiwn 免密碼就可 sudo
          tsaiwn ALL=(ALL) NOPASSWD : ALL
  o How To Create a Sudo User on Ubuntu(簡單)
  o How To Edit the Sudoers File(What is visudo?)(詳細)
Q: Ubuntu Linux 上套件管理安裝等到底用 apt 還是 apt-get ?
A: 用 apt 即可,你喜歡多打幾個字可用 apt-get 啊:-) (那是舊的用法 )
    不過,在 Dockerfile 內規定要用 apt-get 就是啦!

以下是我已經 docker pull 好幾個不同 Linux 的 docker image;
    (docker pull 意思是從 docker 倉庫把 docker image 抓下來)    
你可以看到 Alpine 最小, 只有 5.59MB; (其實最早期的 Linux 不到 1MB 可放軟碟片)

TOP
  ToC (Table of Contents)
o 也可在 Windows 或 Mac 先裝 Virtual Box 虛擬機再把 Ubuntu 安裝在 虛擬機
(0) Docker 常用命令   docker images; docker ps -a; docker run; ...
   oo 簡單Dockerfile範例示範一兩分鐘做出一個 docker image
(1) Unix / Linux 簡介 (Linux / Ubuntu / GNU / BSD)   ( Unix 發展歷史 )
     => Unix people 強調的 KISS 原則(keep It Simple, Stupid)
(2) Unix File system structure 各目錄放哪些東東? ( man 7 hier )
      ( 也會解釋 點(.) 和 點點(..) 以及 點(.) 開頭的檔案是啥? )
(3) Unix/Linux 的Shell(殼) 與 Command (命令)
      ( 簡介 sh/csh/ksh/tcsh/bash/dash 與 命令路徑(PATH) )
   為何?為何 Unix people 說不要把 . (目前目錄)放入命令路徑內?

  oo
Shell 對打入命令時 的 處理流程(alias、內建命令、外部命令;命令路徑)
  oo Shell變數、環境變數(Environment variable)
  oo Shell 啟動時和結束時會偷做一些檔案內的命令 (.profile, .*rc, .logout, ...)
(4) Program(程式) / Process(行程、進程) / Thread(執行緒)/ Daemon(魔鬼、守護程式)
(5) Unix 常用命令 (讓你照著打入練習)
   o> 也可看看以前我整理給大一同學的 21 個 Unix 常用命令
  .abc. Unix 常用與次常用命令 63個 - 照字母排列
(6) 👉 點這看 Linux 的線上手冊(Manual pages)(建議先看各章節介紹)
      man 1 intro   man 2 intro   man 3 intro   man 4 intro   man 5 intro   man 6 intro   man 7 intro   man 8 intro
(7) Q & A 以及關於建立虛擬隔離環境(venv) for IoTtalk FarmDashboard
    => Linux Containers on Windows(docker 官方文件)
(8) 👉 Super user (root) 的 Q & A (有和root一樣特權者稱為 super user)
      就是 Super user(system administrator) 應該要懂的 :-)    

    ※ ?? apt install 和 pip install 有何不同 ?
    ※ 👉=> 90 Linux Commands frequently used by Linux Sysadmins
  ※※ 在 Ubuntu 16.04 上先安裝必要工具 (包括 Python 3.6 或 3.7)
          ( for IoTtalk FarmDashboard and/or IoTtalk )
  ※※ 在 Ubuntu 16.04 上安裝 Python 3.7(from Source code) (need gcc)
      (因為 Ubuntu 16.04 用 apt install 安裝 Python 是 3.5 版)
(9) List of Linux distributions(維基百科)
    => Top 5 Most Stable Linux Distributions in 2022
    (Debian, Linux Mint, Ubuntu, Fedora, OpenSUSE)
    => Debian vs Ubuntu: What’s the Difference?(Ubuntu is based on Debian)
    => 50 Best Linux Distros for 2022(Ubuntu, Fedora, CentOS-stream, ...)
###
(※) Compare KVM and VNC Connect
    => What is KVM (Kernel-based Virtual Machine)?
    => What is a VNC (Virtual Network Computing)?(類似微軟遠端桌面、PcAnywhere)
    => How to Install and Configure VNC on Ubuntu 20.04
    => How to Install KVM on Ubuntu 20.04
    => How To Install AnyDesk on Ubuntu 22.04|20.04|18.04
    => TigerVNC on Github
    => AnyDesk vs. TigerVNC vs. TeamViewer
(※) Set Up a VPN Server With Docker In 5 Minutes
    => [VPN] VPN Client介紹(Asus VPN 相關主題)
    => 如何在華碩路由器設定VPN用戶端?
    => 如何在Windows上使用 IPSec VPN 連線(華碩+Shrew Soft)
    => 如何透過 Windows 電腦連線至 Synology VPN Server?
(👉) 👉 👉 想要學 Python 語言?點這看我寫的 Python + Flask 快速入門講義
  =>How To Serve Flask Applications with uWSGI and Nginx on Ubuntu
      (官方建議 Flask 網站開發時用 Python 但正式產品建議用 uwsgi 跑)
=> What is Open Source Software Licenses GPL v2?(GPL v3?)
👉👉 5個用起來很像 Windows 的 Linux 版本(桌面很像:-)
👉👉 Make Linux Look Like Windows 10 With These Tips and Tweaks(桌面很像)
👉👉 How to Make Linux Look Like Windows 11(customize KDE)

👉 關於 Unix 的發展歷史,可以點這看維基百科關於 Unix 的說明
👉 關於 LinuxLinux distribution 以及 List of Linux distribution
👉 Linux Kernel 原作者 Linus Torvalds(Wikipedia)
👉 關於 Kernel (operating system)Linux Kernel(Wikipedia)
👉 關於 MicroKernel (operating system)(Wikipedia)
👉 談 Microkernel(2021 iThome 鐵人賽)
👉 Could Linux Become A Microkernel?(Linux 已太肥怎可能是 Microkernel?)
👉 關於 BSD(Berkeley) 與 FreeBSD 以及 List of BSD operating systems
👉 關於 POSIX (Portable Operating System Interface)(Wikipedia)
👉 聽說 Microsoft Authenticator 比 Google Authenticator 好且安全?(2FA)
#####

很多應用需要用到資料庫(Database),例如 MySql, SQLite, MariaDB, PostgreSQL, ...
關於 MySql 與其安全設定 + ...
在 MySql 建立 (create) new user for your application
與 mysql 有關的一些常用 Linux 命令
常用的 SQL commands
關於 IDF on IoTtalk ...
o 如何在 IoTtalk server 增加/修改 Device Model 物聯網設備類別 ? (new Window)
使用 Python SQLAlchemy ORM, 可以選用 MySQL 或 SQLite
關於多語系(i18n)使用說明 for IoTtalk FarmDashboard
=> Install Docker Engine on Ubuntu(dockerDocs)
=> How to Install Docker On Ubuntu 18.04
=> Play with Docker Classroom training.play-with-docker.com/
o password 管理:Introduction to LastPass    and ...  
o 7 Best LastPass Alternatives for 2022    and ...  
o 23 Best Password Manager Tools for 2022 
o Python 官方網站:https://www.python.org/      Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive
o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹
(1)華碩官網無線路由器設定VPN (可以支援八個人 VPN 連線)
   https://www.asus.com/tw/support/FAQ/1044190
(2)Client 端用第三方免費的: (電腦手機client都用第三方的)
   https://www.asus.com/tw/support/FAQ/1033576/
工作站(Workstaton) vs. 伺服器(Server)
    其實兩者的電腦幾乎一樣,雖然當伺服器的電腦通常比較貴,但也有把便宜電腦拿來當伺服器的。
伺服器通常不關機,工作站則通常下班時(或想休息時)就把它關機以便節省電費。
還有,雖然大部份的伺服器用 Linux/Unix 系統,但也有用微軟的Windows Server系統。
    那這樣工作站和伺服器兩者到底有啥差別?
簡單說,當伺服器的電腦通常平常不必接螢幕,甚至根本不知道機器在哪裡,
系統管理員通常開啟終端機程式用遠端連線連進去管理提供服務(Service)的程式,所以通常也不必安裝桌面(Desktop)GUI環境。
    至於工作站,顧名思義就是你拿它來工作,所以你可能開很多視窗,
包括瀏覽器或是編輯各種文件甚至開啟終端機視窗連線到其它的伺服器。
雖然有些討厭微軟的人堅持安裝 Linux/Unix 並安裝桌面GUI環境當工作站,
但絕大多數的使用者都使用微軟的 Windows系統。
    蘋果Macbook 的 macOS 其實是 Unix 的一種,是根據 BSD (柏克萊大學改過的 Unix)和 Mach (CMU 大學改過的Unix) 混合而成。
不過,有些人買了 Mac Book 卻安裝 Windows 或安裝雙系統。



Linux Shell 命令(command)初體驗
啥是Shell 等下會解釋
馬上來體驗使用這 PWD 的 Linux . . . Alpine Linux ..
(1)點 +ADD NEW INSTANCE 生出一個跑 Alpine Linux 的機器
    (Linux / Unix 雖然版本眾多, 但其命令約有 98% 是相同的 !)
(2)在中間黑色終端機命令窗中依序輸入以下 Unix / Linux 命令:
cat /etc/os-release  #  看看這是啥系統?
pwd    # 看看我在哪個目錄 Print Working Directory
touch you  # 生出 you 這檔案
you   # 看看電腦是否認識 you 這命令; 現在顯然不認識 :-)
./you  # 在 Unix 習慣上目前目錄(.) 不在命令路徑path內, ./you 表示執行目前目錄下的 you
### 但 you 必須是可執行的檔案, 當然內容也要有命令腳本或程式機器碼
echo $PATH   # 查看命令路徑, 注意 PATH 大寫; Windows 的 CMD 打 path 命令即可看
### 可用 nano 或 vim 編輯器修改 you 內容
### nano 最簡單, 只要知道 CTRL_S 存檔, CTRL_X 離開; vi 和 vim 則是 Unix people 的最愛:-)
### 也可用畫面上命令窗上方的 EDITOR (只可以編輯 /root 之下的檔案 (含子目錄) )
 ## 點 EDITOR, 彈出一個編輯窗, 可看到 /root 之下有 you 這檔案(剛才 touch you 生出的)
 ## 點 you 就會開啟 you 讓你編輯, 輸入 echo I love you 你你你
 ## 記得點左方的 Save 存檔; 必要時點 Reload 看看(若沒存檔就按 Reload 就白打了喔!)
### 接著回中間終端機命令窗繼續測試 Unix / Linux 命令
source you   # 執行腳本 you
.  you   #  same as above line
chmod +x you   # 讓 you 變成可執行檔 (eXecutable)
./you   # 執行目前目錄的 you ;; 注意打 you 仍不行是因 Unix 預設命令路徑不含 . 目前目錄
###$$$ Windows 則是永遠把 . 目前目錄當作命令路徑的第一個!!!
  ### Unix/Linux people 則建議永遠不要把 . 放入命令路徑!因為很危險!
也可以開啟 Windows CMD 或 Power Shell 等窗用 ssh 連進去使用剛才生出的 Alpine Linux:
當然你的 Windows 要有安裝 ssh 連線工具! 參看以下 (5) 列的參考網站
(1)點 視窗中 提示的 ssh 網址右邊複製按鈕
(2)到 CMD 命令窗中按滑鼠右鍵
(3)選 貼上
(4)按 ENTER 鍵
(5)如果不能登入或沒有 ssh 請參考以下這三篇
  o Generating an SSH Key on Windows(我用 Git 的 Git-GUI 點 HELP 來產生 key)
  o How To Install And Use The Windows 10 SSH Feature
  o Installing SFTP/SSH Server on Windows using OpenSSH

按下 ENTER 鍵後會問密碼,
這是指你在 Windows 用 Putty 或 Git 的 Git-GUI 產生金鑰對(Key pair)時你輸入的密碼!
TOP ToC
Shell(殼)就是命令解譯程式 (Command Interpreter)
  Shell(殼) 與 Command (命令)
在終端機(terminal)視窗負責接收我們打的命令並根據狀況執行的程式叫做 Shell(殼),  
其正式的名稱叫做命令解譯程式 (Command Interpreter)
因為它接收我們命令後會想辦法執行該命令(等下說明)
Windows 的 CMD 和 PowerShell 也都是 Shell,甚至桌面管理程式也是 Shell (GUI Shell)。
上面顯示的是我在 Windows 上開啟的 CMD 命令提示字元和 PowerShell 視窗。
Unix/Linux 上常見的 Shell 如下:
sh   --   Unix 第二代的 Shell,就是 /bin/sh 這檔案
      sh 又稱 Bourne shell;是AT&T公司 Stephen Bourne寫的;
     (第一代是 Ken Thompson 寫的也叫 sh)
csh   --   C SHell,語法很像 C 語言,加州大學研究生 Bill Joy 寫的隨 BSD 2.0發佈。
tcsh   --   C SHell 的改良版本,目前為FreeBSD和其延伸發行版的預設shell。
ksh   --  是AT&T公司的 David Korn 寫的,在AT&T公司廣受歡迎。
dash   --   Debian Almquist shell (Debian 是 Ubuntu 所屬的公司)
          # Debian 公司把 sh 指到 dash;
          # 它由 NetBSD版本的Almquist shell (ash)改版來的。
bash   --   Bourne Again SHell (bash)現在最常用的預設shell,
          bash 的作者是Brian Fox,不是原先的Bourne。
Shell script file (shell 腳本檔)
    Shell 也算一種程式語言,腳本式程式語言(Script language);
我們可以把很多命令搭配 if, for, while 寫在一個檔案讓 Shell 批次(batch)執行,
這檔案叫做 shell script(腳本),又稱批次檔(batch file)。
shell 腳本檔內用 # 當註解的開頭
    各種Shell語法略有不同,但腳本檔(script file)一律用#當註解開頭,
要注意的是檔案第一列註解如果類似如下:
#!/bin/csh
    則表示該腳本(script)要用 /bin/csh 來解譯,表示該檔案用 csh 語法寫的!
如果第一列不是用 #! 開頭則用目前使用中的 Shell 來解譯
執行腳本例如 mycmds 這文字檔的方法有三種:
source mycmds     # 不產生 子 shell; 打 . 取代 source 是一樣的所以算同一種 :-)    
.  mycmds   # same as above (注意 . 後面至少空一格 !)
bash mycmds  # 當然這會啟動新 shell "bash" 
./mycmds   # 必須 eXecutable, 可用 chmod +x mycmds 讓它變 eXecutable  
# 還有第四種:
exec mycmds ## 不懂不要用 ! 不要用 ! 不要用 ! 因為這會把目前的 shell 幹掉!!
## 所以這第四種不算 :-)

腳本檔案沒規定副檔名, 所以 .csh 不保證用 csh 執行 !!
一般只是建議儘量符合實際狀況, 通常都用 .sh 代表一切shell腳本 :-)

  .bashrc / .cshrc / .login / .profile / .logout ...
    再往下捲一些說明這些 .開頭幾個檔案用途 ! !
TOP ToC
Shell 對打入命令時 的 處理流程
Shell(殼; Command Interpreter)如何處理我們打入的命令?
  alias, 內建命令、外部命令;命令路徑(PATH);| (pipe)
內建(builtin)命令 -- Shell 程式內就能做的 ## IJKLMNOP ijklmnop 54321000    
外部命令 -- 腳本批次檔案或機器碼執行檔,依據命令路徑 (PATH)尋找外部命令檔案。
命令路徑(PATH) -- 環境變數,用來照順序在各目錄尋找外部命令檔案。
echo $PATH
你會看到類似如下答案:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
當你在 Shell 打一個命令時 Shell 的處理流程:
(1)先查看是否有在 alias 列表內 (但用 \ 反斜線開頭者不查 alias)
(2)查看是否為 Shell 的內建命令 (builtin command)
    man bash-builtins # 查看 有哪些 shell 內建命令以及用途用法 (系統要有安裝 manual !)
(3)若不在內建命令,依據 $PATH 命令路徑搜尋是否有與該命令同名的檔案
    (當然檔案必須是 eXecutable, 可打 chmod --help 查看說明。)
註: Windows 找外部命令固定先找目前目錄(.),找不到再到命令路徑尋找。
      Windows 的命令路徑可以打 path 命令查看; 路徑內其實不含目前目錄(.)。
      提醒 Unix people 強烈建議不要把目前目錄(.)放入命令路徑。
      因為那樣會很危險

ls -l /bin | grep "sh" # list all files in /bin 然後 傳給 grep 命令過濾
ls -al /bin | wc -l # 看看 /bin 之下 共有幾個檔案; -a 表示包括 . 開頭的檔案

各命令解譯程式 (Command Interpreter)的命令和語法略有不同 !
除非特別說明,否則這裡我們原則上以 bash 為主。
前面說了,有些命令是 Shell 內建(builtin)的,有些是系統內例如 /bin/* 等預先安裝好的檔案。    
bash 的內建命令有哪些可以打 man bash-builtins 查看。(要有安裝 manual pages)
最基本的外部命令通常 一開始就被放在 /bin/usr/bin 目錄內。
ls -l /bin | more # 慢慢看 /bin 底下有哪些東東 !
which ls # ls 這命令放哪個目錄 ?
which alias # alias 是 shell 內建命令; 所以不會回答, 有些 shell 會告訴你這是內建命令 !!
type which # 結果 類似 which which
## 注意微軟 Windows 的 type finename 類似 Unix/Linux 的 cat filename
type alias # 會告訴你 alias 是 bash 的內建命令!
man bash-builtins # 查看 bash 的內建命令有哪些(要有安裝 manual pages)
which rm # 殺檔案建議用 \rm -i 檔案
alias rm='\rm -i' # 這樣以後 rm filename 會先問你確定嗎???

有些命令如 alias 和 unalias 是Shell內建命令, ls 和 rm 是在 /bin 底下的程式,
另外有些命令須要系統管理者另外安裝,若遇到系統說不認識該命令,就:
sudo apt update # super user 或 root 不必打 sudo
sudo apt install 該命令 # 不過有時可能不是這樣直接,例如 ping 要 apt install iputils-ping    
## 善用 google 搜尋 :-)

通常系統管理者安裝的程式會被放到 /usr/local/bin 裡面。
o 如果是程式庫(Library),系統預置的當然放 /lib 和 /usr/lib 裡面,
系統管理者安裝的會被到 /usr/local/lib 這目錄裡面。
TOP ToC
環境變數 可以傳給 子 shell,Shell 變數 只在目前這 shell 有效。
  Shell 變數 與 環境變數(Environment variable)
Shell 變數 -- 只在目前這 shell 有效
環境變數 -- 可以傳給 子 shell
    在 Shell 中打的命令如果就是 shell 程式(通常在 /bin 之下以 *sh結尾)會進入子 shell,    
    這時環境變數會傳進去子 shell, 但原先 Shell 的 shell 變數不會傳進去。
set     # set 列出 shell 變數
printenv     # 列出 環境 變數
set | head # head 預設等於 head -10 會列出前面 10 列
env | head -15 # 前面 15 列的 env 結果
GGYY="Hello 12345"   # 設定 Shell 變數 GGYY 值為 "Hello 12345"  
echo $GGYY
set | grep GG   #   set 列出 shell 變數 然後交給 grep 過濾只含 "GG" 的              
printenv | grep GG # printenv 列出 環境變數 然後 pipe 給 grep 過濾出含 GG 的列
bash # 產生子 shell
echo $GGYY # 在 子 shell 內不認識 GGYY
exit #離開 子 shell 回到原先的 shell
echo $GGYY # 又認識了
export GGYY # 讓 GGYY 變成也是 環境變數
printenv | grep GG # printenv 列出 環境變數
bash # 產生子 shell
echo $GGYY # 在 子 shell 現在會認識 GGYY 因剛剛 export
exit #離開 子 shell 回到原先的 shell; 若是最後的 Shell 則登出(Logout)終端機


## printenv 只能"print"出環境變數
## env 可用在執行命令時暫時改變某些環境變數(可多個)
env EGG="my_value" command_to_run command_options
#setenv (某些 shell 用 setenv 列出或設定環境變數)

有些 Shell 習慣上環境變數用大寫字母,Shell變數用小寫字母。
bash ggyy.sh # 啟動新的子 Shell 處理 ggyy.sh
source ggyy.sh #在原來的 Shell 處理 ggyy.sh 這 Script
TOP ToC
 
  Shell 啟動時和結束時會偷做一些檔案內的命令
  .bashrc / .cshrc / .login / .profile / .logout ...
通常在系統中所有使用者啟動Shell都會先執行系統的 .profile 或 .login 或 .*rc檔案;
請注意,每一個Shell版本放置這些檔案的地方不大相同,通常就/etc 底下;
然後再執行 user Home directory 的 .profile 或 .login 和 .*shrc 等。
sh
Shell 啟動 (依照所列順序):
/etc/profile - login shell 才會執行
# 以下通常在 User 的  Home Directory
.profile - login shell 才會執行
Shell結束之前:
  any command (or script) specified using the command: trap "command" 0

csh
啟動 (依照所列順序):
# 以下通常在 User 的  Home Directory
.cshrc - 每次都會執行
.login - login shell 才會執行
結束之前:
.logout - login shells.
其他:
.history - 儲存 history (根據 $savehist 的值決定大小).

tcsh
Shell啟動 (依照所列順序):
/etc/csh.cshrc - 每次啟動都會執行
/etc/csh.login - login shells 才會執行
# 以下通常在 User 的  Home Directory
.tcshrc - 每次啟動都會執行
.cshrc - 假如沒有 .tcshrc 在的話
.login - login shells 才會執行
Shell結束之前:
.logout - login shells 才會執行
其他:
.history - saves the history (based on $savehist).
.cshdirs - saves the directory stack.

ksh
Shell啟動 (依照所列順序):
/etc/profile - login shells 才會執行
# 以下通常在 User 的  Home Directory
.profile - login shells 才會執行
$ENV - always, if it is set.
Shell 結束之前:
如果你用了 trap "command" 0,則結束時會去執行 "command"。

bash Shell 啟動 (依照所列順序): /etc/profile - Login shell 才會執行, 且會 source(.) /etc/bash.bashrc /etc/bash.bashrc # 不是 Login , 但 Interactive 會先執行這 # 以下通常在 User 的 Home Directory .bash_profile - login shell 才會執行 .profile - 如果沒有 .bash_profile,則會在 login 時執行 .bashrc - interactive shell 會執行 # 就是說, interactive 但不是 Login 的那個 shell 會跳過 .bash_profile 和 .profile $ENV - 若有設定則讀取之 Shell 結束之前: .bash_logout - login shell 結束時執行這 其他: .history - saves the history (based on $savehist). .inputrc - Readline 初始化之時 # 參看 bind --help
啥? 你要用 Ubuntu Linux 系統? 簡單, 在 PWD 的終端機視窗打這樣:
docker run -it ubuntu  
#這樣會從 hub.docker.com 抓最新版本 ubuntu 的 docker image 下來跑成 container! #這container就是個剛灌了 Ubuntu 系統的機器了, 當然很多工具都沒安裝! #你可以拿它來練習安裝設定系統(練習當系統管理者:-) #你也可以改用這樣抓我已經安裝了一些工具的 Ubuntu 16.04 版本:
docker run --name ggyy -it tsaiwn/tsaiwn:16  
#給 --name ggyy 的好處是不用查看 cid (container id)就可用 ggyy 代表該 container #因為這個 tsaiwn/tsaiwn:16 裡面我有把 ssh service 跑起來! #且裡面我建立了 tsaiwn 帳號, 密碼 1234567 #此時在 Ubuntu 的 bash, 可以打 hostname -i 看 IP #然後暫時離開 container (把它踢到背景)就可測試 ssh 連入該機器。 #要暫時離開 container 回到 Alpine Linux 可以敲 CTRL_P CTRLQ #回到 Alpine Linux 可以用以 docker attach ggyy "接" 回該 container #或用 docker exec -it ggyy bash "進入" 該 container 執行 bash #剛剛說了因為我在 tsaiwn/tsaiwn:16 有把 ssh server 跑起來且有建立 tsaiwn 帳號, #所以回到 Alpine Linux 也可以用以下連線進去該 Ubuntu 系統: ssh tsaiwn@172.17.0.2 # (通常第一個跑的 container 用這 IP, 實際看 hostname -i ) #以下畫面是我故意修改了 /etc/profile/etc/bash.bashrc 以及 ## 以及 ~tsaiwn/.profile~tsaiwn/.bashrc #在裡面 echo 一些訊息 ## 這樣就可以測試 Login 時到底會執行哪些 .*rc 檔案 (系統的在 /etc/, 再 ~user/ 的) ## 如果已經 Login 了在 Shell 打 bash ## 則會啟動 sub shell 此時因不是 Login shell 所以不會執行 profile (看以下畫面) ## 你可以自己測試看看 :-) ### 要回到剛才 CTRL_P CTRL_Q 離開的 container ### 如果 docker run 忘了指定 --name 那就需要先找出 cid 才有辦法 docker attach ### 先打 docker ps -al 看看該 container 的 ID # l 表示 Latest or Last ### 然後打 docker attach cid # cid 是該 container 的 ID (打前兩三個字即可) ### 注意 docker exec 和 docker attach 不太一樣; ### docker attach cid 是"接回"背景的 container, 這時如做"結束"動作會讓 container 結束。 ### docker exec -it cid bash 是背景的 container 仍在背景跑,我們另外跑進去執行 bash ### 這種方式進入後若 bash 結束;在背景的 container 並沒有結束 !
上面圖片中 in etc/profile 是指在 /etc/profile (我偷懶沒打全部:-) 那個 in bash.bashrc 是指在 /etc/bash.bashrc (我偷懶沒打全部:-) 還有 in tsaiwn/.profile 是指在 ~tsaiwn/.profile 也就是 /home/tsaiwn/.profile
TOP ToC      
  關於 Shell 的一些小常識(tips) -- 提高你的工作效率 !  
Q:如何知道目前使用的 Shell ? A:目前的 Shell 記在 SHELL 這變數內: echo $SHELL Q:終端機螢幕很亂如何清除? A:用 clear (Windows 的 CMD 窗是用 cls ) 另外一個密技是敲 CTRL_L 清除螢幕,命令打一半時也可用喔,馬上試驗看看! (如果 CTRL_L 沒效請參看 bind --help ) Q:我打了一列很長的命令中間打錯一個字重打可能又打錯有好辦法嗎? A:善用上下左右鍵和 CTRL_A 與 CTRL_E 以及 Backspace 鍵 o 敲往上鍵 叫出之前打的命令(萬一多敲一下就敲往下鍵一下) o 敲左右鍵或 CTRL_B 和 CTRL_F 移動游標(cursor) o 敲Backspace 鍵 會把游標左邊的字砍掉 o 敲CTRL_A 跑到最左邊 o 敲CTRL_E 跑到最右邊 o 但是當整列正確時直接敲ENTER 鍵 即可,不必把游標移到最右邊。 o 如果想重複用大約十幾次甚至數十次之前的命令想改一兩個字呢?試以下: history # 調出以前的命令記錄 history | more # 調出以前的命令記錄並慢慢看 (按 ENTER 或空白鍵) history | grep vim # 調出以前的命令記錄過濾出含有 vim 的 !56 # 立即重複執行以前編號 56 的命令(history 看到的編號) !56:p # 調以前編號 56 的命令變成最新命令但不要執行以便按往上鍵來修改 o 如果命令只是打錯第一個單字其它後面都正確,例如: dockee run -it ubuntu:18.04 # docker 打成 dockee 那立即打如下看看: docker !* # !* 表示上列命令中去除第一個空白之左的單字(word)剩下全部 docker !*:p # :p 表示不要執行 Q:如何暫停目前正在執行的程式先讓出終端機讓我打命令然後繼續該程式? A:敲 CTRL_Z (CTRL 壓著敲 z 鍵)可把程式踢到背景(background)執行; 之後可以打 fg 把它調回前景 (foreground),參考: fg --help jobs --help fg 2 # 把編號 2 的背景工作調回前景 (attach 到終端機) 另外,如果執行時命令最後是打 & 再按下 ENTER 鍵則程式立即跑到背景。 Q:如何立即停止(殺死)目前正在執行的程式 A:敲 CTRL_C (CTRL 壓著敲 c 鍵)可把程式立即中止。 萬一碰到有些程式故意寫成不理會 CTRL_C 則須先敲 CTRL_Z 踢到背景, 然後用 jobs 查看其編號後用 kill -9 %編號 砍死它(注意 %Job的編號沒打 % 會被當 PID) 或用 ps 查看其 PID 後用 kill -9 PID 砍死它(注意這時 PID 左邊沒 % 號) Q: 如何改變命令提示(prompt)? A: 命令提示(prompt) 記錄在 PS1 這變數(PS後是123的1): echo $PS1 ggyy=$PS1 # 先記住 PS1 內容 PS1="Yes> " whoami PS1='$(whoami)@$(hostname -i)> ' ## 用單引號 !!! PS1='\e[1m\e[31m\u\e[32m@$(hostname -i)# \e[0m' ## \e[0m 恢復正常 pwd PS1='\e[1m\e[31m\w\n\u\e[32m@$(hostname -i)# \e[0m' cd /home cd /usr/bin ls -l *sh PS1='\e[1m\e[31m$(whoami)\e[32m@$(hostname -i)\n$(echo $PWD)# \e[0m' whoami PS1='\e[1m\e[31m\u\e[32m@$(hostname -i)\n\w# \e[0m' cd /usr/local cd /usr/local/bin PS1='\e[1m\e[31m\u\e[32m@$(hostname -i)\n\W# \e[0m' # \W 大寫 pwd PS1=$ggyy # 恢復成以前的 prompt (我們前面用 ggyy=$PS1 記住的)
Q: cd 點點(cd ..) 和 cd 減號(cd -) 以及 cd ~ (波浪號、蚯蚓號) 各有何用?
A: cd .. 是 cd 到目前目錄的上一層目錄, cd - 是到上次使用的目錄,
   如果在兩個目錄切換來切換去時 cd - 特別好用。
   cd - 也可搭配 pushd 命令, 不過 pushd 之後通常用 popd 回到剛剛的目錄。
pushd /usr/local/bin # 記住目前目錄(push 到堆疊)並跳到 /usr/local/bin
popd  # 回到剛剛的目錄 (pop 出來給 cd 用,是用 Stack 先進後出來記住)
   至於 cd ~ 則是跳到目前使用者(user)的家目錄 (home directory)。
   通常 user ggyy 的家目錄是 /home/ggyy 但是 root 的家目錄是 /root 這目錄。
   請注意 / 我們稱為 root directory, 就是整個硬碟最開始最上層!
   至於 /root 不要稱 root directory, 它是 root 這 super user 的 HOME directory家目錄。
cd ~tsaiwn  # 到tsaiwn的HOME目錄 即 /home/tsaiwn

以上是關於 Shell 的一些小常識
 

TOP ToC
Program(程式) / Process(行程、進程) / Thread(執行緒)          
程式(Program)就是含有給電腦CPU可執行指令的檔案,人看得懂的叫做原始碼(Source code;有些人翻譯為代碼),
人看不懂但CPU看得懂的叫做機器碼(machine code)。
  o 躺在磁碟內叫做程式(Program),跑起來(執行起來)變成 Process,所以 Process 就是執行中的程式。
    一般把 process 翻譯為行程,老共則翻譯為進程。
  o 躺在磁碟內叫做docker image,跑起來(執行起來)變成 docker container。
  o docker rm cid # 砍掉 docker container # 若還在跑可以 docker kill cid 先砍死它
  o docker rmi iid_or_imageName # 砍掉 docker image (iid 只要打開頭兩三個字即可)
  o docker image ls # 秀出有哪些 docker image # 同 docker images
  o docker container ls # 秀出有哪些 docker container 可看到 cid
  o docker ps -a # 秀出有哪些 docker container 可看到 cid (包括已經執行結束的)
    ### 已經執行結束的 才可以 docker rm  cid 砍了它;
    ### 還執行中的要先 docker kill cid 砍死它:-) 或 ..
    ### 或 先 docker attach cid 回去敲 CTRL_C 然後打 exit 結束該 container !
    ## 所以,通常先用 docker ps -a看看有哪些 container;
    ## 然後必要時用 docker kill cid 砍掉還執行中的 Container;
    ## 接著用 docker rm  cid 砍掉已死的(結束 exit 的) Container;
    ## 再來用 docker images 看看有哪些 docker image ?
    ## 最後才用 docker rmi  iid_or_imageName 砍了不要的 docker image ?

  o 一個Program可以同時執行多次產生多個Process行程。
  o 一個Process可以有多個Thread(執行緒)。
同一個Process內的Thread使用相同的Memory Space,但這些Thread各自擁有其Stack(堆疊)。
各Thread透過reference可存取到相同的object,但各Thread 的局部變數(Local variable)各自獨立。
                                                     ..
Daemon (魔鬼) 是服務程式(Service program)
Daemon Program(守護程式) / Daemon Process(守護行程) / Client(客戶端)          
守護程式(Daemon Program)是指執行起來可以提供某種服務(service)的程式,
而守護行程(Daemon Process, 老共翻譯為守護進程)就是執行起來的守護程式(Daemon Program),
通常用 service 服務名 startsystem start 服務名啟動。
這種程式因為不需要和人互動,所以通常躲在背景(background)執行,
  (像 Shell 與人互動的程式會佔用終端機則稱在前景(foreground)執行。)
  (在終端機執行命令時如果結尾打 & 則程式會在背景執行,不佔用終端機,
    此時打 fg 可以把它調回前景,當然又會佔住終端機,這時可敲 CTRL_Z 把它踢回背景。)
    (打 help fghelp jobs 看看。)

守護行程(Daemon Process, 守護進程) 通常用來提供某種服務(Service),
例如以前讓很多人玩 BBS 的 bbsd, 讓大家上傳或下載檔案的 ftpd, 連線用的 sshd 或 telnetd 等。
注意到了吧?結尾都是 d, 這只是習慣用法,沒有強制規定Daemon Program必須用 d 結尾。
既然守護行程(Daemon Process, 守護進程)通常是提供某種服務(Service),
    有些文件也稱之為 server process (伺服程序、伺服行程);
    所以在Windows中微軟就稱它為服務(Service)程序。
o 與守護程式(Daemon Program)相對應的是該服務的 Client (客戶端程式),
    對啦,它們當然是 Client-Server 的關係。
例如安全連線的 ssh 是提供 ssh 安全連線服務之 sshd 的 Client 端。
## apt update
## apt install openssh-server   # ssh 和 sshd 在這套件裡面
## service ssh start   # 把 sshd 跑起來 (注意不是 service sshd start 喔!)
## systemctl start ssh   # 另外一種用法, 注意 systemctl 先寫動詞再寫 service name
##  但是你可能要另外用 apt 安裝 systemctl    (相對於 service 比較新的命令)
## service ssh status  # 看看 ssh 服務(sshd)的狀態
## PS1='\e[1m\e[31m\u\e[32m@$(hostname -i):\w# \e[0m'    # \w 表示 working directory
## PS1='\e[1m\e[31m\u\e[32m@$(hostname -i):\e[31m\w# \e[0m'   # \e[31m  是紅色
## PS1='\e[1m\e[31m\u\e[32m@$(hostname -i):\e[33m\w# \e[0m'   # \e[33m  是黃色

  Daemon (好魔鬼) vs. Demon(壞魔鬼、惡魔)            
Daemon 和 Demon 發音相同,讀音如 "滴們" /diː.mən/ 且意思都是 "魔鬼"。
oo 點這看看 Daemon 在 劍橋字典怎模念. (好魔鬼有 A )
oo 點這看看 Demon 在 劍橋字典怎模念. (音同意思也相近!)
所以,daemon 不該翻譯為惡魔,要翻譯為魔鬼,
且是指心懷善意的好魔鬼,總不能說"好的"惡魔 (都有一個'惡'了怎會有"好的"呢?)

還有,有些人說daemon要念如"Day-Mon" 也不對,
因為 daemon 該字本來就要念"滴們", a 不發音。
* 至於 demon 翻譯為惡魔是剛剛好,
因為 demon 是指壞的魔鬼(evil spirit, 邪靈)。
請注意,電腦程式的 daemon 不要寫成 demon,
因為 daemon process 是守護程序(行程、進程)。
每個人都有一個 ID, 每個男人都有十二粒, 因為隱藏十粒(實力)(聰明的人才看得到:-)
    每個 process 也都有一個 ID 稱作 PID, 該 process 的母親的 ID 稱作 PPID(Parent Process ID).

o PID 為 0 的其實是 kernel 內的特殊排程程式 Scheduler,不是一般的 process(行程)。
o PID 為 1 的是開機(boot)過程中第一支 process, 以前是 init, 現在是 systemd
  (Why ‘init’ Needed to be Replaced with ‘systemd’ in Linux)
o 但是,但是 Docker container 的"開機"並不是走 "init" 程序,
  所以你會發現用 docker run 跑的 container 內 PID 1 是你啟動的程式(通常是 bash)。
  請注意 docker container 並不是完整的 Operating System(作業系統)。

o PID 為 2 的是 kthreadd 顧名思義就是 kernel thread 的 daemon, 當然 docker container 不會有這個東東。
o systemd (或 init) 的 PPID 是 0 表示沒有"母親"(或說父親),剛剛說了這是boot開機第一支 process.
o 所有的 daemon (除了 kthreadd) 的 PPID 都是 1, 就是說它們的母親都是 systemd (或 init)。
    ( kthreadd 的 PID 是2, 但它的 PPID 也是 0, 表示沒有 parent process)
o 因為 daemon 被啟動(start)時,會先分身(fork)生出 child process,
  然後原先的 parent process會自殺(exit),這使得 child process 變成孤兒(orphan),
  依據規定所有的孤兒(orphan process) 會被 PID 為 1 的收養,所以其 PPID 才會是 1 的行程(process)。
oo 請注意 孤兒(orphan process)與殭屍(Zombie process)不同,
      Zombie process是指 child process 已經結束(exit)在等待 parent process 的接受或認可,但卻一直等不到。

=> 關於 為何 docker container 沒有 systemd, 可以點這看 Docker and systemd
=> 也可點這看 Running systemd inside a docker container
o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹
TOP ToC
Docker 的目的和 Virtual Machine 不同
Docker 的用法通常是把應用程式(Application)打包成 docker image file 方便給使用者用!
先來測試我用 docker commit 弄出的 tsaiwn/haha 這 image
對啦,就用 PWD (Play With Docker)的網頁即可!
以下先點 +ADD NEW INSTANCE 開個新的機器,然後執行我打包好的一個網站服務:
docker run --name haha -dtp 8080:5678 -p 5566:5678 tsaiwn/haha
### 注意 tsaiwn/haha 要 -dtp 且 port 是 :5678
下載 tsaiwn/haha 完畢跑起來後,你會發現 Alpine Linux 的終端機仍可使用;
可以打 docker ps -a 查看有哪些 container ? (docker image 跑起來就叫 docker container)。
這時可以再跑別的 docker image,也可以用docker exec 進入被踢到背景執行中的 container,如下:
docker exec -it  haha  bash  # 進入剛才跑在背景的 tsaiwn/haha ( --name haha)
#如果 docker run 沒打 --name haha 則要找出 container ID 來用在 docker exec
# 找出container ID 的方法可用 docker ps -al 或用 docker container ls
docker ps -a   # 列出全部 container, 包括已經結束(exit)的
docker container ls   # 只列出還在跑的 container
docker image ls   # 列出目前有哪些 docker image
docker images   # 也是列出目前有哪些 docker image

等 tsaiwn/haha 這image被 pull 下來 run 成 container 後,
你會看到視窗中 OPEN PORT 右方生出 5566 和 8080 可以點了開新窗秀出網頁。
該兩個網頁顯然是相同的,因為我們用 -p 把 5566 和 8080 對應到haha 內部port 5678,
還有,因為我們docker run 時用 -d 參數所以 haha 這container會在背景執行,
命令窗可以繼續使用,例如執行另外的docker image 產生別的網頁服務(web service)。
滑鼠點 OPEN PORT 右方的 5566 或 8080 會看到一個簡單網頁,
然後 過五秒將跳轉如下畫面, 就是原來的網址後面加 /hello
只要你的 PWD docker 四小時還沒用完且你的程式還跑著, 該網址在全世界都有效。
你可以把該網址給其他人測試, 網址很長用 Email 或 LINE 傳沒問題,
或是你可以先把網址用 reURL.cc 縮短成短網址再傳給朋友,這樣他就可以用打的:-)
以下跳轉後的畫面中有連結(hyperLink)可點按跳去玩幾A幾B 遊戲。
Docker 最大的好處就是解決部署(Deploy) Application 的問題!
不必再叫使用者安裝這安裝那 ..
就先打包成一個 docker image file 並 docker push 後叫客戶類似這樣:
docker run -dt -p 8080:5678 -p 5566:5678 tsaiwn/haha 
(丟到 hub.docker.com 如果不讓別人用是要付錢的!!)
上面跑的簡單網頁是用 Python + Flask 寫大約 30行的程式碼弄出來的,
o 關於如何用 Python + Flask 快速弄出網站或 Web Service 請看 iottalk.vip/6/#W2
    或
    測試這更簡單只有 6 行的 w.py (就是以下六列, 注解和空白列不算:)
### w.py     # coding=utf8
from flask import Flask
app = Flask(__name__)

@app.route("/")
def webRoot( ):
   return '<font color=Red size=7>Hello World!  &nbsp; &nbsp; 這是網站喔 !</font>'

app.run( '0.0.0.0' , port=5566)
其實 +ADD NEW INSTANCE 生出的 Alpine Linux 預設已經安裝了 Python3 和 pip3
所以可以立即測試上面這個 w.py 看是否真的可長出簡單網站 !?
(1)安裝 flask 套件,在黑色終端機窗中打以下命令: 
    pip3 install flask 
(2)建立 w.py 
 (2.1)如果你會 vim 就用 vim 建立 w.py 放入上面範例  w.py 內容
 (2.2)如果不會 vim, 照以下做
   (a)把上面 w.py 內容用滑鼠 Mark 後敲 CTRL_C 複製備用
   (b)在終端機窗打 touch w.py  生出空的 w.py
   (c)點黑色窗上方的 EDITOR 彈出編輯窗
   (d)在編輯窗中 /root 下方可看到 w.py 點它一下
   (e)在生出的檔案編輯窗中敲 CTRL_V 把在(a)複製的程式碼貼上
   (f)點 Save 存檔
   (g)到黑色終端機窗打 cat w.py 確認 OK
   (h)可以關閉 w.py 的編輯窗了
(3)在黑色終端機窗中打以下命令:
   python3 w.py
(4)點上方 OPENPORT 並輸入 5566 之後按下 ENTER 鍵
(5)看到網頁了 !
o 關於如何把 Application 包成 Docker Image,等下就用我弄的另一個 tsaiwn/haha0 當範例。
    你也
可先看這簡單的Run Python Flask App using Docker
o 還有,這篇 Python Flask Docker 也很簡單
o 較完整且複雜點的,這篇 還用到 nginx 和 docker compose
o 通常Flask網站產品化建議要用 uwsgi,這篇有用uwsgi + nginx 很詳細 還有影片教學(25分鐘)
    (影片: Containerizing Python web apps with Docker, Flask, Nginx & uWSGI)
TOP ToC
簡單範例示範用 Dockerfile 一兩分鐘做出一個 docker image
簡單範例示範一兩分鐘做出一個 docker image
請先另外 +ADD NEW INSTANCE 開個 Alpine Linux 來跑 tsaiwn/haha0  
docker run --name haha -dp 8888:5566 tsaiwn/haha0       
等 tsaiwn/haha0 被 pull 下來並跑起來後,
終端機窗上方那 OPENPORT 右方會長出 8888 可以點看看網頁!
另外,因為我們 docker run 用 -d 所以 Container 在背景跑不佔用我們終端機,
我們可以在終端機打其它命令,例如:
docker inspect haha # 查看 image haha 內部資訊 (包括用到的 port )
docker exec -it haha bash   # 因前面有 --name haha 所以不必查看 cid
這個 tsaiwn/haha0 是用以下 Dockerfile 做 docker build 生出的:
要先準備兩個檔案:w2.py 和 Dockerfile (如下說明)

#(1)首先建立 w2.py
#初學 Unix / Linux 可能不熟悉 vim 或 nano 編輯器,
# 沒關係,可以暫時利用 PWD 的 EDITOR 功能:
# 先在終端機視窗(目前是 Alpine Linux) 打 touch w2.py 生出 w2.py
# 點 EDITOR 以便開啟類似 notepad 記事本的編輯器(彈出小視窗)
# 在編輯器視窗的左邊 /root 下有 w2.py 點它開啟編輯
# 把 w2.py 的內容(參考以下的連結找到 w2.py)貼上去 (或複製上面給的簡單 w.py 來貼上)
  (提醒這編輯窗用 CTRL_V 貼上即可, 記得按 Save 存檔。複製和 notepad 一樣用 CTRL_C
    ,再次提醒那 PWD 的終端機窗要用 CTRL_SHIFT_V 貼上,用 CTRL_INSERT 複製。)
# 關於這用到的 w2.py 請點這參看我以前給大一計概的範例
# 到 終端機視窗打 cat w2.py 確認,然後該 EDITOR 彈出的窗就可以關閉啦!
#(2)接著要建立 Dockerfile
#用同樣方法把以下內容建立一個叫 Dockerfile 檔案:
# Dockerfile 內的註解是必須用 # 開頭,若開頭不是 # 即使後面 # 不是註解
# 使用 hub.docker.com 官方的 image ubuntu:latest當 Base 
FROM ubuntu 
# 複製目前目錄下的 w2.py 到 docker內的 /app/w2.py  
COPY ./w2.py /app/w2.py 
RUN apt-get update 
# Dockerfile 內規定用 apt-get (不能用 apt )
RUN apt-get install -y python3 python3-pip
RUN pip install flask
CMD python3 /app/w2.py

#(3)再來用 docker build 根據 Dockerfile 生出 image haha0
# 建立上面的 Dockerfile 後, 用以下 docker build 命令就可生出 haha0 這 image:
docker build -t haha0 . 

#(4)最後當然把它 push 上去 docker hub 倉庫:
docker tag haha0 tsaiwn/haha0      # 要 docker push 一定要 tag 成類似這樣 帳號/name:tag
docker login     # 必須登入 tsaiwn 才能 docker push 被 tag 成 tsaiwn/image 的檔案
docker push tsaiwn/haha0     # tsaiwn 是我在 docker 帳號, 用你自己的喔!
# 關於裡面用到的 w2.py 請點這參看我寫的網頁
# docker push 上去 hub.docker.com 後任何人可在任何有 docker 的機器這樣:
docker run --name haha -dp 8888:5566 tsaiwn/haha0
#等 pull 下來之後可以:
docker inspect haha # 查看內部資訊, 可看到用的 port
docker exec -it haha bash   # docker run 有 --name 的好處就是不必查看 cid
# 進入 container 後可打 top 看看機器狀況
看到了吧?
只要有如上兩個檔案: w2.py 和 Dockerfile
然後打如下命令就可以生出 haha0 這個 docker image 了:
docker build -t haha0 .   
### tsaiwn/haha0 的 Ubuntu 版本是最新 (latest) 版本
### tsaiwn/haha 的 Ubuntu 版本比較舊但還多裝了很多工具(haha0 用上面 Dockerfile 造出的)    
## 注意 tsaiwn/haha0 用的 port 是 :5566 (所以 -p 8888:5566 )
## 注意如用 tsaiwn/haha 則 port 是 :5678
docker run --name haha -itp 80:5678 tsaiwn/haha   
## 還有, 因為 tsaiwn/haha 我用 docker commit 弄的, 其 Entrypoint 是bash
## 所以 docker run 要有 -t 否則會立即結束 (所以要用 docker run -dtp 8080:5678 ... )
## 注意 -dt 即使"-t"意思是終端機也是在背景執行, 用 -it 則讓你可以在 Shell 之下互動。
o Two different methods of creating Docker images      
啥!?
你說 用我網頁寫的 w2.py 和 Dockerfile 做出的 "網站" 不會自己跳轉/hello 那頁 ?
對啦, 因為我的 tsaiwn/haha 和 haha0 內的 w2.py 有稍為改過, 要把首頁的內容改為如下:
就是 return 後面的字串改這樣:: (注意單引號打三次喔!)
'''<head><meta http-equiv="refresh" content="5; url=/hello">
<font color=Red size=7>Hello World!  &nbsp; &nbsp; 這是網站喔 !
</font> <br> <br> <br>
<font size=5 color=black>5 秒後跳轉另外一頁 /hello </font>
'''
我另外用 Alpine Linux + python3 + w2.py 做了一個 tsaiwn/haha38  
裡面放了 Dockerfile 和 w2.py 並且有把網站跑在 port 5566
你也可以測試看看:
docker run --name h38 -dp 888:5566 tsaiwn/haha38
docker images
docker exec -it h38 bash
cat /etc/os-release
ls -l /app
cat -n /Dockerfile
# 我有把 Dockerfile 放入 image 內

啥? w2.py 太簡單?
你可以再另外 + ADD NEW INSTANCE 來測試 tsaiwn/dashboard  
或是就在剛剛原來的機器測試 tsaiwn/dashboard  
以下我在同個 Instance (案例) container 同個命令窗,
我先打 docker ps -a 看看有沒有 container 在跑,顯示剛剛的 haha 還在 ! 然後我們繼續打:
docker run -dt -p 7788:5000  tsaiwn/dashboard 
為何剛剛haha 的 -p 的對應是 :5678 現在 dashboard 的寫 :5000 ?
這是因為 haha 內部我把服務故意跑在 5678,但 dashboard 是跑在 port 5000
    (但 tsaiwn/haha0 是跑在 port 5566 )
阿所以就要。。。
跑起來後 PWD 視窗上方 OPEN PORT 右方 會看到 新生出 7788 如下圖:
跑起來後也是點擊 PWD 視窗上方 OPEN PORT 右方 新生出的 7788 看看!
會看到如下畫面:
用帳號 admin 密碼 123456789 登入後會看到如下沒資料的畫面:
這是因為還要到其連接的 IoTtalk server 上做:
(a)建立 Device Model(DM) DataServer
(b)建立 project, 拉入需要的 DM
(c)各 DM 的 IDF 連線到對應的 DM 之 ODF
(d)綁定(bind)各 DM 與其正確的 DA
關於交大 IoTtalk FarmDashboard 安裝/操作請點這參考
 

在 tsaiwn/tsaiwn:16 裡面我有偷放 w2.py
再次看看並測試我改過的 w2.py
就是 docker run 這 tsaiwn/tsaiwn:16 並做 python3 w2.py 看看
   
      這是前面說過原先我寫的 w2.py (給外文系大一同學參考的)      
我改過的 w2.py 可在這 tsaiwn/tsaiwn:16 裡面找到;
你可以再另外 + ADD NEW INSTANCE 來測試 tsaiwn/tsaiwn:16
這個 docker image 是我用 Ubuntu 16.04 加入了 Python3 和 manual pages 以及...
+ ADD NEW INSTANCE 開啟 Alpine Linux 終端機視窗後, 打入:
docker search tsaiwn
docker run -itp 8888:5566 tsaiwn/tsaiwn:16      
把上面這兩句用滑鼠 Mark 後敲 CTRL_C 複製;
準備貼到 PWD 的 docker container (Alpine Linux)的終端機視窗內;
#注意這次用 -it 才可以有互動的模擬終端機用(跑 bash)!
!! 提醒在 PWD 網頁的終端機視窗要敲 CTRL_SHIFT_V 才能貼上,
不是CTRL_V 喔! 也無法用滑鼠按右鍵選貼上!
但可以用滑鼠 Mark 然後敲 CTRL_INSERT 複製(不是 CTRL_C 複製喔!)
    等下我們就用這個新跑tsaiwn/tsaiwn:16 
的 container 來測試並解說 Unix / Linux  一些常用命令。
docker run -itp 8888:5566 tsaiwn/tsaiwn:16       
過一下下就會把 tsaiwn/tsaiwn:16 這 docker image 拉(pull)下來並跑起來, 這時會看到進入 Ubuntu 16.04 的 Shell (bash),且是 root 身份。 上方 OPENPORT 右邊會長出一個 8888 可以點,不過現在點了開啟網頁會發現有錯誤! 然後請在終端機命令窗打入: (對啦,我把 w2.py 偷放在 / 這裡啦!)
pwd  # show current working directory; should be / means root directory
## 注意 / 是 root directory; 但 /root 是 root 的家 home directory 喔
cat -n w2.py   # 印出時每列編號
head -23 w2.py   # 看前面 23 列
cat -n w2.py | tail -23  # show last 23 lines
file w2.py ## 不 要 一 次 全 部 貼上 不然來不及看喔 
python3 w2.py     # 其實 ONLY need this line
再去點那個 OPENPORT 右邊的 8888 看看 ! 有網頁了 ! Yeah ! (跟用 docker run -dp 8080:5566 tsaiwn/haha0 跑的一樣)
更複雜的 Python + Flask 網站範例,
請參考這 w3.py 搭配 static 目錄和 templates 目錄
你可以再 +ADD NEW INSTANCE 弄個新機器測試 tsaiwn/tsaiwn:17
docker run --name ggg -itp 8899:5566 tsaiwn/tsaiwn:17 
跑起來後也可打 python3 w2.py 看看
你會發現它說少了 "Flask" 這模組 !! (故意留給你練習的:-)
這時只要先做:
pip install flask

然後就可以做:
python3 w2.py
                                                     ..

TOP ToC
Private IP / Intranet / Tunneling / ngrok
PWD 的 Docker 使用 Private IP (192.168.x.x)
然後在其上跑的 Docker container 的 IP 當然也是 private IP (172.17.x.y);
理論上應該只能從內部連出去,怎會可以從外部 ssh 連線進去?且在上面跑的網頁也可以被網外使用呢?

因為有個叫做挖地道(Tunneling)的技術 !
例如ngrok 就是是用這技術提供服務。
又例如自己 PC Windows 使用 Private IP 弄個網站,
也可以抓免費的 ngrok 來使用 ngrok 的挖地道(Tunneling)服務(要註冊登入但可免費使用)。

網站相關的實用小常識 -- ngrok 可以幫你挖地道
如何讓內網(Intranet)的網站可以讓外網全世界的人連進來?
就是你的網站跑在 Private IP (例如 192.168開頭的)電腦上, 但想讓別人用啦!
很簡單.. 不要錢(當然付錢可有更多服務),
  ==> 用 ngrok 服務即可, 抓 ngrok 來安裝, 然後設定一下就可用了!
o Download ngrok:
      https://ngrok.com/download
o ngrok on github:
      https://github.com/inconshreveable/ngrok

oo   ==>How to Set Up an SSH Tunnel With PuTTY?
      (用來讓你 在你用 ssh 連線過去那機器跑的 Web 例如 123.123.123.38:8088
      可以變成 Local 機器上某 port 例如 127.0.0.1:80
      通訊是利用你連線那 port 22 通道, 因為加密通訊所以我們稱為 Tunnel 地道 ! )

再說一次,PuTTY SSH Tunneling 可讓你 PC Windows 上 http://localhost:8080 其實連到遠端機器上的 localhost:8888
      * 這裡的 8080 和遠端 8888 都是自己可以選擇設定的。
      * localhost 另一個代稱是 127.0.0.1 都是表示自己這部電腦。
      ! 當然,你必須用 PuTTY 透過 port 22 登入遠端機器才可以喔。
oo   ==>Tunneling and Port Forwarding
oo   ==>Is there a difference between port forwarding and tunneling?
oo   ==>SSH Tunneling (Port Forwarding) 詳解

    o CentOS / RHEL / RedHat 死掉了嗎??   & IBM 2019 年七月用 340億美金併購 Red Hat
    o More about Redhat / CentOS / RHEL / Fedora
    o Best Linux Distros that you should try in 2022   o Best Linux Distributions For Everyone>
==> https://www.docker.com/community/open-source
  Docker is an open source containerization platform.
Docker container image is a lightweight, standalone, executable package
      of software that includes everything needed to run an application:
          code, runtime, system tools, system libraries and settings.
Docker hub -- Build and Ship any Application Anywhere. --
      這是 Docker Container image 的免費倉庫(repositories): https://hub.docker.com/
# Docker 並不是唯一的選擇, 10 Best Docker Alternatives 2022
          https://rigorousthemes.com/blog/best-docker-alternatives/

TOP ToC
Introduction to Docker Container and Kubernetes (簡稱 K8S)
    Docker 使用 Container 虛擬化技術,把應用程式(Application)與其相依性資源等 執行環境封裝(打包)成一個image(映像檔);之後可被 Docker engine 執行。 這樣大家可以把 Docker image 上傳去 Docker hub 共享, 也可以去 Docker hub 搜尋把需要的 Docker image 拉下來讓 Docker engine 執行。 只要你的機器(實體機器或VM機器都可)的作業系統有 Docker engine(Docker 引擎), 就可以執行拉下來的 Docker image, Windows 和 Mac OS 可到 Docker 官網抓 Docker 引擎來安裝。
    Container(容器)與虛擬機器(Virtual Machine; VM)類似,但二者在原理上不同。 Container是將作業系統層虛擬化,虛擬機器(例如VirtualBox)則是虛擬化硬體
所以在虛擬機器(VM)上還要安裝外來的作業系統(Guest Operating System)。
    Docker 或其它 Container 可以在有安裝Docker 引擎的實體機器跑,
也可以在 VM 上任何有Docker 引擎的系統跑。
通常用法是一個實體機器先配置成數個 VM (配置好CPU、RAM與Storage並安裝外來作業系統), 然後在 VM 的作業系統跑許多 Container 和其它非 Container 程式。
    Container通常把Application切分成微服務(MicroService)的方式做部署(Deploy), 當應用程式大到跨多個伺服器部署的多個容器,部署(Deploy)與設定(Config)也變得更加複雜。 雖然 Docker 為封裝與分散容器化應用程式提供了開放標準,但可能太複雜不容易操作且容易出錯。 這時可考慮 Kubernetes (簡稱 K8S)來管理協調和排程多個容器,以及調整容器執行個體的規模。
    Kubernetes(K8S)是一套由Google設計出來用於自動化部署、擴展與管理容器化應用程式的開源系統,它支援了多種不同的容器工具如Docker等。同時也被許多公司用於開發產品,如Rancher Labs、OpenShift、Tectonic、EcOS、PKS及IBM私有雲產品等。
    Kubernetes本身由三種元件組成:Pod、Worker Node、Master Node。
(1) Pod
    Pod是Kubernetes中最小的運作單位,每個Pod都會對應到一個應用程式。 一個Pod通常存在一個或以上的容器,在同一個Pord中的容器則會共享網路資源,透過內部Port進行溝通。
(2) Worker Node
    Worker Node是Kubernetes最小的硬體單位在此簡稱為Node,一個Node對應到的是一台實體或是虛擬機器。
(3) Manager Node / Master Node
    Manager Node 是Kubernetes的指揮中心,負責管理所有Node,Liferay支援在K8S上做自動化部署管理 ,以及Docker上的部署。

    Kubernetes 是開放原始碼協調流程軟體,提供 API 來控制例如 docker 這些容器的執行方式和位置。 該軟體可讓您執行 Docker 容器和工作負載,並協助您解決在調整跨多部伺服器部署之多個容器規模時的一些複雜作業。
o K8S 官方網站 -- https://kubernetes.io/
o K8S OpenSource -- https://github.com/kubernetes/kubernetes
o K8S Engine Mirantis -- https://www.mirantis.com/software/mirantis-kubernetes-engine/
o A Comparison : Docker Swarm vs. Kubernetes(K8S)(IBM)
o Docker hub -- https://hub.docker.com/ 進入後點 Sign Up 註冊後到Mail驗證即可使用。
o PWD : Play with Docker -- https://labs.play-with-docker.com/ (使用 Docker 帳密就可使用 PWD)

TOP ToC
## docker 相關名詞解釋:
# Docker image -- 映象檔 is a file used to execute code in a Docker container # Docker container -- 把 docker image 跑起來(生出 Instance 案例)就叫做 Container; ##同一份 image 可多次執行跑出很多份案例 (Instance) 稱作 container(容器) ##每個 Instance 各自的 Container ID 不同 !     ##可以用 docker ps -a 看全部 container (包括在跑的以及結束 exited 的) # Docker hub -- 官方 docker 倉庫 -- a central repository of container images. ### docker pull 不需要 login; ### 但 docker push 必須 Login 且要 docker tag 成 username/image:tag 格式 # Docker engine -- 用來執行 docker image 的程式 (dockerd + docker) ## Docker Engine is the core product of Docker company,     ## including its daemon (dockerd) as well as its CLI (docker). # CLI == Command Line Interface
## docker 常用命令: docker search tsaiwn # 到 hub.docker.com 搜尋 tsaiwn 關鍵字 docker run --name haha -dp 80:5566 tsaiwn/haha0 # 抓 tsaiwn/haha0 來跑 docker ps -al # 查看剛才那個(last) container 的狀態, 可看到 cid (container id) docker stop cid # 用來 STOP 執行中的container; cid 是 container 的 id (打前面幾個字即可) docker restart cid # stop 之後可以 restart docker kill cid # 用來殺死執行中的container; cid 是 container 的 id (打前面幾個字即可) docker rm cid # 用來砍掉 container; cid 是 container 的 id (打前面幾個字即可) # 執行中的container 要先 kill 它 才能 rm 砍掉它 docker rmi iid # 用來砍掉 image; iid 是 image 的 id (打前面幾個字即可) # 若還有該 image 的 container 案例(Instance) 在跑則 image 砍不掉! # 若該 image 還有別的 tag 與這同 iid 也砍不掉, 這時要用 image name docker rmi imagename # 用來砍掉 imagename, 若同 iid 還有別的 tag 則這只是 untag docker commit 38 newgood # 把目前 cid 是 38開頭的 container 寫到 newgood image file docker tag tsaiwn/tsaiwn:16 badbad # 把它 tag 會看起來多一個 image 但其時 iid 相同 docker rmi tsaiwn/tsaiwn:16 # 只是 untag docker tag newgood tsaiwn/tsaiwn:16 # 這樣才可以 docker push # (就是要 tag 成 yourUsername/anyname:tag 才可以 docker push) docker push tsaiwn/tsaiwn:16 # 要先 docker login; push 去 docker hub # 可以先建立 Dockerfile 再用 build 方式產生 image file docker build -t tsaiwnNew . --no-cache # . 表示 Dockerfile 在目前目錄 另外關於較複雜的docker-compose 和 docker-compose.yml 請點這看官方文件  ==> https-using-nginx-certbot in Docker(using docker-compose)  ==> How To Use the Official NGINX Docker Image(in docker Blog)  ==> Day 11 利用 docker 安裝 nginx 並配置 https(2021 iThome 鐵人賽)
* Docker 常用命令; Again
docker --help  # list docker commands and what they do
docker image --help  # help message about docker image
docker run --help  # help message about docker run
docker run -it tsaiwn/tsaiwn:16     # pull (download) and run the image from docker hub 
# 這樣你就立即有 Ubuntu 16.04 可用, 且包括 Python3 和 ssh 工具
docker run --name ggg -itp 80:5566 tsaiwn/tsaiwn:16 
#這樣你跑起來後打 python3 w2.py 才有一個簡單網站可以用(port 80:5566)
docker run -it tsaiwn/tsaiwn:18     # pull (download) and run the image from docker hub 
# 這樣你就立即有 Ubuntu 18.04 可用, 且包括 Python3 和 ssh 工具 # 可以打 type ssh 看看是否認識 ssh 命令 (這是 client 端) # 也可打 service ssh status 查看 ssh daemon 狀況 (這是 server 端)
docker run -itp 8080:5566 tsaiwn/tsaiwn:18     # 也是 pull (download) and run Ubuntu 18.04 
# 這樣你就立即有 Ubuntu 18.04 可用, 且可以打 python3 w2.py 讓 port 8080 有網頁, 但是.. # 但是, 我故意沒pip install flask 所以它會說找不到 Flask 模組 # 這時你只要先 pip install flask 然後再次執行 python3 w2.py 就可以有網頁啦! ## 複製/貼上 in PWD Play with Docker Window : (很重要所以說三次:-) ## use mouse to mark ==> CTRL_Insert to copy ==> CTRL_SHIFT_V to Paste ## use mouse to mark ==> CTRL_Insert to copy ==> CTRL_SHIFT_V to Paste ## use mouse to mark ==> CTRL_Insert to copy ==> CTRL_SHIFT_V to Paste
docker image ls # List images on my machine docker images # List images on my machine; same as above command. docker run -dt -p 80:5000 myapp # myapp 在 docker 內的 port 5000 對應到外部本機的 port 80 docker run -dt -p 8080:5678 tsaiwn/haha # test tsaiwn/haha port 5678 (注意故意用 5678) docker run -dt -p 7788:5000 tsaiwn/dashboard # test tsaiwn/dashboard port 5000 docker run -dt -p 8088:5566 tsaiwn/haha0 # test tsaiwn/haha0 port 5566 (注意 haha0 故意用 5566) ## docker pull ubuntu:16.04 # pull the image docker tag ubuntu:16.04 ggyy # give the image ubuntu with Tag 16.04 an alias name ggyy docker run -it ubuntu:16.04 # Run the image ubuntu with Tag 16.04, open interactive terminal docker run -it ggyy # Same as above command if you had TAG it as ggyy
docker run -itd ggyy # Same as above command BUT run as daemon (在背景執行) ## -itd means interactive terminal daemon; will show container ID and then detach it, ## .. and you can attach it back to your terminal soon later docker ps -a # see containers status and their ID (包括已經結束的 container) docker attach 388 # attach a background container which ID begins with 388
docker search tsaiwn # search Repository docker images docker container ls # LiSt all running containers (docker ps -a 才能看到結束 exited 的) docker search tsaiwn # 很重要所以多打幾次 :-) docker run --name ggyy -itp 8088:5566 tsaiwn/tsaiwn:16 # run the image tsaiwn/tsaiwn:16 cat /etc/od-release # Unix command to see OS information # 這時在 Ubuntu 16.04 內,敲 CTRL+P CTRL+Q 可暫時離開這 container 回到 Alpine Linux, # 之後可用 docker attach ggyy 返回 container (因為剛有 --name ggyy 所以不用 cid) # 還有, 因剛剛有 -p 8088:5566 把內部 port 5566 對應到外部 8088, 所以.. ## 所以, 你可以打 python3 w2.py 就有前面我們說的簡單網站了!    
alias dk='docker' # Unix command, so that you can use dk instead of docker alias dks='docker image ls' # Unix command, dks == docker images alias dkps='docker ps -a' # Unix command, dkps == docker ps -a alias dkss='docker container ls' # Unix command, dks == docker container ls alias dkss='docker ps -a' # this is enough for use to see containers status ## In a Docker container, Press CTRL_P then CTRL_Q will leave (detach) current docker docker container ls # see running container(s), see their container ID docker ps -a  # see ALL containers, including "exited" containers docker commit 3388 heyha # commit the container with ID begins with 3388 to image heyha docker tag heyha tsaiwn/mytest3 # tag heyha as tsaiwn/mytest3 docker login # before push, you should login (my username is tsaiwn on docker hub) docker push tsaiwn/mytest3 # push my new image to docker hub so that can be used later docker attach 3388 # attach a background container which ID begins with 3388
# 另一種生出 Docker image 的方法是使用 docker build 從 Dockerfile 內的描述做出 image docker build -t tsaiwnNew . --no-cache # . 表示 Dockerfile 在目前目錄 ## 對啦,就是前面已經舉例做出類似 tsaiwn/haha0 的方法。   # 以下兩列 Unix/Linux 命令和 docker 無關 :-) apt install -y iputils-ping # Unix command to install ping utility 安裝 ping 工具 cat /etc/os-release # Unix command to show OS name and version ... 查看 OS 版本資訊
另外關於較複雜的docker-compose 和 docker-compose.yml 請點這看官方文件

如果你把 Ubuntu 安裝在自己電腦實體機或虛擬機,
當然可以安裝專面管理程式當"工作站" 使用滑鼠甚至開很多工作窗!
至於在 PWD 的 docker 跑 Ubuntu 則可以利用 VNC (Virtual Network Computing) 玩遠端桌面!
+ADD NEW INSTANCE 再開個 Alpine Linux 視窗並打入:
docker run --name vnc -dp 80:80 -v /tmp:/dev/shm tsaiwn/vnc 
等跑起來後,開啟 OPENPORT 右方的 80 網頁就有 VPC 遠端桌面可以用。
這個 tsaiwn/vnc 其實就是 dorowu/ubuntu-desktop-lxde-vnc
不過跑這種遠端桌面有點慢,而且好像會被 PWD 切斷連線 ?!
所以,最好要習慣用終端機連線 :-)
  =>What is a VNC (Virtual Network Computing)?(類似微軟遠端桌面、PcAnywhere)
  =>How to Install and Configure VNC on Ubuntu 20.04

o o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹

TOP ToC
* Unix / Linux 簡介 (Linux / Ubuntu / GNU / BSD)

Linux is an operating system which is developed by Linus Torvalds in 1991. (當時 Linus 是芬蘭赫爾辛基大學計算機科學系大三學生。其實在他大二修 Operating Systems 課程時曾在網路新聞論壇(Newsgroup; 就是全世界互通的討論群組)貼文說他想自己寫一個作業系統,說當然不是像 GNU 那麼偉大的作業系統,但至少要像 MINIX 那樣完整)。 1991年9月,Linux Torvalds公開了Linux內核源碼0.01版,程式碼大約一萬行(10239 Lines)。程式碼被大學FTP server管理員Ari Lemmke發佈在網際網路上,網路上很多人給Linux Torvalds"按讚"鼓勵或給他意見,1991年10月又公開了0.02版。1991年12月發佈0.11版,1992年2月釋出0.12版本,並且把授權條款改為GNU 的 GPL 授權條款。1994年3月14日發布了1.0版本Linux,1996年Linus Torvalds碩士畢業, 他的學位論文是《Linux: A Portable Operating System》。

1996年6月9日,Linux 2.0版本發布,2008年9月,基於Linux內核的手機作業系統Android發布。2011年7月21日,Linux3.0版本發布,2013年6月釋出的Linux核心版本3.10包含15,803,499行程式碼。2015年6月釋出的4.1版本已發展到超過1950萬行程式碼,由近14000名程式設計師貢獻。

2019年3月3日,Linux 5.0版本發布,2019年11月24日,Linux 5.4版本發布(第20個LTS發行版本)。2020年12月13日,Linux 5.10版本發布(第21個LTS發行版本)。2021年10月31日,Linux 5.15版本發布(第22個LTS發行版本)。(LTS意思是長期支援,通常是五年)

Linux 核心明確地表明僅發表在 GNU 通用公眾授權條款 v.2(GPL第二版),
該協定允許任何人對軟體進行修改或發行,包括商業行為,只要其遵守該協定,所有基於Linux的軟體也必須以該協定的形式發表,並提供原始碼。
Linux Torvalds公開說他本人不贊成 GPL v3 (第三版)。

Linux 是 Unix 的一種,或者該說是 Unix-like (類似 Unix)的開放原始碼(OpenSource)作業系統。 就是一個很像 Unix 但不是 Unix 的作業系統(Unix 版權是 AT&T 公司,不過後來輾轉賣來賣去), 這原本是 GNU (GNU is Not Unix)基金會(創始人是Richard Stallman)在做的事, 實際上當初Linus Torvalds 也是因受到 GNU 的啟發才開發 Linux 系統。 後來在 2012年Linux正式成為GNU計畫中的一部份, 所以有人吵著說 Linux 該改名為 GNU/Linux 但是好像有些人不認同。。。

另一個類似 Unix 且很多人用的是 FreeBSD,也是開放原始碼(OpenSource)。 FreeBSD 是基於BSD Unix (Berkeley Unix)的原始碼開發,BSD Unix是加州大學 柏克萊分校在1975年開始基於 AT&T Unix開發的BSD Unix作業系統。

關於 Unix 的發展歷史,可以點這看 Unix 在維基百科的說明

Ubuntu vs. Linux
Linux 是指作業系統的核心(Kernel),Ubuntu 是 Linux 發行版(distribution; 散播版)的一種。 Ubuntu is a Linux based Operating System and belongs to the Debian family of Linux. 其他知名的 Linux 發行版有 Red Hat (小紅帽)、Fedora(Red Hat的社群版)、CentOS、Alpine Linux等。


o => 維基百科上 Linux 的介紹
o => 關於 Linux Kernel (內核)
o => List of Linux distributions(維基百科)
o => 老共華爲18級工程師也寫Linux學習筆記出版書籍
o => Unix和Linux作業系統有什麼區別?
==> Unix Family Tree (.jpg 圖片)(1969-2019) (1951 - 2022)

o => What are the major technical difference between Multics and Unix?
o => Multics -- 作業系統的始祖(但是 ... )
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o 微軟在2018年6月以75億美元收購GitHub.com(Github 2008年2月上線)
o Python 官方網站:https://www.python.org/   Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o IDE:How to Install, Run, and Connect to Jupyter Notebook on a Remote Server
o How to Install Jupyter Notebook without Anaconda on Windows?
o Install Anaconda and Jupyter Notebook on Windows(簡單說明)
o Install Anaconda on Windows(官方文件)
Anaconda 號稱是第一厲害的資料科學(Data Science)工具平台
Anaconda 主要支援 Python 和 R 語言的安裝與套件管理。
Anaconda is a distribution of the Python and R programming languages for scientific computing.
Anaconda is primarily developed to support data science and machine learning tasks.
Anaconda提供了很多應用程序,包括好用的交互型簡易IDE Jutyter Notebook以及應用版本的JupyterLab,python的Qt控制台,數據科學開發必備的利器Spyder,輕量級的代碼編輯器VSCode,重量級的IDE Pycharm,機器學習的雲端開發環境datalore和IBM Watson studio cloud,以及資料探勘神器Orange 3等工具(VSCode 和 Pycharm 這些並不是預設安裝,但可以直接在界面中點擊安裝)。
=> Jupyter Notebook vs PyCharm
=> 12 BEST Python IDE & Code Editors For Mac & Windows In 2022
* * Note that Python 3.9.x cannot be used on Windows 7 or earlier.

o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive
Ubuntu is derived from Debian.
It means that Ubuntu uses the same APT packaging system as Debian and shares a huge number of packages and libraries from Debian repositories. It utilizes the Debian infrastructure as base.
There are hundreds of Linux distributions, only a handful of them are independent ones, created from scratch. Debian, Arch, Red Hat are some of the biggest distributions that do not derive from any other distribution.
oo   DeBian 這字如何念? (爹比嗯)

o Difference between UNIX and Windows Operating System(simple view)
o Is Windows Unix based?
o Difference between UNIX and Windows Operating System !

TOP  ToC
繼續在PWD 網頁點 +ADD NEW INSTANCE 開啟 Linux 終端機練 來 練習使用 Unix 命令

* Unix 常用命令(讓你跟著打入練習)
PWD(Play with Docker)的 Docker 是跑帶有 Docker 引擎的 Alpine Linux系統。
  所以在 PWD 點 +ADD NEW INSTANCE 後你就建立一台安裝Alpine Linux系統的機器(最多可建立五台),
然後在網頁中的 終端機視窗打:
cat /etc/os-release   # 可看到目前 OS 的版本資訊
cat --help  # 查看 cat 用途和用法;任何命令都可打 --help 查看
pwd  # 看看目前在哪個目錄 (Print Working Directory)
type pwd  #  看看 pwd 是啥命令 (  pwd 是 shell 內建的 builtin 命令 )
type type #  type 也是 shell builtin 命令
type --help  # 查看 type  用途和用法
type cat  # 注意 type 在 Windows 的 CMD 類似 Unix 的 cat 是印出檔案內容
# 如果你習慣用 type filename 看檔案內容, 可如下 alias
alias type=cat   # 這樣以後打 type fileName 就等於打 cat fileName
type /etc/os-release   #  善用 TAB 鍵, 打到 /et/os 就敲 TAB鍵自動完成(auto completition)
type type  # 會說找不到 type 這檔案
\type type # 用反斜線開頭表示不要查看 alias 對照表;所以這時 \type 就是原來的 type 命令
\type cat  # 問 cat 是哪類命令 ?
which cat # 結果類似 \type cat
which python
which python3
which ping
whoami # show user name
ls -al /   # see file/dir in  root directory /
ll  # unknown command now
alias ll='ls -l'   # so that you can use ll  as ls -l
ll /   #  same as ls -l /   # option -a means also see hidden file ( begins with . dot)
ping -c3 mit.edu  # 看看連去 mit.edu 來回要多久 ; Alpine Linux 已經安裝 ping 工具
ping -c3 ibm.com  # 看看連去 ibm.com 來回要多久 
man bash-builtins  # 查看 有哪些 shell 內建命令以及用途用法 (系統要有安裝 manual !)
# Alpine Linux 預設沒有 manual pages !
現在來跑 Ubuntu Linux 
docker run -it ubuntu  # pull ubuntu:latest image and run; 這樣就變成最新版本的 Ubuntu Linux
# 這樣 終端機視窗 變成 Ubuntu 最新版 (因為從 hub.docker.com 抓下 ubuntu:latest 跑起來了) cat /etc/os-release # show OS information type ll # 看看是否認識 ll ll /etc/ # 看 /etc/ 有哪些東東 ## Ubuntu 預設已經偷偷 alias ll 了!! 不必再自己 alias ping -c3 mit.edu # If ping NOT work, apt install iputils-ping # 這 Ubuntu Linux 預設沒安裝 ping 工具 ! apt update # 更新系統 apt install -y iputils-ping # 安裝 ping 相關工具 ping -c3 ibm.com # 看看連去 ibm.com 來回要多久 ping -c3 140.113.1.254 # 看看連去 NCTU (NYCU) 來回要多久 ping -c3 140.114.1.254 # 看看連去 NTHU 來回要多久 man bash-builtins # 查看 有哪些 shell 內建命令以及用途用法 (系統要有安裝 manual !) # 這個 Ubuntu 最新版預設沒有 manual page; # 因為 docker 本來用途是跑 Application 的; 不用安裝太多開發者用的東東 :-) 再點 +ADD NEW INSTANCE 建立另一台安裝Alpine Linux系統的機器, 然後這次在網頁中的 終端機視窗打:
cat /etc/os-release   # 可看到目前 OS 的版本資訊
這時當然仍是 Alpine Linux ## 接著來跑 Ubuntu 16.04
docker run -it ubuntu:16.04  # 這樣就變成 16.04 版本的 Ubuntu Linux  
# 現在變成 Ubuntu 16.04 (因為抓下 ubuntu:16.04 這 docker image 來跑成 container ) pwd # 看看目前在哪個目錄 cat /etc/os-release # 善用 TAB 按鍵, 打到 os 就敲 TAB 鍵 man bash-builtins # 這當然仍沒 manual pages ping -c3 mit.edu # 仍然沒 ping 可以用, 要自己安裝 apt update # 更新系統 apt install -y iputils-ping ping -c5 mit.edu #apt update #apt install apt-file #apt-file update #apt-file search --regexp 'bin/ping$' # where is the ping command ### netutils-ping: /bin/ping ### iputils-ping: /bin/ping # we use this iputile-ping 如果跑我弄的 tsaiwn/tsaiwn:16 這 image .. 則已經安裝了 manual pages 和一些工具 再點 +ADD NEW INSTANCE 建立另一台安裝Alpine Linux系統的機器, 然後這次在網頁中的 終端機視窗打:
docker run -it tsaiwn/tsaiwn:16  # 這也是 Ubuntu 16.04 但我裝了 manual pages/Python ... 
cat /etc/os-release # 查看 OS 版本 ; 發現也是 Ubuntu 16.04 whoami # who is the user login now man bash-builtins # 這次有 manual pages 了 man intro man 2 intro # 看 chapter 2 簡介, system calls man 3 intro # library functions man 8 intro # chapter 8 for super users man hier # 查看 Unix /Linux 檔案結構,各目錄的用途 ... man 5 proc # process information pseudo-filesystem ping -c3 ibm.com # 有 ping ㄟ ping -c3 140.113.1.254 # 看看連去 NCTU (NYCU) 來回要多久 which python # 有喔! type python3 # 也有 !! python -V # 看 python 版本 pwd type nano # 有 type vim # 有 ls -al /etc # see file/dir in root directory /etc type ll # 查看 ll (小寫的 LL) ## Ubuntu 預設已經偷偷 alias ll 了!! 不必再自己 alias ll /etc | more # 把 ll 結果 pipe 給 more, 可以按空白看下一頁或按 ENTER 往下一列 cd /tmp # jump into the dir /tmp ## 這 /tmp 任何人都可以讀取和寫入 ll mkdir haha # 造一個子目錄 haha cd haha # cd 跳進去該目錄 touch gg # 產生檔案 gg (若原先已經存在則會更新檔案更改日期時間) touch yyy # 可以打 touch --help 查看 touch gps echo "123" >> gg echo "456" >> gg cat gg date >> yyy echo "---" >> yyy date >> yyy echo "=== ===" >> yyy cat yyy cat gg cat yyy >> gg cat gg cd .. ll cp -p haha/g* . ll mv gg test.txt ll cat gg cat test.txt type test.txt # 會說不認識 test.txt (不是不認識 type 喔! 因 type 是內建密令 ) alias type='cat' # 從此 type 和 cat 一樣 (Windowd CMD 用 type 印出檔案內容) type test.txt # print out content of test.txt ## 那我要用原先內建的 type 查看命令怎辦? \type pwd # 左邊把 \ 反斜線表示不理會 alias \type type unalias type # 取消 alias type type type # 又恢復成 builtin 的 type ## 來測試可執行的檔案 eXecutable file echo "echo 'I Love U'" >>gps # 在 gps 檔案內增加 echo 'I Love U' cat gps # 查看 gps 內容 source gps # run this file as script file . gps # same as above; 注意 . 點後面至少空一格 ! 對啦, 此時 . 等於 source ./gps # error, 'cause not eXecutable chmod +x gps # make it eXecutable ./gps # run again, now OK gps # 不認識這命令, 因為 . 不在 $PATH 命令路徑內 ! ###   ## 為何 Unix people 說不要把 . 放入命令路徑內?因為很危險 # Unix / Linux 強烈建議不要把 . 放入 $PATH 命令路徑內 !
Unix/Linux 的 /tmp 任何人可以讀寫 (Unix 通常很多人連線進去 共用!) 很多人喜歡這樣: cd /tmp ls -l # 查看 /tmp 下有哪些檔案 ## 有些人常說害人為快樂之本 ... 以前有人故意在 /tmp 內寫了一個 script 命名 ls 且內容如下一列: \rm -R -f ~/ 並且用 chmod +x ls # 把 ls 檔案設定為可以 eXecutable 可執行的! ## \rm 的 \ 反斜線表示即使你有 alias rm 也不使用 alias 的!! 確保執行 rm 命令 ! ## -R 表示 recursive 也砍子目錄子子孫孫 ! -f 表示不要問, 砍就對了 ! 然後, 跑去 /tmp 做 ls 或 ls -l 的就哪個那個慘叫囉 !! 從此以後, 很多人發誓再也不把 . 放到 $PATH 命令路徑內 !!! 所以你常會看到 Unix people 在寫程式時常這樣: g++ myprog.cpp # 編譯 myprog.cpp 並 Link 成執行檔 ./a.out # 因為生出的執行檔預設是 a.out
## 但 Windows 系統預設一定先找目前目錄當做命令路徑的第一個目錄!! type test.txt # same as cat echo "3gy388 test " >> test.txt echo "5566 haha" >> test.txt echo "5678gy haha" >> test.txt type test.txt grep "38" test.txt # filter, show only those lines contains "38" grep "3" test.txt grep 3 test.txt grep "[3|8]gy" test.txt # filter, contains "3gy" or "8gy" grep "3.*gy" test.txt # filter, 3...gy where ... can be any 0 to many char clear ps -aux # see process list kill pid # kill the process which process ID is pid kill -9 pid # force kill the process which process ID is pid ll cls alias cls='clear' cls nano test.txt # try the simple editor nano apt update apt install -y nano vim # install nano, vim nano test.txt # edit the test.txt; CTRL_S to save, CTRL_X to exit cat test.txt df df . gcc --version apt update ## gcc/g++ ... apt install -y gcc gcc --version python3 -V apt update apt install -y python3 python3 -V cd /usr/local/bin # jump into /usr/local/bin # 注意現在是在 /usr/local/bin 必須有 super user 權限才能寫入; 或左邊加 sudo (須是sudoer) ln -s /usr/bin/python3 python # /usr/local/bin/python points to /usr/bin/python3 pip3 apt update apt install pip3 # can NOT find pip3 apt install python3-pip # 這樣才是 安裝 pip3 cd /usr/local/bin # make sure we are in /usr/locak/bin ln -s /usr/bin/pip3 pip # /usr/local/bin/pip points to /usr/bin/pip3 cd / sudo --help # 如果不認識 sudo 要如下安裝 apt update # update syustem before any apt install apt install -y sudo # 安裝 sudo 套件 sudo --help # 可以了 apt --help | head # 只要看前面 10 列 adduser tsaiwn # add a user usermod -aG sudo tsaiwn # 讓 tsaiwn 可以用 sudo (變 sudoer ) ssh # 如果不認識 ssh 要安裝 openssh-server : apt update apt install -y openssh-server # allow ssh remote Login service ssh status service ssh start ## so that you can remote Login ## systemctl enable ssh ## seems not work in Docker ## /etc/rc.local NOT run in Docker cd /root # jump into /root directory ls -al vim .bashrc # /root/.bashrc is run when root login # 打入 :3 # in vim, jump to Line 3 # 打小寫 o # lowercase letter o to open a new line (Enter Insert mode) service ssh start #add this line cat /etc/os-release #add this line too echo "My IP is `hostname -i` " # 注意 ` ` 不是單引號,是 ESC 下方的重音符號 ## 重音 (反向單引號)夾著的命令會立即執行然後被結果取代 !! # PRESS Escape # to leave insert mode # 打入 :w # write to file (先確定寫入; 不建議用 :wq 建議分兩次) # 打入 :q # quit vim (離開 vim ) ## OR ### Do the following ... cd /root cat >> .bashrc <<GGYYY service ssh start #add this line cat /etc/os-release #add this line too echo "My IP is `hostname -i` " GGYYY ### 前面的 <<GGYYY 表示看到 GGYYY 開頭就結束 ##改好之後可以打 bash 測試看看 bash ## 或打 su 測試看看 su exit ### leave the shell (command interpreter) :: will terminate the container !
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o 微軟在2018年6月以75億美元收購GitHub.com(Github 2008年2月上線)
o Python 官方網站:https://www.python.org/   Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive


TOP  ToC
apt install 和 pip install 有何不同 ?
(1)apt install 是安裝 Linux 套件(package); 
   會跑去 Ubuntu 的倉庫 (Repository) 抓!!
   只有 Super user 才可以 apt install 安裝套件。
(2)pip install 是安裝 Python 的程式庫套件(package);  
   會跑去 Python 的 Pypi 倉庫 (Repository)抓!!
   一般 User 只要建立虛擬隔離環境(python -m venv)就可以 pip install
### PyPI(Python Package Index)是python官方的第三方庫的倉庫(Repository),
### 所有人都可以下載第三方庫或上傳自己開發的庫到PyPI。
### PyPI推薦使用 pip (意思是Python Install Package)管理器來下載第三方庫。

# apt install python3  # 安裝 python3
# apt install python3-pip   # 這是 pip3; 
## 如果裝完 python3 仍沒 pip3 可用就要如上安裝 pip3
######
 

TOP  ToC

如果用 Windows 或 Mac 想在自己機器跑Linux,
可以抓免費 VirtualBox 先裝虛擬機

! ! 用 VM 安裝 Ubuntu Linux
**如果用 Windows 或 Mac 可以抓 VirtualBox 先裝虛擬機,
    然後在虛擬機上安裝 ubuntu 16.04 版本ubuntu 18.04 版本ubuntu 20.04 版本
o 直接到官方網站抓 VirtualBox 6.x for Windows 或 Mac OS X
    https://www.virtualbox.org/wiki/Downloads
o 也要下載 ubuntu 16.04/18.04/20.04 的 64bit server 版 .iso 檔案:
      ubuntu-16.04.7-server-amd64.iso 2020-08-10 18:24 880M
    https://releases.ubuntu.com/16.04/
    https://releases.ubuntu.com/18.04/
    https://releases.ubuntu.com/20.04/
o Ubuntu 台灣官方網站:
    https://www.ubuntu-tw.org/modules/tinyd0/


關於 Linux 系統,
若要用 IoTtalk 和 FarmDashboard,
建議用ubuntu 16.04 版本, 其它版本不確定是否與IoTtalk 和 FarmDashboard相容。
可輸入指令 cat /etc/os-release 查看系統資訊
$ cat /etc/os-release
ubuntu server
...  

o 通常剛安裝好 Ubuntu server 該做一些設定讓系統比較安全...
(Initial Server Setup with Ubuntu 16.04) ..
    See https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04


o 在 VirtualBox 虛擬機上安裝 Ubuntu (建議先把 Ubuntu 16.04 的 .iso 檔案抓回來你電腦)
 1.)Open VirtualBox and select New . A new window will come out.
 2.)Choose your guest OS and architecture (選 64 bit,  select Ubuntu)
 3.)Set your Base Memory (RAM) 建議至少 2GB
 4.)Click next until it show the vm storage size. 建議設定 10GB 應該就夠用!
    Put how much space you need depending on your hardisk and finish the wizard 
    by clicking the create button.
 5.)On VirtualBox main window, select START and pick your MEDIA SOURCE. 
    For example, select the .iso on your desktop.
 6.)Finish the installation as normal install.
 7.)Remove your installation .iso from the virtual optical disk drive before restarting the VM.
可以參考這:
    https://www.kjnotes.com/linux/102
    或 點這參看 林一平教授實驗室的教學影片

o Ubuntu 參考安裝教學:
  => http://ailog.tw/lifelog/2019/08/09/ubuntu-16-04-lts-install/

  => How to Install Ubuntu Linux on VirtualBox on Windows 10
  => How to Install Ubuntu in VirtualBox on Windows 10(Youtube 影片)
  => How To Install Ubuntu 20.04 LTS On VirtualBox In Windows 11 ?
  =>What is a VNC (Virtual Network Computing)?(類似微軟遠端桌面、PcAnywhere)
  =>How to Install and Configure VNC on Ubuntu 20.04

oo How to install docker engine on Ubuntu
oo Install and Configure Docker Engine on Ubuntu(詳細)
oo Installing Docker and docker-compose for Ubuntu 20.04
oo Install Docker Engine on Ubuntu(docker 官方文件)

TOP ToC
* 要用IoTtalkFarmDashboard 建議使用 Ubuntu 16.04 (後面版本不確定相容)
*在 Ubuntu 16.04 上先安裝必要工具 for IoTtalk FarmDashboard
o 安裝 python3 和 pip3
sudo apt -y install python3 python3-pip
 ## 這樣會是 Python 3.5 版,如需要 3.6 或 3.7 須從 Source 安裝(參考往下捲一些)
o 安裝 mysql-server(check install status)
sudo apt install mysql-server
password setting:輸入自訂密碼(8位以上) (e.g userpassword)
o 安裝 pymysql 
sudo pip3 install PyMySQL
o 安裝 git
sudo apt install git-core
o 安裝 vim
sudo apt install vim
o 安裝 tmux
sudo apt install tmux
Ubuntu 16.04 用 apt install 安裝 Python 是 3.5 版;
但是有時我們需要至少 python 3.6 版 或 3.7 版 ...
你可以在 PWD Alpine Linux 終端機打如下弄個 Ubuntu 16.04 來練習安裝:
docker run -it ubuntu:16.04  
# Install Python 3.7 on Ubuntu 16.04, from source apt update apt install -y wget apt install -y gcc # compiler apt install -y make # make utility apt install -y zlib1g-dev # zlib apt install -y libssl-dev #ssl apt install openssl # openssl cd /opt # grab the Python source code from python.org wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz       # wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tgz       # wget https://www.python.org/ftp/python/3.9.8/Python-3.9.8.tgz       tar -xvf Python-3.7.9.tgz cd Python-3.7.9 # ./configure --with-ssl # should install the libssl-dev # ./configure --prefix=/usr/local # ./configure make make install which python3 pip3 -V which python which pip3

docker run -it ubuntu:16.04  
# Install Python 3.6.8 on Ubuntu 16.04, from source ## 可以把以下先存入文字檔例如 ggg.sh 然後 source ggg.sh 即可 apt update apt install -y vim wget curl ssh # /etc/init.d/ssh restart service ssh start service ssh status # openssl apt install openssl -y apt install -y libssl-dev #ssl apt install -y zlibc zlib1g zlib1g-dev # zilb # gcc/g++, make, ... (build-essential) apt install build-essential -y ### sudo apt install libnss3-dev libssl-dev ### sudo apt install libncurses5-dev libgdbm-dev libreadline-dev libffi-dev cd /opt #grab Python-3.6.8.tgz wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz ## wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz # uncompress tar xvf Python-3.6.8.tgz cd Python-3.6.8 #config Python before compile #./configure --enable-optimizations --with-ssl # 這樣 make 會比較久 ./configure --with-ssl # should install the libssl-dev # ./configure --prefix=/usr/local # allow 2 jobs ONLY when make ( -j 2 ) make -j 2 make install ## make altinstall # 保留已經有的 Python, 其實就是不會去用 ln 做 Link 啦! ## 這樣如果剛安裝 python3.6.8 後要打 python3.6 而不是 python 或 python3 pip3 install --upgrade pip python3 -V pip3 -V # test ssl module python3 -m ssl pip -V pip install flask
### MEMO memo .. 如果 pip3 install 出現如下錯誤表示 ssl 模組有問題 !
###  ... pip3 ..There was a problem confirming the ssl certificate ...
apt install -y zlib1g-dev   # zlib
apt install -y libssl-dev  #ssl
# openssl-1.1.1g.tar.gz
wget  https://www.openssl.org/source/openssl-1.1.1e.tar.gz
tar -xvf openssl-1.1.1e.tar.gz
cd openssl-1.1.1g
./config shared --openssldir=/usr/local/openssl --prefix=/usr/local/openssl
# ./config --with-openssl=DIR  # override root of the OpenSSL directory to DIR
# ./config --with-openssl=/usr/local/openssl  # override root of the OpenSSL directory to DIR
make && make install
## config openssl
echo "/usr/local/lib64/" >> /etc/ld.so.conf
ldconfig
cd ..
###

通常抓 Source code 來安裝的步驟如下:
(1)安裝需要的編譯器和工具:
apt update
apt install -y wget   ##或  curl  ### git-core
apt install gcc    # compiler ## and/or g++
apt install make   # make utility
apt install zlib1g-dev   # zlib  ## or others
(2)先跳到 /opt 目錄
cd /opt
(3)抓 source code; 通常可用 git clone 抓; 若不行可用 wget 或 curl 抓
# 用 wget 或 curl 抓回 source code 壓縮檔回來解壓縮
(4)用 tar xvf tgzfile.tgz 解壓縮
#如果用 git clone 就不必解壓縮
(5)跳入子目錄
#通常會在 /opt 下生出子目錄, 例如 Python-3.7.9
cd Python-3.7.9
(6)執行設定檔 (Script 腳本)
./configure  或 ./config    # 可能要給參數
#有可能會問答, 也可能自動偵測,然後會生出 makefile 檔案
(7)用 make 執行 makefile 內的命令
make   # 如沒有 make 要先 apt install make
(8)安裝
make install  # 會依據 makefile 內 install: 指示做命令!  
### 如果任一步驟有錯誤訊息,
### 把錯誤訊息用 google 查詢就可看到其他人的解答!
######
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o 微軟在2018年6月以75億美元收購GitHub.com(Github 2008年2月上線)
o Python 官方網站:https://www.python.org/      Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive
o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹

TOP ToC
Super user(system administrator) 應該要懂的五四三..
  Super user (root) 的 Q & A
通常 super user 就是指 root, 不過擁有與 root 一樣特權者也算 super user 超級使用者。
Shadow password
Q:為何 /etc/passwd 檔案內沒有 user 的密碼?
A:古時候密碼是加密後放在 /etc/passwd 內沒錯,
   但因為 /etc/passwd 是設定大家都可以看得到,
   現在電腦越來越厲害加密也有可能被破解!
   但是系統有許多程式會讀取該檔案其它資訊所以須要保留。
   所以密碼被改放另一個一般user不可以讀取的檔案,
   通常是 /etc/shadow  (但系統管理者有可能故意改為別的)。 
sudo 套件
Q: 為何需要 sudo 套件?
A: 系統安裝設定管理都需要 root 權限但通常不建議用 root 身份做事, 
   這樣可以有多個系統管理者(super user)用 sudo 做事之後可查看 Log 釐清責任:-)
* 注意 Ubuntu 上安裝套件使用命令是 apt install
  當然可以打 apt --help 看看如何用 apt
因為需要 root (super user)才可以做安裝動作,
如果不是 root 通常使用 sudo apt install
有權限使用 sudo 的使用者稱為 sudoer(s);
這時需要先安裝 sudo 套件並設定可以 sudo 的使用者(user)。
關於設定 sudoer 通常有兩種方法:
  (1)用 usermod 把 user 加入 sudo 的 group
  (2)用 visudo 修改 /etc/sudoers 或在 /etc/sudoers.d/ 目錄下的任何檔案。
o  How To Create a Sudo User on Ubuntu(簡單)
o  How To Edit the Sudoers File(What is visudo?)(詳細)
fail2ban 套件
Q: 有壞人一直嘗試 ssh 登入我的機器要怎麼辦?
A: 安裝 fail2ban 設定參數,例如十分鐘內統計到踹我三次沒成功就 ban 掉他 17 小時或更久。
  fail2ban 是監看 log (例如 /var/log/auth.log),
然後依據設定檔(例如 /etc/fail2ban/jail.d/ggyy.conf)內的參數,
如果符合條件就下達 iptables 命令把壞人 IP 關到監獄 (Jail);
可以用 sudo fail2ban-client status sshd  查看 ssh 嘗試失敗被關的清單。
fail2ban 預設是使用 iptables 來設定防火牆(firewall),
你可以用 sudo iptables -L f2b-sshd -v -n --line-numbers 查看 Rules;
當然你可以修改設定檔讓它 action 改用其它防火牆程式。
有些系統安裝 fail2ban 時會自帶 firewalld 當防火牆,Ubuntu 預設使用  iptables 當防火牆。
How to Install and configure fail2ban to Secure Your Server
o How to set up fail2ban with UFW on Ubuntu 20.04?
ufw 其實會叫用 iptables; 所以安裝 ufw 時會自動把 iptables 也安裝(如果原先沒iptables)。


系統管理者管人、事、時、地、物 ...
Super user(system administrator) 應該要懂的 ...
(0)基本常識:了解 "man hier" 和Ubuntu 用 apt update, apt install, ...
    要會基本命令 vim, ssh, wget, curl, git
  o 點這看網頁版的 hier 關於 Linux 檔案架構
(1)管理帳號(人)
  (a)要了解這些命令:adduser/deluser, passwd、gpasswd; usermod, chown、chmod, umask
  (b)要了解 sudo 套件以及管理 sudoer
  (c)要了解 /etc/ 底下各種重要檔案用途用法
  (d)管理密碼驗證與登入    
    How to Use Two-Factor Authentication with Ubuntu(using Google PAM)
    https://ubuntu.com/tutorials/configure-ssh-2fa(Google PAM)
    o Two-Factor Authentication (2FA) Explained: FIDO U2F(Universal 2nd Factor == CTAP1)
    o What Is FIDO and How Does It Work? o Open Standard U2F - FIDO Universal 2nd Factor
    o https://fidoalliance.org/ (Official site 官方網站-history)
    o FIDO Alliance and W3C have a plan to kill the password(講很久了:-)
       ( Video: The state of FIDO 2019 ) (25分鐘)
    o FIDO2 is the passwordless evolution of FIDO U2F
    o Google chrome U2F API will be replaced by WebAuthn API on February 2022. ( WebAuthn )
    o Video: FIDO2 explained by John Craddock; 2020 (6分鐘)
    o Video: Beyond Passwords: Simpler, Stronger Authentication with FIDO2 (64分鐘)
    o Why Should We Disable Root-login over SSH?
    o 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
    無密碼登入真的比較安全嗎?理論上有道理,但是多數人不相信:-)
    o How To Create SSH Keys With PuTTY to Connect to a VPS
    o Video: FIDO2 – Creating a passwordless future (FIDO2 Authentication)(55分鐘)
    o 你會發現與 FIDO 相關影片都很少人看: Google and Microsoft Debut FIDO2 :-) (超過一萬人算多的)
    o 最多人看的這影片與 FIDO 無關: Fido (機器人) vs Spot(狗) — Animal vs Robot
    o Video: Cat Vs. Dinosaur - Cat Spooked, Then Befriends a Robot Dinosaur
    o Video: Google Authenticator 雙重認證教學 虛擬貨幣交易所、錢包必備 (2分鐘)
    o Video: 全景公司廣告 雙因素保障 WFH 遠端辦公安全 | 支援 VPN / VDI (1分鐘)
    o Video: STOP Using Google Authenticator❗(here's why + secure 2FA alternatives) (7分鐘)
    o Video: Stop Using Google Authenticator (here is the reason) (21分鐘)
    o 到底 FIDO2 是否真的能 KILL 掉 Password? ( FIDO 官方網站文章 )
    o Understanding Token-Based Authentication: A Detailed Review (frontEgg)
    o What is an authentication token? 
    o Intro to JWT - Step by Step (Json Web Token) 
    o Introduction to JSON Web Tokens (J W T)
    o OpenID Connect Protocol (Open ID)
    o Certified OpenID Connect Implementations (OpenID.Net)
    o What's the Difference Between OAuth, OpenID Connect, and SAML? 
   👉聽說 Microsoft Authenticator 比 Google Authenticator 好且安全?(2FA MS or Google)
   👉使用微軟及 Google Authenticator App 範例(C# TOTP)
   👉使用微軟及 Google Authenticator App 範例(PHP Google's OTP Authenticator)
   👉微軟帳戶兩階段驗證教學(MS)

  (e)了解 Directory Service微軟 Active Directory
    o How To Configure OpenLDAP and Perform Administrative LDAP Tasks
    o OpenLDAP Software 2.4 Administrator's Guide(官方)
    o The Difference Between Active Directory and LDAP
    o What Is and How Does Single Sign-On Authentication Work?(SSO)
    o How does single sign-on work?(SSO)
    o Difference Between SSO and LDAP (With Table)
  (f)管理 使用者的 Quota  (磁碟可使用量)
    How To Set Filesystem Quotas on Ubuntu 20.04
(2)管理網路
  (a)要懂網路基本概念:FQDN(Fully Qualified Domain Name), TCP/IP, port, SMTP, POP3, IMAP
      =>How to set up NIS for Ubuntu (Master, Client, Slave)(cluster)
      =>Installing NIS server on Ubuntu 18.04 LTS(cluster)
      =>yppasswd, ypchfn, ypchsh(command used for cluster)
  (b)網路基本設定相關: /etc/hosts, /etc/host.conf, /etc/resolv.conf, 
     /etc/inetd.confinetd, /etc/xinetd.conf 與 xinetd
  (c)要讓網路會通:ifconfig, ip, ping, netstat, route, traceroute, ifup, ifdown, arp, rarp
       =>man page for "ip" command  (ip – A Replacement for ifconfig)
       =>How to use the "ip" command in Ubuntu
       =>How to Use "ip" Command in Linux with 31 Examples
  (d)要懂 DNS 與管理:BIND, DNS(Domain Name Service), DNS 正查、DNS 反查, dig, nslookup, ...
       =>How to Install and Configure DNS Server (Bind 9) on Ubuntu
       =>How To Configure BIND as a Private Network DNS Server on Ubuntu 18.04
       ==>What Are Domain Name System (DNS) Resolvers and How Do They Work?
  (e)要會網路防火牆設定: nmap, iptables (防火牆相關), ufw(Uncomplicated Firewall)
       =>安裝 ufw 也會自動安裝 iptables(ufw is based on iptables)
       =>TCP Wrappers and xinetd, /etc/hosts.allow, /etc/hosts.deny
       =>How the Iptables Firewall Works(Linux FireWall 基礎)
       =>How to Install and Configure Fail2ban on Ubuntu(自動禁掉踹我太多次的)
       =>主機基本防護 防止爆力登入 fail2ban 簡易用法(自動禁掉踹我太多次的其它軟體)
         alias f2b='sudo fail2ban-client status sshd'  
       =>Iptables Tutorial – Securing Ubuntu VPS with Linux Firewall
       =>Firewall Introduction (iptables and ufw)(ufw is based on iptables)
       -=>What is the difference between iptables and ufw in Linux?
       =>nmap Command in Linux with Examples
       -=>How To Test your Firewall Configuration with Nmap and Tcpdump
       =>The TCP Wrappers package has been deprecated in RHEL 7 and therefore
         it will not be available in RHEL 8 or later(Red Hat 系列都取消了 TCP Wrapper)
  (f)Secure your Server
       =>10 Steps To Secure Linux Server + {Bonus Tips}
       =>40 Linux Server Hardening Security Tips
       =>8 Best DDoS Protection Service Platforms & Anti DDoS Software(要錢)
  (g)要會Debug網路: 搞懂 TCP/IP 與 socket, tcpdump, wireshark
       =>10 Tips On How to Use Wireshark to Analyze Packets in Your Network
  (h)用關鍵字問 Google 大神 :-)
       =>參看 22 Linux Networking Commands for Sysadmin
       =>參看40 Useful Linux Network Commands for Modern SysAdmins
(3)管理系統的各種服務(Service)
  (a)了解 Mail 相關設定/SMTP/POP3/IMAP等,以及網頁Server相關設定;了解 NGINX 與 Apache
  (b)了解 ssh 和 openssh-server 以及 ssl 和 openssl 相關
     o How To Create a Self-Signed SSL Certificate for Nginx in Ubuntu 20.04
     o How To Harden OpenSSH on Ubuntu 20.04
  (c)了解 serviesystemctl 命令
  (d)安裝程式語言、程式庫、各種工具套件
  (e)了解 /opt 以及如何從 source code 安裝有些應用程式
  (f)重開機:reboot, shutdown, 了解 /etc/rc.local 檔案
(4)管理系統的設備 (device)
  (a)了解設備驅動程式(Driver)的安裝與管理: lspci, dmesg, ...
  (b)了解 /dev 和 /mnt 和 /etc/fstab、/etc/mtab 以及 mount, umount 命令
  (c)備份與還原:tar, gzip/gunzip, dd, crontab, rsync, Timeshift
    How To Use Rsync to Sync Local and Remote Directories
  (d)網路檔案系統:NFS
    How to Configure NFS Share in Ubuntu
    How to set up a Samba file server(Ubuntu 官網)
    How To Set Up a Samba Share For A Small Organization on Ubuntu 
    如何在Windows 10系統連線至支援SMBv1通訊協定的網路裝置?(華碩)
    如何在 Windows 中偵測、啟用和停用 SMBv1、SMBv2 和 SMBv3 (微軟)
(5)其它:要搞懂 /var/log/ 且要會查看各種 Log 
  o How To Become a Linux Administrator
  o How to use rsyslog to create a Linux log aggregation server(can forward logs to other servers)
  o 10 skills every Linux system administrator should have
  o 20 Linux Commands for System Administrator
  o 30 Useful Linux Commands for System Administrators
  o 90 Linux Commands frequently used by Linux Sysadmins
fail2ban configure_File: /etc/fail2ban/jail.d/defaults-debian.conf
            Or   /etc/fail2ban/jail.conf   (不建議改這)
#
### 編輯 /etc/ssh/sshd_config 這個 SSH 服務的設定檔
### # 是否允許 root 管理者以 SSH 登入
### PermitRootLogin no
### 存檔後 service ssh restart
###  o Why Should We Disable Root-login over SSH?
###  o 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
###  o How To Create SSH Keys With PuTTY to Connect to a VPS
##
# 使用 fail2ban 防 linux ssh 被暴力破解
# fail2ban 是使用python 開發的,它通過監控掃描系統日誌檔案,並從中分析找出多次嘗試登入失敗的IP地址,
# 然後呼叫iptables將其 Ban 掉。當然它不僅僅只限於 ssh 服務,還可以防護 ftp、pop 等網路服務。
## 用 ubuntu docker container 測試
docker run -it --privileged ubuntu
## Install fail2ban apt update apt install rsyslog vim -y <<GGGXXX GGGXXX #do NOT remove above line! service rsyslog start apt install iptables -y <<GGGLLL GGGLLL #do NOT remove above line!! # 安裝 ssh server/client; 第一個問題回 6 表示亞洲; 第二個問題回 73 表示台北 apt install ssh -y <<GGGYYY 6 73 GGGYYY #do NOT remove above line!!! service ssh start apt install fail2ban -y <<GGGXY GGGXY # vim /etc/fail2ban/jail.d/defaults-debian.conf service fail2ban start fail2ban-client status sshd # vim /etc/fail2ban/jail.d/defaults-debian.conf # service fail2ban restart # service fail2ban status ## Fail2ban按以下順序讀取配置文件。每個.local文件都會覆蓋.conf文件中的配置: /etc/fail2ban/jail.conf /etc/fail2ban/jail.d/*.conf /etc/fail2ban/jail.local /etc/fail2ban/jail.d/*.local 對於大多數使用者而言,配置 Fail2ban 的最簡單方法是 將 jail.conf 複製到 jail.local 並修改 jail.local 文件。 不必包括相應 jail.conf 文件中的所有設置,僅包括您要覆蓋(改寫)的設置。 ### ## 用 ubuntu docker container 測試
docker run -it --privileged ubuntu
apt update apt install rsyslog vim -y <<GGGXXX GGGXXX service rsyslog start # install ssh; 回答 6 和 73 apt install -y ssh <<GGGYYY 6 73 GGGYYY service ssh start apt install -y ufw <<GGGYYY GGGYYY ufw status # ufw enable # update-alternatives --set ip6tables /usr/sbin/ip6tables-nft # ufw disable # ufw enable # ufw reload # ufw deny from 172.17.0.1 # ufw status # ufw enable # ufw reload # ufw status # ufw allow ssh # ufw show added # ufw allow 8000:8088/tcp # ufw allow 8000:8088/udp # ufw allow from 140.113.210.0/24 # ufw allow from 140.113.210.0/24 to any port 22 # ufw deny from 140.114.0.0/16 # ufw reload # ufw status # ufw status verbose # ufw status numbered # ufw disable
https://www.linode.com/docs/guides/using-fail2ban-to-secure-your-server-a-tutorial/#ubuntu
# cat  /etc/fail2ban/jail.d/defaults-debian.conf
[sshd]
enabled = true
port    = ssh
bantime = 170m ; #封鎖 170分鐘
findtime = 17m ; #十七分鐘內
logpath = %(sshd_log)s
backend = %(sshd_backend)s
##嘗試登入 n 次失敗
maxretry = 5 ; 5次失敗 就 ban 它

ignoreip = 127.0.0.1/8 ::1 140.113.210.242/24
# service fail2ban start
# service fail2ban status
# fail2ban-client status sshd
# alias f2b='fail2ban-client status sshd'

https://tkaefer.de/blog/2019/08/28/docker-and-fail2ban-i-solved-it/
https://medium.com/@ebuschini/iptables-and-docker-95e2496f0b45
https://docs.docker.com/network/iptables/
初探 IPTABLES 流動之路 - 以 Docker 為範例
https://riptutorial.com/docker/topic/9201/iptables-with-docker
Docker hosted openssh-server with fail2ban(use docker-compose)
  Fail2ban works with iptables by default.
However, installing fail2ban on CentOS 8 also installs fail2ban-firewalld
(which changes that default) .
  The basic idea behind fail2ban is to monitor the logs of common services
to spot patterns in authentication failures.
  Fail2ban works by dynamically altering the firewall rules to ban addresses
that have unsuccessfully attempted to log in a certain number of times.
  When using the default iptables target for SSH traffic,
fail2ban creates a new chain when the service is started.
It adds a new rule to the INPUT chain that sends all TCP traffic directed
at port 22 to the new chain. In the new chain,
it inserts a single rule that returns to the INPUT chain.
  IPtables and UFW both are Linux system firewalls,
the difference between them is UFW is built upon IPtables,
IPtables a very flexible tool but it's more complex as compared to UFW,
other difference is that IPtables requires a deeper understanding of TCP/IP,
which might not be the case with every Linux user.

o UFW Essentials: Common Firewall Rules and Commands Most users think IPBan is a great alternative to Fail2ban. CrowdSec is more than just a Fail2Ban replacement though, and makes it a bit confusing. See https://danielmiessler.com/study/crowdsec/
whoami
su
sudo
visudo   # 修改 /etc/sudoers  或 /etc/sudoers.d/ 內的檔案
adduser tsaiwn
usermod -aG sudo tsaiwn   # 讓 tsaiwn 可以用 sudo
apt   # apt-get
iptables -I INPUT -p TCP --dport 443 -j ACCEPT
===============  
============
o Check to see the port if it is open or Not
o Check to see my public IP
o ICANN Lookup (Whois domainname)


TOP  ToC

IoTtalk FarmDashboard可以用 MySql 也可以用 sqlite
關於 MySql 與其安全設定 (如果使用 sqlite 則這部份不用看:-)
 How To Install MySQL on Ubuntu 16.04
mysql security setting (mysql 安全設定)
   Ubuntu 安裝完Mysql的時候預設是沒有root密碼以及相關設定的,
這時候就需要先用mysql_secure_installation這個指令來對MySQL進行初始化設定。
mysql_secure_installation
若問密碼, 按 ENTER 鍵即可
然後,原則上全部回答 y 就對了:-)
如果啟動 mysql 有啥 PID 錯誤, 就 : 
sudo rm -rf /usr/local/var/mysql/*.err  
sudo service mysql restart
操作參考: 請點這去 digitalocean 看 首先登入 mysql(以root使用者登入)
mysql -u root -p
檢查資料庫的 characterset status
status 
  編碼應該要如上圖:四個都是utf8 Server characterset不是utf8的處理流程: 先按Ctrl+D登出MySQL 修改 /etc/mysql/mysql.conf.d/mysqld.cnf
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
 <註>倘若您的Ubuntu版本為14.04,請修改 /etc/mysql/my.cnf
sudo vim /etc/mysql/my.cnf  
o 關於到底改 my.cnf 還是 mysqld.cnf點以下這參考文件 https://blog.programster.org/ubuntu-16-04-default-mysql-5-7-configuration 在 vim 該 .cnf 檔案後按 i 進入insert mode 加入設定:複製下列程式碼,於 mysqld.cnf 或 my.cnf 內貼上
[mysqld]
init_connect = 'SET collation_connection = utf8_bin'
init_connect = 'SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_bin
按ESC退出insert mode 存檔並結束修改 :wq 然後重啟mysql以更新設定, 接著登入 mysql(以root使用者登入), 打 status 再次查看 characterset status
sudo service mysql restart  
mysql -u root -p
status
  TOP ToC 在 MySql 建立 (create) new user for your application # 登進mysql新增 MySQL 內的 user,允許連線 IP,與資料庫( db_name ),以及權限 : CREATE USER 'user_name'@'%' IDENTIFIED BY 'user_password'; # 設定new user的user_name(e.g dashboard),允許連線的IP( % 代表允許任一IP連線)和user_password 注意, password 千萬不要含有 @ 阿不然有大麻煩 !!! 這裡的 user_name 是指 mysql 內的 user, 與 Linux user name 無關, 也不必要叫做 dashboard, 但這是要用來放在 FarmDashboard 的 config.py 內設定 DB_CONFIG = ' ... ' 用的,大約在 config.py 的 LINE 10 ~ LINE 13處。 # 看看已經有哪些 user SELECT * FROM mysql.user; # 給予使用者權限 privileges for new user GRANT ALL PRIVILEGES ON *.* TO 'user_name'@'%'; flush privileges; 說明: ALL PRIVILEGES: allow the user to read, edit, execute and perform all tasks across all the databases and tables. *.*:The asterisks in this command refer to the database and table that they can access. (* represents that all databases and tables are available) @'%': allow the user from any IP flush privileges; #:刷新權限以更新權限設定 詳細 mysql 的 user 與 權限 (Permissions)請參考這:   https://www.strongdm.com/blog/mysql-create-user-manage-access-privileges-how-to 建立給Application使用的database, 設定db_name (e.g dashboard) create database db_name; 例如: create database dashboard; TOP ToC
與 mysql 有關的一些常用 Linux 命令 # Start the MySQL service normally. sudo service mysql start # 或 sudo systemctl start mysql #If you are receiving the following error message: MySQL services are masked # Do the below commands sudo systemctl unmask mysql.service sudo service mysql start # Stop MySQL sudo service mysql stop # To restart MySQL service sudo systemctl restart mysql # 或這樣 sudo service mysql restart # To verify if the MySQL service is started or not sudo systemctl status mysql # 或這樣 sudo service mysql status # To check the MySQL version mysql --version # To enable MySQL to start automatically on every reboot sudo systemctl enable mysql # Turn off MySQL. sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown # reset mysql root password sudo mysqladmin -u root password # Set MySQL root user password and policy, RUN mysql_secure_installation script sudo mysql_secure_installation # (原則上都回答 y 就是了:-) # Start MySQL manually, without permission checks or networking. #(通常是因忘了密碼用來進入 mysql 內修改 root 或 user 的密碼用, 正常不建議!) sudo mysqld_safe --skip-grant-tables --skip-networking & # 執行上句子後, 可以不用輸入密碼直接連入 mysql mysql -u root ###### ##### 如果前述 start MySQL 有錯誤, 可以做以下兩步驟後重來: # Make MySQL service directory. sudo mkdir /var/run/mysqld # Give MySQL user permission to write to the service directory. sudo chown -R mysql:mysql /var/run/mysqld
常用的 SQL commands: # Log in without a password. mysql -uroot mysql
*** Note that MySQL command is not case sensitive.
    But also note that database name/table name/column name ARE Case sensitive.
 create database [database_name];
 show databases
 use  [database_name];
 show tables
 SELECT * FROM [table_name];
 drop table [table_name];
 drop database [database_name];
***mysql Commands summary # Update the password for the root user after you login into mysql. UPDATE mysql.user SET authentication_string=PASSWORD('ENTER_YOUR_PASSWORD_HERE'), plugin='mysql_native_password' WHERE User='root'; flush privileges; # 登出 mysql exit; # 重新啟動 mysql sudo service mysql restart ## 其實不必進入 mysql 那麼麻煩, 直接以下這樣重設 mysql root 密碼比較簡單: sudo mysqladmin -u root password MySQL Tutorials... MySQL Tutorial @ W3Schools.com MySQL Reference Manual MySQL 官方網站 SQLAlchemy 1.4 Documentation (ORM) [python] sqlalchemy 閱讀筆記 SQLAlchemy筆記(ORM 物件與關聯教學) Flask-SQLAlchemy 資料庫操作-ORM篇(二)

TOP  ToC
FarmDashBoard for IoTtalk
準備用 git clone 抓取並安裝 Farm DashBoard( install dashboard )
 然後 connect dashboard to mysql  (or sqlite)
  ( FarmDashBoard 現在需要取得授權才可以下載 Source code,
      請 Email 給交大 "林云蔚博士"<jyneda@gmail.com> 詢問如何取得授權。  ) 

### ###  關於FarmDashboard for IoTtalk 的安裝與設定請點這參考


看到以上畫面,代表dashboard安裝設定完成

 
TOP ToC
摘述關於安裝 FarmDashboard
(1)安裝需要的工具與 Python 程式庫套件
(2)修改 config.py
(3)修改 在子目錄 db 內的db_init.json
(4)修改 /etc/mysql/mysql.conf.d/mysqld.cnf
(5)資料庫初始化 python3 -m db.db init
(6)啟動 dashboard Server 和它需要的 DA:
bash startup.sh      
說明:
   通常我們把可能要改的儘量寫在 project 根目錄的 config.py,
要使用 sqlite 或 mysql 可以修改 project 根目錄的 config.py
vim config.py
用註解寫範例句子讓使用者參考,使用者 copy 註解掉的去用即可

# DB_CONFIG = '<database>[+<orm_lib>]://[<user>[:<password>]]<host>[:<port>]/[db_name][?charset=utf8]'
# ex: DB_CONFIG = 'mysql+pymysql://user:pass@localhost:3306/dashboard?charset=utf8'
# ex: DB_CONFIG = 'sqlite+pysqlite:///db.sqlite3'
DB_CONFIG = ' see above 2 lines'

#注意用 mysql 時 user:pass 就是你在MySQL的帳號:密碼 請不要用引號夾住!!!

如果使用 MySql 需要安裝並設定 mysql,
如果使用 SQLite 則 SQLite 已經內建在 Python3 內!
SQLite is a self-contained, file-based SQL database.
SQLite comes bundled with Python and can be used in any of
your Python applications without having to install any additional software.
See How To Use the sqlite3 Module in Python 3

### ### 關於FarmDashboard for IoTtalk 的安裝與設定請點這參考
TOP ToC

o sensors(IDF/ODF) 

必要時, 在iottalk server的GUI上新增對應IDF與ODF
(e.g http://140.113.199.213:7788/dfm)
# 如果需要的 IDF name 和 ODF name 都已經有了, 就不必新增了 !
# 如果對 DFM/DMM 不熟悉請參看IoTtalk使用手冊的(七):
   https://iottalk.vip/000/#AddDM
  (包括文字說明和兩個參考影片。)

IDF 資料要有 DA 從 Sensor 取得資料送去 IoTtalk Server
當然, DA 也可能是你自己電腦上寫的爬蟲程式去氣象局抓資料,送去 IoTtalk server 的 IDF。
也可能是在 IoTtalk server 上跑的程式去 Arduino Yun 或廣達的 Sensor Box 取得資料。
## 關於抓氣象資料的爬蟲程式,可以參看以下這
   交通部中央氣象局V8版網頁爬蟲
   https://github.com/IoTtalk/Crawler_CWB_V8
## 關於該爬蟲程式範例解說,可參看以下影片:
   
TOP ToC
o ODF -- 在iottalk server的GUI上新增對應DM DataServer 勾選需要的 ODF, 必要時須新增 DF_name
  TOP ToC
多語系(i18n)使用說明 ..  (from FarmDashboard README.md)
文字準備
## python
# use gettext('') to the needing change words.
from flask_babel import gettext
msg = gettext('Babel is good.')
# or if you want to use constant strings somewhere in 
# your application and define them outside of a request, 
# you can use a lazy strings lazy_gettext('').
from flask_babel import lazy_gettext
msg = lazy_gettext('Babel is good.')

## Javascript
# use {{ _('') }} to the needing change words. for example:
{{ _('System Management') }}
### 使用語言包 (Babel) ## 首次使用 #1 - 將所有 python 及 html 所用到的字串擷取出來: pybabel extract -F app/babel.cfg -o messages.pot . #2 - 建立字典檔 (儲放於 app/translations 下): pybabel init -i messages.pot -d app/translations/ -l #3 - 翻譯文字,修改前一步產生的 po 檔,翻譯對應語系的文字,檔案路徑為: app/translations//LC_MESSAGES/messages.po #4 - 編譯字典 po 檔成 mo 檔,供 babel 使用: pybabel compile -f -d app/translations ## 更新字典檔 (與首次使用相同,差別在於第二步的 update 用 update 取代 init) #1 - 將所有 python 及 html 所用到的字串頡取出來: pybabel extract -F app/babel.cfg -o messages.pot . #2 - 更新字典檔: pybabel update -i messages.pot -d app/translations/ -l #3 - 翻譯文字,修改前一步產生的 po 檔,翻譯對應語系的文字,檔案路徑為: app/translations//LC_MESSAGES/messages.po #4 - 編譯字典 po 檔成 mo 檔,供 babel 使用: pybabel compile -f -d app/translations
TOP ToC
o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹

TOP ToC
Q&A   Q&A    Q&A

Q1:如果 docker pull 或 docker run 出現如下的錯誤訊息,只要 docker login 就可以了。
docker: Error response from daemon: toomanyrequests: You have reached your pull rate limit.
You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit.
See 'docker run --help'.
A1: 只要 docker login 登入你 hub.docker.com 的帳號就可以了。
docker login

Q2:Docker 終端機內如何 Copy/Paste ?
A2:用滑鼠可 mark 終端機內一段文字,然後敲 CTRL+Insert 複製(Copy),
   CTRL+Shift+V 可以貼上 (Paste)

Q3:Docker 的終端機螢幕可否變大? 字型太小可否變大?
A3: ALT+ENTER 可切換終端機變全螢幕或變回瀏覽器視窗內。
    CTRL+=  字型變大 
    CTRL+-  字型變小 
 ** 請注意字型變大後如果使用 nano 或 vim 等編輯器時可能位置會不準確。

Q4:我要 docker push 怎都出現 Error 不讓我 push 上去?
A4: 規定要先 tag 成 username/image:tag 形式才能 push; 例如:
docker tag tsaiwn/ubuntu ggyy  # 先把原先的 tag 成 ggyy 才能移除 tsaiwn/ubuntu
docker rmi tsaiwn/ubuntu   # 這時其實只是 untag 因它與 ggyy 的 iid 相同
docker tag oknew tsaiwn/ubuntu   # 新版本 oknew 要 tag 成 username/image[:tag]
docker login    # 必須 Login 到 docker hub 後才可以 push 上去
docker push tsaiwn/ubuntu   # 沒 tag 就用是 :latest 當 tag
   
Q5:我可以 docker run 一個 FreeBSD 的 image 嗎?
A5: 不行啦!因為 PWD 的 docker 是基於 Linux 系統。
   如果你要跑 FreeBSD 的 docker image 那必須在 FreeBSD 系統上,
   在 Windows 上可以先安裝 VirtualBox 虛擬機器然後安裝 FreeBSD 外來系統。


Q6:如果出現如上圖的錯誤訊息,表示找不到虛擬隔離環境。
A6: 建立虛擬隔離環境 venv
這是因為 startup.sh 腳本內要使用 project 根目錄下 venv 的虛擬隔離環境。

o 在 project 根目錄下建立虛擬隔離環境 venv
python3 -m venv venv
o 激活虛擬環境: 
source venv/bin/activate
o 重新安裝requirements.txt 內的套件
   因為進入venv後等同於進入一個新環境,故須重新安裝相關檔案。
pip install -r requirements.txt
重啟 your application 


倘若還是無法正常連線,請重啟VM試試
o 重開機
sudo reboot

Q7:怎知道 缺ping 要 apt install iputils-ping 呢?
A7: 問 google 大神 或用 apt-file search 如下:
apt-file search -x "/bin/ping$"
## -x 表示用 regular expression;  ping$  表示結尾 是 ping
## 不過 apt-file 需要另外安裝
## 然後要做apt-file update 這時會抓很多資料回來且空間佔用很大喔!
## (大約 600MB; 而最新版的 ubuntu docker image 也才 72MB)
## (當然搜尋也很花時間,到 google 查別人答案可能比較快:-)


References
o 關於 IoTtalk DFM (Device Feature Management)設備功能的管理


o 關於 IoTtalk DMM (Devie Model Management) 設備模型(類別)的管理
o Build your own IoT Dashboard ( using FarmDashboard) IoTtalk Dashboard 結合影像應用於遠端環境監控系統(1) Part 2 IoTtalk Dashboard 結合影像應用於遠端環境監控系統(2)
TOP  ToC
* IoTtalk 簡介:
IoTtalk 系統交通大學林一平博士帶領研發團隊開發的智慧物聯網應用開發平台, 提供業者一個整合、開放、低成本、高效率, 且能串聯不同規格標準的物聯網應用(例如智慧家庭、智慧校園、智慧農場)平台林一平教授曾任科技部次長,交大副校長林一平教授興趣多元,喜好藝術繪畫寫作遨遊於科技與人文間自得其樂,參看林一平教授的臉書(fB),以及林一平教授的Digitimes專欄文章。(點這看林一平教授寫的『大橋驟雨』以及關於歌川廣重的『大橋安宅驟雨』;還有,看看林教授該書中提到晚年住在亞爾Arles的梵谷模仿大橋驟雨的『雨中橋』油畫)
    目前這 IoTTalk 系統平台已經成功用於智慧校園智慧農業, 例如用於種植薑黃(5分鐘影片) (參看 今週刊報導(2019.01.23)農譯科技公司 AgritalkTech.com/ 以及分家出去的 AI_農業 Agritalk.com.tw/)(現已合併回來) (arduino.cc 分家出 arduino.org 訴訟後又和解+合併)
oooooooo

FarmDashBoard 現在需要取得授權才可以下載 Source code,
請 Email 給交大 "林云蔚博士"<jyneda@gmail.com> 詢問如何取得授權。
如果你使用FarmDashboard發現Bug或有建議請Email給"顏泰翔博士"<ksoyam95@gmail.com>


TOP ToC
常用與次常用 Unix / Linux Commands 照字母排列
完整 Linux Manual pages   Linux 這字如何念?
(1)建立 user 帳號與其相關資料
adduser tsaiwn  # 建立 tsaiwn 這帳號以及相關目錄檔案等
# 也參考 useradd (只建立帳號) -- 不建議使用

(2)化名 alias 用來簡化命令的輸入(少打幾個字:-)
alias ll='\ls -al'   # 讓 ll 等同 ls -al
alias dk=docker  # 這樣打 dk 等於打 docker
alias   # 列出目前有哪些 alias commands

(3)套件安裝管理
apt install -y vim  # install vim and -y means don't ask
# Alpine Linux 用 apk
# RedHat/CentOS/Fedora 用 rpm (Redhat Package Manager) 或 yum

(4)cat # 查看檔案內容 (Windows 用 type filename)
cat ggyy  # show content of file ggyy
cat /etc/os-release   # show OS information
cat -n /etc/passwd
cat aaa bfile cfile > myNewFile   # 把三個檔案串接後存入 myNewFile 內
wc aaa bfile cfile myNewFile   # word /line count
tail /etc/passwd |  cat -n    # 先 tail 取後 10 列再交給 cat 編號
cat -n /etc/passwd | tail   # 先編號再交給 tail 取後面10列

(5)cd ## Change Directory
cd haha  # 進入 haha 子目錄
cd ..   # 回上一層目錄
cd /home/tsaiwn  # 進到該目錄
cd ~tsaiwn  # 到 tsaiwn 的 Home directory, 通常就是 /home/tsaiwn
# 相關指令有 pwd, pudhd, popd, mkdir, rmdir, chmod -x directory, chmod +x directory
pwd  # see where am I ; print working directory
pushd .  #  記住目前的目錄 (在 stack) 其實不必 pushd .
pushd /etc    # 記住目前目錄並且 cd  到 /etc
pwd  # see where am I; should be /etc
popd    # 恢復到前面做 pushd 時的目錄
pwd

(6)chmod  ## CHange MODe of file or directory
chmod +x ggyy  # make ggyy Runnable
# +x means eXecutable fpr file
# and +x means can cd into it if it is a directory

(7)chown  ## CHange OWNer of file/directory
chown tsaiwn theFile  # change theFile owner to tsaiwn
chown -R tsaiwn thatDir  # change owner of thatDir recursively
## also see chgrp for change group

(8)cp   # CoPy file and/or directory
cp gg yyy    # copy gg to yyy
cp -p gg yyy  # copy gg to yyy BUT preserve file property, for example, creation datetime
cp -p -R ggd yyd  # copy recursively

(9)curl   # 文字版瀏覽器, 可以模仿溜覽器, see also wget
curl https://www.nthu.edu.tw
# curl --help
# 參考 wget 命令
(10)df # 查看 how many free space on disk df # disk free space (11)diff # compare two files diff filea fileb # difference between 2 files (12)deluser # 砍掉 user 帳號以及它的檔案 deluser # delete user interactively # see also userdel, useradd, adduser (13)du directory # 查看 disk usage du . # (disk usage)show how much disk used in . current directory (14)echo message # print message echo "message" # print message (15)exit 離開或結束 exit # leave current shell (若是最後一個 shell 會 Logout) (16)查看檔案類型 file myfile.txt # 查看這 myfile.txt 是哪種檔案; 注意與附檔名無關 !! # Windows 上的檔案通常用 .xxx 附檔名判斷;但 Unix / Linux 不是! (17)fg # foreground fg # for ground # 把最近被踢到 background 的 job 叫回來(attach) # 執行程式時如果最後打 & 表示在 background 執行, 立即讓出終端機 # 敲 CTRL+Z 可以把目前的 job 踢到 background, 讓出終端機 # 如果 telnet/ssh 遠端連入, 要先打 ~符號再敲 CTRL+Z 把連線暫時踢到 background (18)find # 找尋符合條件的檔案 find /usr -name "*ggyy*" # find file name *ggyy* in /usr/*... (19)git 須要用 apt install -y git git clone https://github.com/... # 也可抓回 .zip 再解壓縮 (20)grep ## 過濾出含有某些字串的列 grep "ggyy" * # find file that contains ggyy (21)groups username -- show group of username groups root groups tsaiwn cat /etc/group | grep tsaiwn (22)壓縮與解壓縮 gzip / gunzip /zip / unzip # zip/unzip files (23)查看前幾列 head -12 myfile # show first 12 lines (24) 查看以前打過哪些命令 history # show command history # !38 重複執行編號 38 的命令 # 可以用 !38:p 列出編號 38 的命令且變成最近打過的命令但不執行 # 這樣可以接著按往上鍵然後修改其中某些字; 這時 CTRL_A 到最左, CTRL_E 到最右) (25)查 IP 和 domain name hostname -i # show IP address of current machine hostname -I # 可能不只一個 IP address hostname # 秀出 hostname (26)ifconfig 須安裝 net-tools: apt install net-tools ifconfig # show interface config information #apt update #apt install -y net-tools (27)ip replace the old command ifconfig # 注意以下 apt install ip ip --help #apt update #apt install -y iproute2 #apt-file search --regexp 'bin/ip$' ### iproute2: /bin/ip ### iproute2: /sbin/ip (28) jobs # show running jobs kill %2 # 殺死用 jobs 看到編號 2 的 process (注意 2 不是 PID) # 正在執行程式時可以敲 CTRL_Z 把它踢到背景(background); 此時打 jobs 看看 # 然後再跑另外一個程式, 再敲 CTRL_Z 踢它到背景;然後再次打 jobs 看看 # 這時應該看到兩支被踢到背景的; 打 fg %1 可以叫回 (attach) 第一個 process # 打 fg %2 當然是叫回用 jobs 看到編號 2 的 process (注意不是 PID) (29) kill pid # 殺死 process ID 為 pid 的 process (跑起來的程式稱 process 行程; 進程) kill 3388 # kill process whitch pid is 3388 kill -9 3388 # 如果前面那句莎不掉, 加個 -9 用力殺 :-) (30) less file # 慢慢看 file 內容,類似 more 但可以往回捲動 # 可以打 less --help; 通常 less 須要另外安裝 # 這 less 其實是根據 more 改寫的; 打 more --help 看看 # 號稱 less is much better than more # apt update # apt install less (31) locate ggyy # lsearch filename contains ggyy (32)查看檔案, 類似 Windows CMD 的 dir ls ls -al ls -alF ls -l (33)程式設計師常用 make 來 build 出執行檔和做 install 動作(要寫 makefile) make # do commands in makefile; see man make make clear # clear 是在 makerfile 內的一個 標記(Label) clear: make install # install 是在 makerfile 內的一個 標記(Label) install: # 當然通常該標記下方通常是做安裝的一些命命 (copy file to system folder) # 例如, 建立一個檔案 makefile 放入以下內容 clear: echo not clear install: echo I love you! # 然後打 make install 看看會怎樣 ## 注意 makefile 檔案中各命命的左邊是敲 TAB 不是打 8 個空格! # 使用 IDE 的則用 project file 取代 makefile (34)查看手冊 man intro # manual introduction to chapter 1 commands man 2 intro # manual introduction to chapter 2 system calls man 3 intro # manual introduction to chapter 3 functions man 6 intro # manual introduction to chapter 6 games man 8 intro # manual introduction to chapter 8 privileged commands (for super user) man hier # 查看 Unix /Linux 檔案結構,各目錄的用途 ... man 5 proc # process information pseudo-filesystem man bash man man # manual (35)建立目錄 (directory) mkdir ggyy # create directory ggyy mkdir -p /etc/abc/ggyy # -p 表示如果 parent directory 不存在就一併產生 (36)more file # 讓 user 慢慢看 file 內容; 按空白鍵顯示下一頁;按 ENTER 鍵往下一列 ## 另外有 less 號稱比較好用; 因為可以往回捲 ! (37) Rename 或 移動檔案 mv fa fok # rename fa to fok mv /tmp/gg myfile # 把 /tmp 的 gg 移動到目前目錄下並改名 myfile (38)Octal dump 用八進位方式傾印檔案內容 od test # dump file test in octal format (印成八進位) (39)改密碼 passwd # change my password sudo passwd tsaiwn # change password of tsaiwn (40)查看某機器是否活著且網路有通 ping mit.edu # 收到請回答 # need to do: apt install iputils-ping ping 140.113.1.254 # ping NCTU/NYCU #apt update #apt install apt-file #apt-file update #apt-file search --regexp 'bin/ping$' # where is the ping command ### netutils-ping: /bin/ping ### iputils-ping: /bin/ping # we use this iputile-ping (41)切換目錄相關 popd # cd 到之前做 pushd 時的工作目錄(從堆疊內 pop 出來給 cd 用) # 相關命令 cd, pushd, pwd, mkdir (42) 也是切換目錄相關, 把目前目錄推入堆疊並做 cd 到新目錄 pushd /etc # 先記住目前的目錄(推入 stack)並 cd 到 /etc # 相關命令 cd, popd, pwd, mkdir # 如果用 cd - (減號) 回到剛才目錄則堆疊內仍有記住並沒 pop 出來! (43)查看目前目錄(current directory)名稱路徑 pwd # shere am I now; Print Working Directory # 相關命令 cd, pushd, popd, mkdir (44)查看 process ps # show running process ps -aux (45)砍掉檔案或目錄 (ReMove file or directory) rm # rmove file -- 危險 ! 殺了就不見了! 最好用 rm -i file(s) rm -i ggyy # remove ggyy but will ask you to commit rm -i g*.py # 互動問答砍掉 g 開頭所有 .py 檔案 alias rm='\rm -i' # 這樣比較不會不小心砍了不該砍的檔案 alias del='\rm -i' # del 檔案 -- -i 這樣比較不會不小心砍了不該砍的檔案 rmdir abcd # remove the directory abcd (must be empty) (46)Secure (remote) CoPy scp haha.png tsaiwn@iottalk.vip:/tmp # copy haha.pnt to /tmp on iottalk.vip via user tsaiwn scp tsaiwn@iottalk.vip:/tmp/abc.png . # copy remote file to current directory (47)執行腳本檔案(Script file) source myfile # run script file myfile source venv/bin/activate # run the script activate (to activate a python venv) (48)secure telnet to remote machine; 須要 apt install -y openssh-server ssh tsaiwn@iottalk.vip # 儘量不要用 telnet 遠端連線, 改用 ssh (BBS 很多用 telnet) # apt update # apt install -y openssh-server # 若要讓別人遠端連入此機器, 安裝後還須 service ssh start # 因 Docker 開機不會走 init 程序, service ssh start 可能要放 .bashrc 內才可自動啟動 sshd 服務 # How Is SSH Different From Telnet? (49) su tsaiwn # become the user tsaiwn (need password if you are not super user) sudo su tsaiwn # NOT need tsaiwn's password whoami su # try to become root exit # leave current shell (若是最後一個 shell 會 Logout) (50) sudo # 用 super user (root)身分執行; 要先安裝 sudo 且設定 sudoers (51)查看檔案後面幾列 tail -18 myfile # show last 18 lines tail /etc/passwd | cat -n # 先 tail 取後 10 列再交給 cat 編號 cat -n /etc/passwd | tail # 先編號再交給 tail 取後面10列 # tail --help 看看 tail 用途用法 # Also 參考 head 命令 (52)備份(archive)與還原 (eXtract) tar cvf mytar . # archive tar tvf mytar # list files in tar tar xvf mytar # eXtract files in mytar (53) top # show running status ... # 參考 ps -aux (54)建立空檔案或改變檔案最後修改的日期時間 touch ggyy.c # create file or modify file creation datetime make # do commands in makefile # 這裡的情境是讓 make 發現 ggyy.c 比其 object 檔案新, 強迫做編譯 (compile) # 因 touch ggyy.c 會把 ggyy.c 的修改日期時間 改為現在 (55) uname # print os kernel name (Linux) # 參考 cat /etc/os-release (56)壓縮解壓縮 unzip / zip / gzip /gunzip # zip/unzip files (57)類似 adduser 但不建議用這 useradd useradd test # create user account test # 若是建立給人用的帳號, 建議用 adduser # 因為用 useradd 只建立帳號, 沒做相關準備事項, 沒建立該 user 的 home directory (58)類似 deluser 但不建議用這 userdel userdel test # delete user account test(only the account) # 建議用 deluser, 因 userdel 只砍掉帳號 (59)usermod :: modify user account data usermod -aG sudo tsaiwn # so that tsaiwn is an sudoer -- can use sudo # apt update # apt install sudo # install sudo utility # adduser tsaiwn usermod -aG sudo tsaiwn # add tsaiwn into sudo group so that tsaiwn can use sudo groups tsaiwn # show user tsaiwn's group(s) cat /etc/group | grep tsaiwn # group information is in /etc/group # Press here to see How To Create a Sudo User on Ubuntu? # More about sudo and /etc/sudoers and /etc/sudoers.d/ (60)wc -- Word Count -- Count char/word/Line in file wc testFile # how many chars/words/Lines in file testFile cat aaa bfile cfile > myNewFile # 把三個檔案串接後存入 myNewFile 內 wc aaa bfile cfile myNewFile # word /line count wc /etc/passwd (61)秀出目前 user 的 username whoami # show user name cd pwd (62)抓取某網址的檔案或網頁 wget 網址 # 從網址抓回檔案 # 或用 curl 也可以 # 參考 curl (63)壓縮解壓縮 zip / gzip / unzip /gunzip # zip/unzip files
前面說過Unix/Linux有些命令是內建(builtin)在 shell 命令解譯程式的,例如 bash 或 tcsh 等。
  (註: csh 和 tcsh 的語法類似 C 語言;當然是指寫 Scrip 時用到 if/for/while 時)
bash 的內建命令有哪些可以打 man bash-builtins 查看。(要有安裝 Manual pages)
最基本的外部命令通常 一開始就被放在 /bin 和 /usr/bin 目錄內,
ls -l /bin | more # "pipe" 給 more 慢慢看 /bin 底下有哪些東東 !
which ls
which alias # alias 是 shell 內建命令
man bash-builtins # 查看 bash 的內建命令有哪些
which rm # 殺檔案建議用 rm -i 檔案
alias rm='\rm -i' # 這樣以後 rm filename 會先問你確定嗎???

但有些命令須要另外安裝,若遇到系統說不認識該命令,就:
sudo apt update # super user 或 root 不必打 sudo
sudo apt install 該命令 # 不過有時可能不是這樣直接,例如 ping 要 apt install iputils-ping
## 善用 google 搜尋 :-)

通常系統管理者安裝的程式會被放到 /usr/local/bin 裡面。
o 如果是程式庫(Library),系統預置的當然放 /lib 和 /usr/lib 裡面,
系統管理者安裝的會被到 /usr/local/lib 這目錄裡面。
常用 Unix / Linux Commands 照字母排列
o 完整 Linux Manual pages   Linux 這字如何念?
o o o
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o Python 官方網站:https://www.python.org/      Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive
Ubuntu is derived from Debian.
It means that Ubuntu uses the same APT packaging system as Debian and shares a huge number of packages and libraries from Debian repositories. It utilizes the Debian infrastructure as base.
There are hundreds of Linux distributions, only a handful of them are independent ones, created from scratch. Debian, Arch, Red Hat are some of the biggest distributions that do not derive from any other distribution.

o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹


TOP  ToC
Unix File System structure
    /   -- This is the root directory.
    /root -- This is the home directory for the root user (optional).
    其他 user 的 home directory 放在 /home/username
    /etc/ 裡面有很多重要系統檔案,例如 passwd 等。
          /etc/passwd # 記錄 user 相關資料包括其 home directory 以及 Login 用的 shell
          /etc/group # 記錄 group 相關資料包括其 gid 以及哪些 user 屬於該 group
          /etc/shadow # user 密碼 (有加密) # 可能被系統管理者改為別的名稱
          /etc/hosts # maps hostnames/alias to IP addresses
          /etc/host.conf # name resolver; specifies how host names on a network are resolved (see man bind)
    /bin/ 裡面是最常用命令的程式檔案
    /usr/bin/ 裡面放第二常用命令的程式檔案
    /usr/local/bin/ 通常是各系統管理者安裝進去的
    /sbin/ 和 /usr/sbin/ 和 /usr/local/sbin/ 當然就是給 super user 用的命令
在 Unix/Linux 的終端機 可打 man hier 查看說明(要系統有安裝 manual)。
點這查看網頁版 的 hier (7) (7) 表示第七章。
o 看 Linux manual pages 網頁版(建議先看各章節介紹)
      man 1 intro   man 2 intro   man 3 intro   man 4 intro   man 5 intro   man 6 intro   man 7 intro   man 8 intro


o How to understand the Ubuntu file system layout?
o Differences between /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin
o /bin vs. /usr/bin vs. /usr/local/bin vs. /sbin and /usr/local/sbin ...

o hier(7) - description of the file system hierarchy(man page)

點(.) 和 點點(..) 以及 點(.) 開頭的檔案
點(.) 代表目前的目錄 (current working directory)
點點(..) 代表上一層的目錄
點(.) 開頭的檔案 代表隱藏檔, 意思是用 ls 或 ls -l 都不會看到它,
    要用 ls -a 或 ls -al 才看得到!
    通常, 點(.) 開頭的檔案習慣上是應用程式的初始設定檔: 例如 .vimrc 和 .nanorc 等;
    或 Shell 跑起來和結束要做的命令檔案: 例如 .profile 和 .bashrc 以及 .login 和 .logout 等。
還有,習慣上應用程式跑起來時會先讀取系統層級(system wide)的設定檔, 再讀取使用者家目錄內的設定檔,
    例如 vim 跑起來會先讀取 /etc/vim/vimrc (或有些系統在 /etc/vimrc )
然後再讀取 ~/.vimrc   # 注意 ~/ 代表目前 user 的 home directory (家目錄)    
    再例如 screen 跑起來會先讀取 /etc/screenrc 再讀取 ~/.screenrc  
( Screen is a terminal multiplexer,
  which allows a user to access multiple separate terminal sessions
  inside a single terminal window.)
o 另外一個類似的是 tmux 這也是要安裝才有的命令。
注意到了,放 /etc/ 的 *rc 檔案通常故意不用點(.)開頭!
至於尾巴用 rc 結尾也只是習慣,代表 Run Command 的意思!
o 關於 Shell 跑起來和結束會做哪幾個檔案請點這跳到前面看。


Linux File System/Structure Explained!

The mind behind Linux | 專訪 Linus Torvalds (Linux 和 git 的作者)(TED)
Tech Talk: Linus Torvalds on git that he created on 2005
  (Linus Torvalds visits Google to share his thoughts on git)
=> Top 5 Most Stable Linux Distributions in 2022
          (Debian, Linux Mint, Ubuntu, Fedora, OpenSUSE)
=> Debian vs Ubuntu: What’s the Difference?(Ubuntu is is based on Debian)
=> 50 Best Linux Distros for 2022(Ubuntu, Fedora, Linux Lite, CentOS-stream, ...)
o => List of Linux distributions(維基百科)
o 關於 Linux kernel 的 Opensource 可到 Github 抓
o Git 也是Linux作者Linus Torvalds寫的, 這是 Git 的 Opensource(Mirror)
o 微軟在2018年6月以75億美元收購GitHub.com(Github 2008年2月上線)
o Python 官方網站:https://www.python.org/      Python Source Releases
o 與Python相關, 17 Popular Python Opensource Projects on GitHub
o Perl: https://www.perl.org/   Perl Download   CPAN -- Perl Archive
Ubuntu is derived from Debian.
It means that Ubuntu uses the same APT packaging system as Debian and shares a huge number of packages and libraries from Debian repositories. It utilizes the Debian infrastructure as base.
There are hundreds of Linux distributions, only a handful of them are independent ones, created from scratch. Debian, Arch, Red Hat are some of the biggest distributions that do not derive from any other distribution.
o o 線上圍棋網站 OGS 以及 關於 OGS 的介紹
TOP  ToC
You are the -th visitors.       、,:;。「︓」『』?!  * ★ ※ ──  🦋 👉 ※ ★ ? 😇👍 I'll be back!』 《 Bite me! 》! 
 ==>What is NFT (Non-Fungible Token) ?    (什麼是 NFT?)
* 想學 Python + Flask 請點這     Jinja2 Jinja2 Doc
  **點這看我寫的超簡單 HTML 網頁教學(可搜尋 HTML + tsaiwn 找到)   *也有要錢的瀏覽器 (沈修平博士)
* 點這看如何選擇 VPS ?     (建議用 DigitalOcean 或 Hostwinds.com 流量小可以先用免費的 AWS EC2)     C++11 vs. 14   14 vs. 17     OppO R17 vs. R17Pro

* PuTTY officeal site to Download PuTTY
* PieTTY project official site   Download Pietty0400b14.zip

* 取妻當取李子柒? 不說話的網紅年賺多少館長也很她ㄟ!唱歌好聽的阿冷以及台灣任何網紅還要紅的李子柒李子柒何許人 i 網紅 阿冷演講 沙漠骆驼
   李子柒、辦公室小野谷阿莫...谷阿莫這群人、蔡阿嘎小玉、以及黃氏兄弟讓大家相信努力就可賺到Youtube的錢。 排名點閱排名也是排名   第一名   黃氏兄弟
  *  關於林一平教授可以參看林教授個人網站林教授Digitimes專欄林教授的書『大橋驟雨』 (林一平fB臉書大戰宅神)
      創意真的偷就有了!   👉 沒有是非的時代誰造成的 ( 要有善良的心, 請思考我們與惡的距離 !)