Category: Linux

  • 重新審視 XSS 攻擊的可能

    重新審視 XSS 攻擊的可能

    事源近日同同事閒談時,她說女兒學校比黑客入侵,全校系統停擺對外對內都是。

    所以我又重新審視 kenng.hk 現有系統的安全性,雖然我的網站內的資料沒有什麼價值,但都希望學習加強保安的知識。

    跨網站指令碼(英語:Cross-site scripting,通常簡稱為:XSS)是一種網站應用程式的安全漏洞攻擊,是代碼注入的一種。它允許惡意使用者將程式碼注入到網頁上,其他使用者在觀看網頁時就會受到影響。這類攻擊通常包含了HTML以及使用者端手稿語言

    XSS攻擊通常指的是通過利用網頁開發時留下的漏洞,通過巧妙的方法注入惡意指令代碼到網頁,使使用者載入並執行攻擊者惡意製造的網頁程式。這些惡意網頁程式通常是JavaScript,但實際上也可以包括JavaVBScriptActiveXFlash或者甚至是普通的HTML。攻擊成功後,攻擊者可能得到更高的權限(如執行一些操作)、私密網頁內容、對談cookie等各種內容。

    更多詳情可以參考以下網站:

    https://www.cloudflare.com/zh-tw/learning/security/threats/cross-site-scripting

    https://baike.baidu.com/item/XSS%E6%94%BB%E5%87%BB/954065

    我自己就使用 CSP 來做一些保障措施 , 那麼什麼是 CSP 呢?

    内容安全策略(CSP)

    内容安全策略CSP)是一个额外的安全层,用于检测并削弱某些特定类型的攻击,包括跨站脚本(XSS)和数据注入攻击等。无论是数据盗取、网站内容污染还是恶意软件分发,这些攻击都是主要的手段。

    CSP 被设计成完全向后兼容(除 CSP2 在向后兼容有明确提及的不一致; 更多细节查看这里 章节 1.1)。不支持 CSP 的浏览器也能与实现了 CSP 的服务器正常工作,反之亦然:不支持 CSP 的浏览器只会忽略它,如常运行,默认为网页内容使用标准的同源策略。如果网站不提供 CSP 标头,浏览器也使用标准的同源策略

    为使 CSP 可用,你需要配置你的网络服务器返回 Content-Security-Policy HTTP 标头(有时你会看到 X-Content-Security-Policy 标头,但那是旧版本,并且你无须再如此指定它)。

    除此之外,<meta> 元素也可以被用来配置该策略,例如

    htmlCopy

    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self'; img-src https://*; child-src 'none';" />
    

    备注:某些功能(例如发送 CSP 违规报告)仅在使用 HTTP 标头时可用。

    威胁

    缓解跨站脚本攻击

    CSP 的主要目标是减少和报告 XSS 攻击。XSS 攻击利用了浏览器对于从服务器所获取的内容的信任。恶意脚本在受害者的浏览器中得以运行,因为浏览器信任其内容来源,即使有的时候这些脚本并非来自于它本该来的地方。

    CSP 通过指定有效域——即浏览器认可的可执行脚本的有效来源——使服务器管理者有能力减少或消除 XSS 攻击所依赖的载体。一个 CSP 兼容的浏览器将会仅执行从白名单域获取到的脚本文件,忽略所有的其他脚本(包括内联脚本和 HTML 的事件处理属性)。

    作为一种终极防护形式,始终不允许执行脚本的站点可以选择全面禁止脚本执行。

    缓解数据包嗅探攻击

    除限制可以加载内容的域,服务器还可指明哪种协议允许使用;比如(从理想化的安全角度来说),服务器可指定所有内容必须通过 HTTPS 加载。一个完整的数据安全传输策略不仅强制使用 HTTPS 进行数据传输,也为所有的 cookie 标记 secure 标识,并且提供自动的重定向使得 HTTP 页面导向 HTTPS 版本。网站也可以使用 Strict-Transport-Security HTTP 标头确保连接它的浏览器只使用加密通道。

    使用 CSP

    配置内容安全策略涉及到添加 Content-Security-Policy HTTP 标头到一个页面,并配置相应的值,以控制用户代理(浏览器等)可以为该页面获取哪些资源。比如一个可以上传文件和显示图片页面,应该允许图片来自任何地方,但限制表单的 action 属性只可以赋值为指定的端点。一个经过恰当设计的内容安全策略应该可以有效的保护页面免受跨站脚本攻击。本文阐述如何恰当的构造这样的标头,并提供了一些例子。

    https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Guides/CSP

    https://www.cnblogs.com/wuguanglin/p/XSS.html

    https://aszx87410.github.io/beyond-xss/ch2/xss-defense-csp

    https://csper.io/evaluator

    https://csp-evaluator.withgoogle.com

  • VPS 安裝 Arch Linux 教學

    VPS 安裝 Arch Linux 教學

    之前已完成的文件 , 又要重新寫一次 , 事因 VPS 供應商的問題 , 導致所有資料盡失

    開始了! 首先你要確定你的 VPS 最少有 2GB RAM 才能有足夠的 RAM DISK 作為安裝 Arch Linux 的 temp root file system。

    你的 VPS 安裝好 Debain 系統先 , 因為接下來用 Grml 軟件才能作為 arch linux 安裝的引導 , 詳情可瀏覽

    https://grml.org

    在 debian console shell 安裝 grml-rescueboot

    apt -y update
    apt -y install grml-rescueboot

    Download grml iso 鏡像檔

    # 可以使用如下命令自動下載最新的 grml iso, 但只能 download full 版本的 iso
    update-grml-rescueboot
    
    # 以我的經驗如果RAM 只有 2GB Ram, 最好用 small 版本的 iso 成功率高很多 , 可以瀏覽 https://download.grml.org
    cd /boot/grml
    wget https://mirror-hk.koddos.net/grml/grml-small-2025.12-amd64.iso

    請先自己記下當前 debain 的網絡訊息 , 用以下 command

    ip address   # 查看現時的 IP 及 netmask 
    ip route     # 查看現時的 default route ip address

    Edit 設定檔 , 這個設定檔是 grml 引導的設定包括暫時 ssh password 等信息.

    vi /etc/default/grml-rescueboot
    
    CUSTOM_BOOTOPTIONS="ssh=password lang=us keyboard=us tz=Asia/Hong_Kong ip=192.168.1.50::192.168.1.1:255.255.255.0:grmlhost:ens3:none dns=8.8.8.8,8.8.4.4 toram"
    
    #其中:
    #192.168.1.50 → 客戶端 IP
    #:: → 省略 NFS server IP(非必填)
    #192.168.1.1 → Gateway
    #255.255.255.0 → Netmask
    #grmlhost → 主機名稱
    #ens3 → 網卡名稱
    #none → 不使用自動設定(如 DHCP)
    
    #如果你用 DHCP 的設定
    CUSTOM_BOOTOPTIONS="ssh=password lang=us keyboard=us tz=Asia/Hong_Kong toram"

    如果你有其他設定需求可以參考以下網頁 grml-rescueboot

    更新 GRUB BOOT 配置

    #先新增 /usr/sbin/ 目錄入 PATH 裏面
    export PATH=$PATH:/usr/sbin/
    
    #之後執行
    update-grub

    更新 grub 記錄, 使下一次啟動時引導到 arch linux 安裝步驟

    # 使用 full iso 的使用如下命令
    grub-reboot "Grml Rescue System (grml-full_20xx.xx.iso)"
    # 使用 small iso 的使用如下命令
    grub-reboot "Grml Rescue System (grml-small_20xx.xx.iso)"

    reboot 系統進入 arch linux 安裝程序

    sync
    reboot

    用 VPS 提供的 VNC CONSOLE 可以看到 BOOT 機情況 , 如果上唔到網可以在這個畫面按下 “e” 就可以手動輸入之前你寫下的網絡訊息 !

    設定完成可以試一試 ping 看看有沒有回應

    Arch Linux 安裝

    以上步驟是在 grml live 引導內進步 , ram disk 有限, 下載完 arch linux base root system 後把 zip 檔 delete

    cd /tmp
    wget https://geo.mirror.pkgbuild.com/iso/latest/archlinux-bootstrap-x86_64.tar.zst
    
    # 解壓 ZST
    tar --use-compress-program=unzstd -xvf archlinux-bootstrap-x86_64.tar.zst
    
    # delete ZST file 增加 ram disk 空間
    rm archlinux-bootstrap-x86_64.tar.zst

    選擇你地區的 source list , 例如 : Hong Kong 當然你可以選擇其他地區, 但可能 update 時會比自己地區 server 慢

    vi /tmp/root.x86_64/etc/pacman.d/mirrorlist
    
    ## Hong Kong
    #Server = https://hk.mirrors.cicku.me/archlinux/$repo/os/$arch
    #Server = https://mirror-hk.koddos.net/archlinux/$repo/os/$arch
    #Server = https://hkg.mirror.rackspace.com/archlinux/$repo/os/$arch
    #Server = https://arch-mirror.wtako.net/$repo/os/$arch
    Server = https://mirror.xtom.com.hk/archlinux/$repo/os/$arch

    避免 chroot 後提示硬碟空間不足

    mount --bind root.x86_64 root.x86_64

    chroot 修改 arch linux root file system

    /tmp/root.x86_64/bin/arch-chroot /tmp/root.x86_64/

    以下操作是在 chroot arch linux root system 內進行

    #初始化 pacman 軟件包管理器 key
    
    pacman-key --init
    pacman-key --populate archlinux

    更新系统並安裝分區需要用到的工具

    pacman -Syyu                 # 更新軟件包管理器的軟件
    pacman -S dosfstools parted  # 安裝劃分分區軟件

    磁碟分區

    # 首先將硬盤轉換為 gpt 類型.
    lsblk                       # 顯示分區情况 找到你想安装的硬盤名
    parted /dev/vda             # run "parted", 進入交互式命令行,進行硬盤類型轉換 ( vda 是我的HD , 如果你的是 sda 或 hda )
    (parted)mktable             # 輸入 mktable
    New disk label type? gpt    # 輸入 gpt 將類型轉換為 gpt 如硬盤有數據警告, 輸入 yes 即可
    quit                        # 最後 quit 退出 parted 命令行交互
    
    cfdisk /dev/vda # 執行分區操作, 分配各個分區大小及類型
    # bios 引導需要分區 BIOS boot, Linux Swap, Linux filesystem
    # uefi 引導需要分區 EFI System, Linux Swap, Linux filesystem
    fdisk -l # 分區結束後, 檢查硬碟分區情况

    分成 1MB BIOS Boot , 3GB linux swap (大約是你的 RAM 1.5倍左右, 如果空間有限可以set細過 RAM 都可以 , 其餘全部做 root ( 當然你可以分開獨立 home folder 另一個分區 )

    如果用 BIOS boot , 用以下 command format ( 多數 VPS 都是用這個 )

    mkfs.ext4  /dev/vda3            # format root 
    mkswap /dev/vda2		# 初始化 swap 分區
    mount /dev/vda3 /mnt	        # mount root 分區到臨時系統
    swapon /dev/vda2		# enable swap 

    如果用 uefi boot , 用以下 command format

    mkfs.ext4  /dev/vda3            # format root 
    mkfs.vfat  /dev/vda1            # 格式化 uefi 分區, 僅 uefi 引導需要
    mkswap /dev/vda2		# 初始化 swap 分區
    mount /dev/vda3 /mnt	        # mount root 分區到臨時系統
    swapon /dev/vda2		# enable swap 
    mount --mkdir /dev/vda1 /mnt/efi   # mount uefi 分區

    安裝基本軟件包

    pacstrap /mnt base base-devel linux linux-headers linux-firmware dhcpcd iwd vim bash-completion grub openssh
    

    生成 fstab 配置文件

    genfstab -U /mnt >> /mnt/etc/fstab

    run exit 退出 chroot 環境

    重新 chroot 到剛剛做好的基本 root file system

    /tmp/root.x86_64/bin/arch-chroot /tmp/root.x86_64/mnt

    出現這訊息不用理會

    設定 time zone

    ln -sf /usr/share/zoneinfo/Asia/Hong_Kong /etc/localtime
    hwclock --systohc
    systemctl enable systemd-timesyncd.service

    配置語言環境, 選擇 en_HK.UTF-8 UTF-8 及 zh_HK.UTF-8 UTF-8 ( 如果你有需要可以選擇你喜歡的語言環境)

    vim /etc/locale.gen

    生成 locale

    locale-gen

    向 /etc/locale.conf 導入内容

    echo 'LANG=en_HK.UTF-8'  > /etc/locale.conf

    設定 host name

    # 加入你想為 server 命名, 例如: ArchLinux
    vim /etc/hostname
    
    # 同時在 /etc/hosts 設定與其匹配
    vim /etc/hosts
    
    # 加入如下内容
    127.0.0.1   localhost
    ::1         localhost
    127.0.1.1   ArchLinux

    設定網絡

    vim /etc/systemd/network/20-wired.network

    內容如下:

    [Match]
    Name=eth0
    
    [Network]
    Address=xx.xx.xx.xx/24 # ipv4 地址
    
    #如果沒有 ipv6 不用加以
    Address=xxx:xxx:xxx:xxx:xxx:xxx:xxx:xxx/80 # ipv6 地址 
    IPv6AcceptRA=no
    
    [Route]
    Gateway=x.xx.xx.1
    GatewayOnLink=yes
    
    # 如果沒有 ipv6 route 不用加以下內容
    [Route]   
    Gateway=xxx:xxx:xxx::1/80
    GatewayOnLink=ye

    如果網絡支持dhcp, systemd-networkd的 dhcp 配置可如下配置

    [Match]
    Name=eth0
    
    [Network]
    DHCP=yes

    設置 systemd-networkd 及 sshd 開機自動執行

    systemctl enable systemd-networkd
    systemctl enable sshd

    配置 dns

    vim /etc/resolv.conf
    
    nameserver 8.8.8.8
    nameserver 8.8.4.4

    setup root password

    passwd root

    安装 grub

    pacman -S grub
    # 對於 bios 引導的機器請使用下面的命令, 注意是安裝 grub 到硬碟而不是某一個分區
    
    # bios boot
    grub-install --target=i386-pc /dev/vda
    
    # uefi only #
    # 如果使用 uefi 引導, 則還需要安裝 efibootmgr
    pacman -S efibootmgr
    # 然後使用下面的命令安裝 grub
    grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB
    # end uefi only #
    
    # 編輯配置文件, 去掉 GRUB_CMDLINE_LINUX_DEFAULT 參數的 quit
    # 添加 net.ifnames=0(避免機器重啟後, 網卡接口名無法與 systemd-networkd 內配置的接口名相匹配, 從而導致機器失聯)
    # 添加 nowatchdog(提高開關機速度), 把 log level 的數值從 3 改成 5(出現系統錯誤方便排錯)
    vim /etc/default/grub
    # 生成 GRUB 所需的配置文件
    grub-mkconfig -o /boot/grub/grub.cfg

    退出 chroot 環境及reboot

    exit
    reboot
  • 2026 年 CloudCone 重大事故停機

    我就是用這間 VPS 供應商,把我的網站放在那裡,過了一個星期還未修復,我的網站 DATA 凍過水! 好彩我還有一份舊備份,花了很多時間重建好!

    今次把 Server 的軟件同時升級 ! 現在我的 Apache 可以支援 HTTP2

    CLOUDCONE 最新情況 , 需要重新 REBUILD 自己的 VPS , 所以之前的 VPS DATA 是沒有了 , 救唔番 ! >_< , 下次續約時會送多兩個月 , 還有一年份半價備份方案優惠可供選購 !

    有關今次的事態可以看看以下網址:

    2026 年 CloudCone 重大事故停机超一周了,还能用吗 – 知乎

    CloudCone 出现 Hypervisor 事故,还能用吗? – VPS之家

    突发!VPS服务商CloudCone遭遇“灭顶之灾”,全网数据遭删除?深扒Virtualizor背后的安全黑洞 – 知乎

  • 肯點解要租用 VPS 虛擬專用伺服器

    肯點解要租用 VPS 虛擬專用伺服器

    肯現在使用 SERVER 其實都是 VPS, 但這個 VPS 是自己買了一部二手電腦安裝了 proxmox 放在一個朋友租的 “數據中心” 櫃子內 .

    但經濟下行的情況下,有可能不會再,所以要想想把伺服器搬回家中。

    家中的寬頻 IP 會隨時轉變的,所以 “kenng.hk”的域名要如何對應 IP 的轉變。

    方法一 . 把 “域名” 放在有 “域名寄存” 的公司 , 把域名指向 Dynamic DNS ( 例子: 把 www.kenng.hk/wp/wp/wp/ CNAME 到 noip , duckdns , dyndns , ddns 等公司 , 再用這些公司來 update 家中 隨時會變的 IP 地址 )

    方法二. 自己建造一個能 dynamic update ip 的 DNS server

    老實講 “方法一” 應該是最平的做法 ^_^ 當然我選用了 方法二 , 不然這編文章都不會出現

    好了首先要找一間最平最平的 VPS 供應商了 , 我在網上找了一找 , 以下是我睇過的文章, 在這裏提供給大家參考一下

    https://www.lifetat.com/vps-hosting-分享和比較/ , https://www.10besty.com/best-vps-hosting-services/#three , https://progressbar.tw/posts/131

    我的目標要找最平又相對稳定的 VPS , 一般 1G RAM 的 VPS 大約要 5美元一個月 , 一年大約港幣 HKD480 左右吧 , 但我自己選了一個 15美元一年的 openvz server , 但這個 server ram 只有 128MB RAM , 如果只是運行 DNS server 就卓卓有餘了 .

    我用的是 RAMNODE 公司提供的服務 https://www.ramnode.com/

    買一個 128MB SVZ , 虛擬機有 1CPU , 12GB SSD , 1Gbps Port , 500GB Bandwidth , 1 IPv4 , 系統我就選用了 Ubuntu 20.04 , 選擇了 LA ( 洛杉磯 ) 本來想放在 NYC ( 紐約 ) 但沒有貨了

    付了錢就可以開始使用了, 本身部機已經裝好了基本的軟件, bind9 , apache , exim , sshd

    不知道是不是我用了 ubuntu os , 用不了 html 5 serial console

    我用 putty ssh 去新 server , 我個人建議有興趣買個 VPS 試東西的朋友 , 最好修改一下 ssh port 會好一點 , 在 internet 世界有著很多 爬蟲 不斷試你的電腦 22 port

    下一章會介紹下一設定 dynamic dns server 方法

  • 使用 Arduino NANO V3.0 顯示二進制時間

    使用 Arduino NANO V3.0 顯示二進制時間

    今次要玩的是 arduino nano V3.0 這個版子是什麼東西呢?

    Arduino Nano V3.0是一塊基與開放原始代碼的超小Simple i/o平台,與之前的USB版Arduino Diecimila相比較,Arduino Nano在體積上佔很大優勢,Arduino即可以用來開發出需要獨立運行,並且具有互動效果的電子用品;也可以用來開發出與電腦相連接,同 Flash,Processing,Max/Msp,PD,VVVV等軟件一同合作完成的互動作品。

    之前用過 orange pi GPIO+python 做過2進制的時鐘,網址如下:

    OrangePi 另類玩法–肯的學習空間

    今次就是用這版子做 2 進制時鐘 , 大家先要了解這版的引腳先

    再來了解 1088BS 引腳代表什麼 ,  但要留意的是那是基於引腳從左至右看 !  

    下列是 1088BS 官方引腳定義,是基如引腳是從下至上看的 ! 

    計時方面不同用 orangepi 讀取電腦時間,  Arduino Nano  要用計時模組 , 我今次用的是 DS1307 RTC 

    DS1307 這模組同 MCU 用 I2C 連接的, 想了解更多 I2C 協定可以參考以下網址 : 

    https://www.itread01.com/content/1545445450.html

    https://zh.wikipedia.org/zh-tw/I%C2%B2C

    Arduino Nano V3.0  的 I2C 引腳在 A4及A5 

    連接 1088BS 的引腳 , 第一列開始分別是  ( 2,3,4,5,6,A1,8)  pin  , 因為我們第7列不使用的, 所以隨便用A1腳, A1不用插東西

    第一行開始分別是 ( 13,13,13,9,13,10,11,12) ,   其中 1,2,3,5 行不使用的 , 所以隨便指派13腳代替, 都是不用插東西

    如果你們有另一想法想用全部」「 自己相應找其他引腳代替連接 A1 及 13 

    下載我編寫的 程式代碼  ( 當然有些是從別人的代碼借來用 , 例如 : 十進轉二進那一段)

    這張圖用 RIGHT CLICK , OPEN WITH NEW WINDOWS 睇會清楚一點

    更新一下我寫的程式先 , 上面我寫的那段程式碼有點缺陷, 就是不能手動UPDATE 時間, 要 DELETE CACHE 再編譯才能從電腦更新時間 , 所以 21年1月15日更新可以不用重新編譯, 手動更新時間程式碼 .

    下載程式碼

    下載了程式 , UPLOAD 上去 NANO , 以後要手動 UPDATE 時間, 就開 SERIAL MONITOR , 115200 baud , 按一下 ENTER 就會出現以下畫面 :

    之後按 “Y” 設定時間 , 按照指示就可以了 .

  • Ubuntu 20.04 LTS 上架 Win10 應用商店

    以下內容轉載自 IT 之家 URL : https://www.ithome.com/0/483/457.htm

    IT之家4月19日消息 Ubuntu 20.04 LTS on Windows 已经上架 Microsoft Store 应用商店,Ubuntu 20.04 on Windows允许使用Ubuntu Terminal并运行Ubuntu命令行实用程序,包括bash,ssh,git,apt等。请注意,Windows 10 S不支持运行此应用程序。

    Ubuntu 20.04 LTS on Windows需要Windows 10 版本 16215.0 或更高版本,支持ARM64、x64体系结构。

    据IT之家了解,要启动Ubuntu 20.04 on Windows,请在命令行提示符(cmd.exe)上使用“ ubuntu2004”,或在“开始”菜单中单击Ubuntu磁贴。

    要使用此功能,首先需要使用“打开或关闭Windows功能”并选择“Windows Subsystem for Linux”,单击“确定”,重新启动,然后使用此应用程序。

    也可以使用Administrator PowerShell提示符执行以上步骤:

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

    这个应用会在Windows Subsystem for Linux上安装Ubuntu 20.04 LTS版本。

    请注意,应用程序更新不会更改Ubuntu安装。要升级到新版本,请在Ubuntu Terminal终端中运行do-release-upgrade。

  • Linux 下配置 Let’s Encrypt 免費 SSL 証書方法 及 設定

    其實申請 Let’s Encrypt certificate 有很多方法,
    有很多 API 總有一款能配給到你系統的配置


    https://letsencrypt.org/zh-tw/docs/client-options/

    我自己就用 Linux bash shell :  acme.sh   &
    certbot  client 來申請 SSL 証書

    參考網站 :  

    烧饼博客

    Calos’s Blog

    DEVLOG of andyyou

  • 今次試的東西是 docker , 類似virtual machine, 又好像 沙盒

    今次我要試的是 DOCKER , 點解要試這個 ? 因為有公司舊的網頁要用 PHP 5.x 同時又要 PHP 7.x 雖然好多方法去實現同時運行多個 PHP VERSION , 但是一個好機會試吓新東西

    在文章最後有我參考過的網站文章連結

    大家先去了解什麼是 DOCKER
    參考網址 : https://philipzheng.gitbooks.io/docker_practice/content/

    好了大家了解都差不多了 , 那我們開始吧 !
    我用的是 ubuntu , 所以以下 command 都是 debian apt

    apt update
    apt upgrade
    apt install gnupg2
    apt install curl
    apt install software-properties-common
    curl -fsSL
    https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
    add-apt-repository “deb [arch=amd64]
    https://download.docker.com/linux/ubuntu $(cat /etc/os-release | grep
    UBUNTU_CODENAME | cut -d ‘=’ -f 2) stable”
    apt update
    apt install docker-ce

    好了安裝完, 立即試一試, hello-world docker 有什麼反應先

    root@docker-test:~#
    docker run hello-world
    Unable to find image ‘hello-world:latest’ locally
    latest: Pulling from library/hello-world
    1b930d010525: Pull complete
    Digest:
    sha256:fc6a51919cfeb2e6763f62b6d9e8815acbf7cd2e476ea353743570610737b752
    Status: Downloaded newer image for hello-world:latest

    Hello from Docker!
    This message shows that your installation appears to be working
    correctly.

    To generate this message, Docker took the following steps:
    1. The Docker client contacted the Docker daemon.
    2. The Docker daemon pulled the “hello-world” image from the
    Docker Hub.
    (amd64)
    3. The Docker daemon created a new container from that image
    which runs the
    executable that produces the output you
    are currently reading.
    4. The Docker daemon streamed that output to the Docker
    client, which sent it
    to your terminal.

    To try something more ambitious, you can run an Ubuntu container with:
    $ docker run -it ubuntu bash

    Share images, automate workflows, and more with a free Docker ID:
    https://hub.docker.com/

    For more examples and ideas, visit:
    https://docs.docker.com/get-started/

    root@docker-test:~#

    當你行 docker run hello-world
    它會自動上到 “倉庫” 找 “hello-world” 這個影像檔然後執行

    之後我們再試下 ubuntu shell 看看

    docker run -it ubuntu bash

    root@docker-test:~#
    docker run -it ubuntu bash
    Unable to find image ‘ubuntu:latest’ locally
    latest: Pulling from library/ubuntu
    423ae2b273f4: Pull complete
    de83a2304fa1: Pull complete
    f9a83bce3af0: Pull complete
    b6b53be908de: Pull complete
    Digest:
    sha256:04d48df82c938587820d7b6006f5071dbbffceb7ca01d2814f81857c631d44df
    Status: Downloaded newer image for ubuntu:latest
    root@7b55e3f631cd:/#

    這個 shell 是在 ubuntu image 內執行的 , “7b55e3f631cd” 就是 docker 生成的 ubuntu 容器內裏就是一個 ubuntu

    root@7b55e3f631cd:/#
    ls -la
    total 72
    drwxr-xr-x 1 root root 4096 Feb 24 07:43 .
    drwxr-xr-x 1 root root 4096 Feb 24 07:43 ..
    -rwxr-xr-x 1 root root 0 Feb
    24 07:43 .dockerenv
    drwxr-xr-x 2 root root 4096 Feb 19 01:17 bin
    drwxr-xr-x 2 root root 4096 Apr 24 2018 boot
    drwxr-xr-x 5 root root 360 Feb 24 07:43 dev
    drwxr-xr-x 1 root root 4096 Feb 24 07:43 etc
    drwxr-xr-x 2 root root 4096 Apr 24 2018 home
    drwxr-xr-x 8 root root 4096 May 23 2017 lib
    drwxr-xr-x 2 root root 4096 Feb 19 01:15 lib64
    drwxr-xr-x 2 root root 4096 Feb 19 01:14 media
    drwxr-xr-x 2 root root 4096 Feb 19 01:14 mnt
    drwxr-xr-x 2 root root 4096 Feb 19 01:14 opt
    dr-xr-xr-x 392 nobody nogroup 0 Feb 24
    07:43 proc
    drwx—— 2 root root 4096 Feb 19 01:17 root
    drwxr-xr-x 1 root root 4096 Feb 21 22:20 run
    drwxr-xr-x 1 root root 4096 Feb 21 22:20 sbin
    drwxr-xr-x 2 root root 4096 Feb 19 01:14 srv
    dr-xr-xr-x 13 nobody nogroup 0
    Feb 20 03:34 sys
    drwxrwxrwt 2 root root 4096 Feb 19 01:17 tmp
    drwxr-xr-x 1 root root 4096 Feb 19 01:14 usr
    drwxr-xr-x 1 root root 4096 Feb 19 01:17 var
    root@7b55e3f631cd:/#

    其實概念就 docker 生成一個可讀寫的容器 , 把 image 投射內其中, 你在這個容器做的東西不會影響 image 內的資料 ,
    image 是唯讀的 !

    每個 docker 生成的容器都是獨立 , 容器究竟是什麼 , 如果你知道有些掃毒程式會創造 “沙盒” 來測試那些行檔是否中毒 !

    沙盒的概述可以看看這些網址
    https://www.arthurtoday.com/2011/06/sandbox.html
    http://www.pcdiy.com.tw/detail/11605

    同樣 docker 生成的容器就算被入侵 , 基本上都不會影響主機 !

    我們試一試裝個 apche , 先看看有那些 image 可以用先

    用command “docker search apache” 找找有什麼 image 可用

    root@docker-test:~#
    docker search apache
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    httpd The Apache HTTP Server
    Project 2869 [OK]
    tomcat Apache Tomcat is an open source
    implementati… 2645 [OK]
    cassandra Apache Cassandra is an open-source
    distribut… 1092 [OK]
    maven Apache Maven is a software project
    managemen… 983 [OK]
    solr Solr is the popular, blazing-fast, open
    sour… 729 [OK]
    apache/nifi Unofficial convenience binaries and Docker
    i… 161 [OK]
    eboraas/apache-php PHP on Apache (with SSL/TLS support), built
    … 143 [OK]
    apache/zeppelin Apache
    Zeppelin 117 [OK]
    eboraas/apache Apache (with SSL/TLS support), built on
    Debi… 91 [OK]
    apache/airflow Apache
    Airflow 87
    groovy Apache Groovy is a multi-faceted language
    fo… 85 [OK]
    nimmis/apache-php5 This is docker images of Ubuntu 14.04 LTS
    wi… 61 [OK]
    apacheignite/ignite Apache Ignite In-Memory docker
    image. 60 [OK]
    bitnami/apache Bitnami Apache Docker
    Image 58 [OK]
    linuxserver/apache An Apache container, brought to you by
    Linux… 25
    apachepulsar/pulsar Apache Pulsar – Distributed pub/sub
    messagin… 22
    apache/nutch Apache
    Nutch 21 [OK]
    antage/apache2-php5 Docker image for running Apache 2.x with
    PHP… 16 [OK]
    webdevops/apache Apache
    container 14 [OK]
    newdeveloper/apache-php apache-php7.2 5
    lephare/apache Apache
    container 5 [OK]
    newdeveloper/apache-php-composer apache-php-composer 4
    secoresearch/apache-varnish Apache+PHP+Varnish5.0 2 [OK]
    oberonamsterdam/apache24-fpm Default Oberon apache setup using PHP-FPM,
    r… 1 [OK]
    jelastic/apachephp An image of the Apache PHP application
    serve… 0
    root@docker-test:~#

    有數十個 image 可用 , 有些包含 tomcat , 有些包含 php , 看看你想用那一個 !

    我們就用第一個 image “httpd” , 下載這個 image 執行 “docker pull httpd”

    root@docker-test:~#
    docker pull httpd
    Using default tag: latest
    latest: Pulling from library/httpd
    bc51dd8edc1b: Pull complete
    dca5bc65e18f: Pull complete
    ccac3445152a: Pull complete
    8515f2015fbc: Pull complete
    e35494488b8c: Pull complete
    Digest:
    sha256:b783a610e75380aa152dd855a18368ea2f3becb5129d0541e2ec8b662cbd8afb
    Status: Downloaded newer image for httpd:latest
    docker.io/library/httpd:latest
    root@docker-test:~#

    下載完成, 我們看看現在我們有多少個可用的 image 先 , 執行 ” docker images”

    root@docker-test:~#
    docker images


    REPOSITORY
    TAG IMAGE
    ID CREATED SIZE


    ubuntu
    latest 72300a873c2c 2 days
    ago 64.2MB


    httpd
    latest c562eeace183 3 weeks
    ago 165MB


    hello-world
    latest fce289e99eb9 13 months
    ago 1.84kB


    root@docker-test:~#

    我們已經有三個 image , 先前測試用的 “hello-world” & “ubuntu” , 還有剛下載的 “httpd”

    好了急不及待, 我們來創造一個 docker 環境行 apache 吧 !

    root@docker-test:~#
    docker run -d –name=apache2 httpd
    2d40f50299972e46392d10b9ecf8afbbe3189cace34aaadede7d1e372509b667

    已經生成了一個基於 httpd image 的容器 “–name” 給予這個容器一個別名

    當前有多少容器在運行 command : “dcoker ps -a”

    root@docker-test:~#
    docker ps -a
    CONTAINER
    ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2d40f5029997 httpd “httpd-foreground” 13 minutes
    ago Up 13
    minutes 80/tcp apache2
    7b55e3f631cd ubuntu “bash” 58 minutes ago Exited
    (127) 52 minutes
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” About an hour ago Exited (0)
    About an hour
    ago distracted_dubinsky

    你們可以看到 status 只有 apache2 在背環繼續執行 , 因為我創造apache容器時加了 “-d” daemon

    要停止那些容器 command : docker stop apache2

    root@docker-test:~#
    docker stop apache2
    apache2
    root@docker-test:~# docker ps -a
    CONTAINER
    ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2d40f5029997 httpd “httpd-foreground” 20 minutes
    ago Exited (0)
    5 seconds
    ago apache2
    7b55e3f631cd ubuntu “bash” About an hour ago Exited
    (127) About an hour
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” About an hour ago Exited (0)
    About an hour
    ago distracted_dubinsky

    你看到 status 已經停止了 , 可以用 “docker start apache2” 或 “docker restart
    apache2” 把容器重新運行

    root@docker-test:~#
    docker start apache2
    apache2
    root@docker-test:~# docker ps -a
    CONTAINER
    ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2d40f5029997 httpd “httpd-foreground” 23 minutes
    ago Up 2
    seconds 80/tcp apache2
    7b55e3f631cd ubuntu “bash” About an hour ago Exited
    (127) About an hour
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” About an hour ago Exited (0)
    About an hour
    ago distracted_dubinsky

    如果要完全移除這個容器,先停止這個容器再執行 command : docker rm apache2

    root@docker-test:~#
    docker rm apache2
    Error response from daemon:
    You cannot remove a running container
    2d40f50299972e46392d10b9ecf8afbbe3189cace34aaadede7d1e372509b667. Stop
    the container before attempting removal or force remove
    root@docker-test:~# docker stop apache2
    apache2
    root@docker-test:~# docker rm apache2
    apache2
    root@docker-test:~# docker ps -a
    CONTAINER
    ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    7b55e3f631cd ubuntu “bash” About an hour ago Exited
    (127) About an hour
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” About an hour ago Exited (0)
    About an hour
    ago distracted_dubinsky
    root@docker-test:~#

    看到沒有已經移除了 apache2 這容器了 !

    好了講了這麼多要點樣 config 這個容器內 apache config 呢?

    我們先入去這個容器看看 ! 這個容器是個 linux 所以要linux shell 入去 !

    參考網址 : https://www.runoob.com/docker/docker-exec-command.html

    command : docker exec -it apache2 bash
    就是在 apache2 容器行 bash shell

    root@docker-test:~#
    docker exec -it apache2 bash
    root@48c49e4b63a3:/usr/local/apache2#

    已經取得 apache 容器 bash shell 了

    root@48c49e4b63a3:/usr/local/apache2#
    ls -la
    total 56
    drwxr-xr-x 1 www-data www-data 4096 Feb 2 00:15 .
    drwxr-xr-x 1 root root 4096 Feb 2 00:12
    ..
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    bin
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    build
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    cgi-bin
    drwxr-xr-x 4 root root 4096 Feb 2 00:15
    conf
    drwxr-xr-x 3 root root 4096 Feb 2 00:15
    error
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    htdocs
    drwxr-xr-x 3 root root 4096 Feb 2 00:15
    icons
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    include
    drwxr-xr-x 1 root root 4096 Feb 24 08:55 logs
    drwxr-xr-x 2 root root 4096 Feb 2 00:15
    modules

    這個 image 裝了 apache location 是 /usr/local/apache2 , 我們要的 apache config 就在 conf folder 內 !

    直接修改這個 conf 是可以的, 但不要忘記如果容器移除了, 資料一同移除 . 那麼如何保存這些資料 ?

    你可以把當前修改過的容器 , SAVE 成新的 image 使用 , 但我們今次用的是 “資料卷”
    https://philipzheng.gitbooks.io/docker_practice/content/data_management/volume.html

    我們現在把容器內的 apache conf folder copy 到實體機某 folder 進行修改及以資料卷的方式掛入 apache2 容器內

    root@48c49e4b63a3:/usr/local/apache2/conf#
    ls -la
    total 120
    drwxr-xr-x 4 root root 4096
    Feb 2 00:15 .
    drwxr-xr-x 1 www-data www-data 4096 Feb 2 00:15 ..
    drwxr-xr-x 2 root root 4096
    Feb 2 00:15 extra
    -rw-r–r– 1 root root 20823 Feb 2
    00:15 httpd.conf
    -rw-r–r– 1 root root 13064 Feb 2
    00:15 magic
    -rw-r–r– 1 root root 60847 Feb 2
    00:15 mime.types
    drwxr-xr-x 3 root root 4096
    Feb 2 00:15 original
    root@48c49e4b63a3:/usr/local/apache2/conf# exit
    exit
    root@docker-test:~# docker cp apache2:/usr/local/apache2/conf
    /opt/docker-apache2-conf/
    root@docker-test:~# ls -l /opt/docker-apache2-conf/conf/
    extra/ httpd.conf magic mime.types original/
    root@docker-test:~# ls -l /opt/docker-apache2-conf/conf/

    把apache2容器內/usr/local/apache2/conf 抄到實體機 /opt/docker-apache2-conf/

    我們先把容器停了及移除重新創造一個用資料卷的apche2容器

    root@docker-test:/opt/docker-apache2-conf#
    docker stop apache2
    apache2
    root@docker-test:/opt/docker-apache2-conf# docker rm apache2
    apache2
    root@docker-test:/opt/docker-apache2-conf#
    docker run -d –name=apache2 -v
    /opt/docker-apache2-conf/conf:/usr/local/apache2/conf   -v
    /opt/docker-apache2-conf/html:/var/www httpd
    52af877f4a8fbd927f3a8fea9e3568e3d8ee317f03f2a6e22f82cc083a8c670f

    解釋一下
    -v /opt/docker-apache2-conf/conf:/usr/local/apache2/conf
    -v /opt/docker-apache2-conf/html:/var/www

    用 -v 就是資料卷的意思
    主機 folder “/opt/docker-apache2-conf/conf” 投射入容器
    “/usr/local/apache2/conf” folder
    主機 folder “/opt/docker-apache2-conf/html” 投射入容器 “/var/www” folder

    一切都OK 了 , 但問題來了 ? 如何把主機的 listing port 投射入容器

    我們就要用 -p 8081:80

    List containers

    root@docker-test:/opt/docker-apache2-conf# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    52af877f4a8f httpd “httpd-foreground” 8 minutes
    ago Up 8
    minutes 80/tcp apache2
    7b55e3f631cd ubuntu “bash” 2 hours
    ago Exited
    (127) 2 hours
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” 2 hours
    ago Exited (0)
    2 hours
    ago distracted_dubinsky
    root@docker-test:/opt/docker-apache2-conf# docker stop apache2
    apache2
    root@docker-test:/opt/docker-apache2-conf# docker rm apache2
    apache2
    root@docker-test:/opt/docker-apache2-conf#
    docker run -d –name=apache2 -p 8081:80 -v
    /opt/docker-apache2-conf/conf:/usr/local/apache2/conf   -v
    /opt/docker-apache2-conf/html:/var/www httpd
    0995c2a40a9be6643f67df8db6664ffae4fea7022261190f08d732d23715f0b7
    root@docker-test:/opt/docker-apache2-conf# docker ps -a
    CONTAINER
    ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    0995c2a40a9b httpd “httpd-foreground” 30 seconds
    ago Up 27
    seconds 0.0.0.0:8081->80/tcp apache2
    7b55e3f631cd ubuntu “bash” 2 hours
    ago Exited
    (127) 2 hours
    ago elastic_morse
    a8560ebcedb9 hello-world “/hello” 2 hours
    ago Exited (0)
    2 hours
    ago distracted_dubinsky
    root@docker-test:/opt/docker-apache2-conf#

    看到 status 主機 8081 port 投射到 容器的 80 port 了

    那就是外部連接主機的port 8081 就會 容器apache2 提供服務 http://xx.xx.xx.xx:8081/

    我參考過的網址

    https://blog.basec.cc/index.php/archives/65/
    https://github.com/acmesh-official/acme.sh/wiki/Run-acme.sh-in-docker
    https://www.jianshu.com/p/ea4a00c6c21c
    https://www.iszy.cc/2019/02/13/docker-acme-sh/
    https://segmentfault.com/a/1190000012063374
    https://blog.chengweichen.com/2015/05/docker-nginx-php-fpm-52-mysql.html
    https://my.oschina.net/u/3375733/blog/1591091
    https://beginor.github.io/2017/06/08/use-compose-instead-of-run.html
    https://zhuanlan.zhihu.com/p/45425683

  • OrangePi 另類玩法

    OrangePi 另類玩法

    OrangePi 另類玩法

    OrangePi 除了可以用來當 Linux Server , Media Player , 懷舊遊戲機之外 ….. !

    還可以用 python / C 寫一些程式用來控制 OrangePi 的 GPIO , 類似單片機 MCU

    今次我分享自己用 python 寫的小程式 , 用 GPIO 來控制 8X8 LED 點陣光亮 ,把當前的時間, 星期用二進制方式顯示出來

    例如 : 時間 16:35:42 我們用八組 2 進制來顯示

    小時 16 , 分開十位及個位 即是1 &6 十位 1 二進是 0001個位 6 二進是0110
    分鐘 35 , 分開十位及個位 即是3 &5 十位 1 二進是 0011個位 5 二進是0101
    秒 49 , 分開十位及個位 即是 4& 9 十位 1二進是 0100個位 9 二進是 1001
    星期 3 , 個位即是 3 個位3 二進是 0011

    我用4 行代表二進制的4個位 , 用 7 列分別代表 [時(2列) 分(2列) 秒(2列) 星期(1列)]

    大家可以看看以下的圖片, H2 及 H3 CPU GPIO 分佈圖,
    我替供的程式碼是以 H3 GPIO 寫, 如果用 H2 CPU 的朋友要自己再更改 PORT 對應了

    再來大家了解一下 8X8 LED 點陣 , 我用的是 1088BS 共陽點陣

    大家可能會發現上圖和下圖描述的 “行” 及 “列” 有所不同 ? 上圖是我使用麵包條所以1088BS 插腳是由左至右方式插 , 下圖插腳是由下至上方式插 , 整塊1088BS 轉了90度 , “行” “列” 會互換了 , 如果你們是左右就參考上圖 , 上下的就參考下圖了 .

    上圖是 “行””R” 為接地 , “列””C” 為正極 , 最好在正極接上一個1k電阻保護LED , 那麼下圖就是 “行””R” 為正極了 , “列””C” 接地

    現在 OrangePi 要安裝 GPIO 資料庫 , 我用這個 pyH3 Python Library
    https://www.instructables.com/id/Orange-Pi-One-Python-GPIO-basic/

    還有很多 Library 用的 , 其他參考 !
    https://pypi.org/project/OrangePi.GPIO/

    當然引用時有不同的呼叫方法 !

    以下是我寫的程序 , 不是寫得好好, 應該可以用 CALL FUNCTION 的方式去處理十個及個位轉二進制的 !
    以後再修改一下
    下載程式碼

    稍為改良用了 call function 的程式碼

    下載程式碼

    如果把 FILE 改為可執行檔

    chmod +x bin2clock.py

    ./bin2clock.py

    應該會發現如下圖 : 顯示時間是 14點15分43秒 星期五

    顯示時間是 14點16分00秒 星期五

    你們看見有7個1K電阻, 因為正極要拉一個電阻才能保護 LED 燈, 你們看見我寫了
    1248 那一列是我弄燒了那列 LED 所以星期用了第八列做 , 同樣第四行都是燒了 所以跳過了

    其餘那4個沒有電阻用釘書釘連接是陰極所以不用電阻 !

  • OrangePi 當懷舊遊戲機

    OrangePi 當懷舊遊戲機

    先下載 OS 影像檔 URL http://www.retrorangepi.org/
    找你自己所買的 OrangePi 型號

    之後要下載寫入軟件
    用來把 OS IMAGE 寫入 SD CARD 內 Balena
    Etcher
    Win32diskmanger

    完成後把 SD CARD 插入 ORANGEPI 內開機
    記得要插 NETWORK 因為要從 WINDOWS 電腦把 GAME ROM COPY 入去 RETRORANGPI 內
    在WINDOWS NETWORK 找找有沒有 RETRORANGE 或者知道 IP 就在檔案總管 \\192.X.X.X