2018年7月24日 星期二

Ubuntu Apache 上用自簽憑證啟用 https 服務

原文 https://ccnrz.wordpress.com/2017/05/04/%E5%9C%A8-ubuntu-apache-%E4%B8%8A%E5%95%9F%E7%94%A8-https-%E8%87%AA%E7%B0%BD%E6%86%91%E8%AD%89/


記錄在 Ubuntu 14.04 ,透過建立自簽憑證的方式, 啟用 https 服務的過程。

0.安裝 Apache

(原文 http://webcache.googleusercontent.com/search?q=cache:yOHRpH4DzUEJ:chyuan3c.pixnet.net/blog/post/167336994-%25E5%2595%259F%25E7%2594%25A8ubuntu%25E7%259A%2584https+&cd=1&hl=zh-TW&ct=clnk&gl=tw)

1
sudo apt-get install apache2

1.啟用 Apache SSL 模組

1
2
sudo a2enmod ssl
sudo service apache2 restart

2.產生自簽憑證

選定好要放憑證的目錄,在這邊我新建目錄 /etc/apache2/ssl
利用 openssl 產生自簽憑證
1
2
sudo mkdir /etc/apache2/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
如果沒有安裝 openssl 的話,輸入下列命令以安裝
1
sudo apt-get install -y openssl
之後會提示輸入憑證的必要資訊
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 國碼
Country Name (2 letter code) [AU]:US
// 國名
State or Province Name (full name) [Some-State]:New York
//地名
Locality Name (eg, city) []:New York City
// 組織單位名稱
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Your Company
// 部門名稱
Organizational Unit Name (eg, section) []:Department of Kittens
// 憑證的名稱(FQDN)
Common Name (e.g. server FQDN or YOUR name) []:your_domain.com
// 聯絡信箱
Email Address []:your_email@domain.com

3.修改 Apache SSL site 設定

編輯 /etc/apache2/sites-available/default-ssl.conf 檔案
1
sudo vim /etc/apache2/sites-available/default-ssl.conf
指派 SSLCertificateFile 與 SSLCertificateKeyFile 路徑
1
2
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key

4.啟用 Apache SSL Site

1
2
sudo a2ensite default-ssl.conf
sudo service apache2 restart

 5.測試

開啟瀏覽器,測試 https 是否有正常運作

server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none

Source : https://stackoverflow.com/questions/21181231/server-certificate-verification-failed-cafile-etc-ssl-certs-ca-certificates-c#

TLDR:
hostname=XXX
port=443
trust_cert_file_location=`curl-config --ca`

sudo bash -c "echo -n | openssl s_client -showcerts -connect $hostname:$port \
    2>/dev/null  | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'  \
    >> $trust_cert_file_location"
Long answer
The basic reason is that your computer doesn't trust the certificate authority that signed the certificate used on the Gitlab server. This doesn't mean the certificate is suspicious, but it could be self-signed or signed by an institution/company that isn't in the list of your OS's list of CAs. What you have to do to circumvent the problem on your computer is telling it to trust that certificate - if you don't have any reason to be suspicious about it.
You need to check the web certificate used for your gitLab server, and add it to your 
/bin/curl-ca-bundle.crt.
To check if at least the clone works without checking said certificate, you can set:
export GIT_SSL_NO_VERIFY=1
#or
git config --global http.sslverify false
But that would be for testing only, as illustrated in "SSL works with browser, wget, and curl, but fails with git", or in this blog post.
Check your GitLab settings, a in issue 4272.

To get that certificate (that you would need to add to your curl-ca-bundle.crt file), type a:
echo -n | openssl s_client -showcerts -connect yourserver.com:YourHttpGilabPort \
  2>/dev/null  | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
(with 'yourserver.com' being your GitLab server name)
To check the CA (Certificate Authority issuer), type a:
echo -n | openssl s_client -showcerts -connect yourserver.com:YourHttpGilabPort \
  2>/dev/null  | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' \
  | openssl x509 -noout -text | grep "CA Issuers" | head -1

to identify the location of curl-ca-bundle.crt, you could use the command
curl-config --ca

Also, see my more recent answer "github: server certificate verification failed": you might have to renistall those certificates:
sudo apt-get install --reinstall ca-certificates
sudo mkdir /usr/local/share/ca-certificates/cacert.org
sudo wget -P /usr/local/share/ca-certificates/cacert.org http://www.cacert.org/certs/root.crt http://www.cacert.org/certs/class3.crt
sudo update-ca-certificates
git config --global http.sslCAinfo /etc/ssl/certs/ca-certificates.crt

Handbook - Ubuntu FireWall (ufw)

原文: https://webcache.googleusercontent.com/search?q=cache:dWjisuVtSoQJ:https://www.phpini.com/linux/ubuntu-firewall-ufw+&cd=1&hl=zh-TW&ct=clnk&gl=tw

ufw (Uncomplicated Firewall) 是 Ubuntu 內建的防火牆配置工具, ufw 使用上十分簡單, 以下是使用 ufw 的簡單例子:
檢查防火牆目前狀態:
$ sudo ufw status verbose
開啟 TCP 埠號 22 對外連線:
$ sudo ufw allow 22/tcp
開啟 TCP 埠號 80 及 443 對外連線, 可以直接用 http 及 https 表示:
$ sudo ufw allow http
$ sudo ufw allow https
或者直接輸入埠號也可以:
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
開啟 TCP 及 UDF 埠號 53 對外連線:
$ sudo ufw allow 53
只准許特定 IP (例如 192.168.0.10)連接埠號 22:
$ sudo ufw allow from 192.168.0.10 to any port 22
允許 192.168.0.0/24 網段存取 Samba 服務
$ ufw allow from 192.168.1.0/24 to any app Samba
列出服務使用埠號詳情:
$ sudo ufw app list

Howto install Google Chrome in Linux ?

Source: https://askubuntu.com/questions/510056/how-to-install-google-chrome

google-chrome-stable is available on a 3rd Party Repository: Google Chrome (for Stable).
Follow the instruction for installation:
  1. Add Key:
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
  2. Set repository:
    echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
  3. Install package:
    sudo apt-get update 
    sudo apt-get install google-chrome-stable

USB Battery Charge Specification - BC 1.2

原文: http://www.cash.idv.tw/wordpress/?p=8334

目前 USB 的充電規範以 USB應用者論壇(USB-IF) 所制定 BC (Battery Charge Spec.) 1.2 為主, 它定義了每個類型充電器的可用電流上限, 以及充電器的類型.
原先大家的認知, 都是 USB 要支援 500mA 的電流. 但是實際上, 這種電流可能連外接硬碟都推不動. 於是大家可能都 "偷偷" 可以供到 1A. 從 BC 1.1 開始, 規範中就直接讓 USB 可以輸出 1.5 A 了.
在充電類型方面, 一共有三種定義:標準下行埠(Standard Downstream Port, SDP)、充電下行埠(Charging Downstream Port, CDP)及專用充電埠(Dedicated Charging Port, DCP). 
所謂的 SDP 就是標準的 USB HUB 輸出, 像是電腦或是螢幕基座上的 USB 輸出大抵都支援 SDP. 它的充電電流是 500 mA, 所以不致於充得太快! 甚至在被充電裝置完全沒電時, 可以先用 100m A 的小電流輸出, 等到充至 0.5~0.7 V 時才改回 500 mA.
這有什麼好處呢?如果沒有人在管電流限額, 而沒電的手機電壓已經趨近於 0 了, 當輸入阻抗很小, 那麼電流將會很大, 使得溫度過高, 說不定會燒壞什麼電路. 阻抗給很大, 那麼電流就會太小, 導致充得很慢. 因此, 聰明的控制電流是有必要的.
至於 CDP 可以提供 500mA 和 1.5A 兩種電流, 算是有 "兩下子" 的 USB 接口. 一般會先用 500mA, 若是符合 1.5A 的快充, 就會切換過去 [3, 4].
最後的 DCP 只能充電, 不能傳 data.
上面提到的都是 USB 2.0, 而一般 USB 3.0 都是用 USB 2.0 的規範充電. 特別強調 USB 3.0 充電的話, 指得是 BC 1.2, 特別是 DCP 快充 [5].
USB 可持裝置 (portable device, 簡稱 PD) 要怎麼知道連上的是哪一種 USB Port, 並從而決定要抽多少電呢?判斷方式可以參考 ref  3 或 4. 簡單地說:
PD 先把 D+ 設為 0.6V:
1. D- 還在低電位, 表示 USB HOST 沒反應, 那麼這 HOST 是 SDP.
2. 若 D- 變成 0.6V, HOST 可能是 DCP 或 CDP.
PD 再把 D+ 或 D- (只有慢速設備會拉 D-) 拉高到高電位, 然後去看另外一根 D- 或 D+:
1. D- (D+) 還在低電位, 表示這是 CDP.
2. D-  (D+) 也變成高電位, 表示 D+ 和 D- 短路, 一定是 DCP.
因此 PD 有辦法知道它所連接的 port 是哪一種, 再進一步決定抽 0.5A 還是 1.5A.
最後, 一個裝置可能有時支援 CDP, DCP, 或 SDP , 這叫做 multi-role port. 若是不只是能支援充電, 還可以當 OTG 用, 這個叫做 ACA (Accessary Charge Adapter).
PD 只要有一個 micro-ACA, 它的 3 個 port 就同時扮演 device 當別人的 USB Storage (OTG Port), 當 host 外接鍵盤滑鼠 (Accessary Port) , 又被充電 (Charger Port). 一孔多用是未來的趨勢, 因此 ACA 早在 BC 1.1 的時候就被列入規範了.
[REF]