Google Cloud 建立與設定 VM 執行個體 for CentOS 7

使用 Google Cloud Platform 雲端服務的 Compute Engine 建立 CentOS 7 的 VM 執行個體,並說明 VM 如何預約靜態外部 IP、如何使用 PieTTY 與 VM 建立 SSH 連結、如何使用 WinSCP 與 VM 建立 SCP 來安全的傳輸檔案,以及透過第三方供應商 SendGrid 的電子郵件服務來讓 Postfix 寄發電子郵件。
GCP (Google Cloud Platform,Google 雲端平台) 是一項使用 Google 核心基礎架構、資料分析和機器學習技術的雲端運算服務。
啟用免費試用
搜尋並點擊 Google Cloud。

點擊「免費試用」。

選擇國家和勾選服務條款。

填寫個人資料。

填寫付款方式與信用卡資料。

啟用免費試用完成,Google Cloud 首頁。

VM 執行個體
Google Cloud 的 Compute Engine,提供可彈性擴充高效能的 VM (Virtual Machine,虛擬機器)。
啟用 VM
點擊「VM 執行個體」。

必須「啟用計費功能」,才能使用 Compute Engine 來建立 VM。

Compute Engine 啟用中。

建立 VM
建立 VM。

VM 配置,映像檔選擇要安裝的作業系統 CeneOS 7。
(720 小時 / 30 天 = 24 小時,就是都免費)

建立 VM 完成。

預約靜態外部 IP
每當建立一個新的 VM 執行個體時,都會自動配發一個臨時外部 IP,但當您重啟 VM 時,這個臨時外部 IP 就會被回收,因此我們必須透過「預約靜態外部 IP」來保留一個固定 IP。
進入 VM 詳細資料。

編輯 VM。

編輯 VM 網路介面。

編輯 VM 網路介面外部 IP。

建立 VM 的外部 IP 位址。

VM 預約新的靜態外部 IP 位址。

VM 預約新的靜態外部 IP 位址完成。

編輯 VM 完成儲存。

VM 預約新的靜態外部 IP 設定完成。

SSH 連結 VM
使用 SSH (Secure Shell,安全外殼協定) 連結 VM 最常用的兩種方式。
在瀏覽器視窗開啟
Google Cloud 預設提供「在瀏覽器視窗中開啟」與 VM 的 SSH 連結。

連結 VM 中...

已連結至 VM。

PieTTY
在 Windows 可以使用 PieTTY 或 PuTTY 軟體,連結 Google Cloud VM 執行個體。
至 PieTTY project 下載並執行軟體後,點擊「PuTTY 模式」。


輸入 Google Cloud VM 的外部 IP。

設定在新建立 SSH 金鑰製作好儲存的 Private Key,並開啟。


成功連結 VM,輸入使用者名稱 (就是在新建立 SSH 金鑰時自訂的)。

儲存設定,這樣下次就不用再重新設定一次。


使用 root 帳戶
執行這個指令即可切換至 root 帳戶 (無須密碼):
sudo su
使用 WinSCP 傳輸檔案
Windows 可透過 WinSCP 軟體使用 SCP (Secure Copy,安全複製),來與 Google Cloud VM 傳輸檔案。
至 WinSCP 下載安裝並執行軟體後,點擊「Advanced」進入進階設定。

設定在新建立 SSH 金鑰製作好儲存的 Private Key。

- File protocol:SCP。
- Host name:輸入 Google Cloud VM 的外部 IP。
- User name:使用者名稱 (就是在新建立 SSH 金鑰時自訂的)。
點擊「Login」登入。



成功連結 Google Cloud VM。

一般帳戶登入切換至 root
修改 SCP Shell 設定為 sudo su -
,在連結後會自動切換自 root 帳戶。

SSH 金鑰
新建立
於 Windows 建立新的 SSH 金鑰,必須透過第三方工具 PuTTY Key Generator。至 PuTTY 依 OS (Operating System,作業系統)位元下載 puttygen.exe。
執行 puttygen.exe,點擊 Generate 來產生 public/private key。


- 產生 public/private key 完成,可自訂「Key comment」(也就是到時登入系統的使用者名稱),這裡我設定 jacky 這個使用者 (修改同時可看到上方的「Public key ...」也會跟著修改)。
- 點擊「Save public key」和「Save private key」,儲存產生的 public/private key (PieTTY 連結 VM 和 WinSCP 與 VM 傳輸檔案要使用)。
- 複製上方的 SSH「Public key ...」(下面的加入 VM 要使用)。


加入 VM
將 puttygen.exe 產生的 SSH「Public key ...」加入 VM。
進入 VM 詳細資料。

編輯 VM 的 SSH 金鑰。

貼上 puttygen.exe 產生的 SSH「Public key ...」並儲存。

SSH 金鑰加入 VM 完成。

CentOS 7 設置
語系
查看目前系統語系。(Google Cloud VM 安裝的 CentOS 7 語系預設為 en_US)
locale
LANG=en_US.UTF-8 LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_NAME="en_US.UTF-8" LC_ADDRESS="en_US.UTF-8" LC_TELEPHONE="en_US.UTF-8" LC_MEASUREMENT="en_US.UTF-8" LC_IDENTIFICATION="en_US.UTF-8" LC_ALL=
將語系設定為中文台灣。
localectl set-locale LANG=zh_TW.utf8
重新登入後再查看目前系統語系,已設為中文台灣。
locale
LANG=zh_TW.utf8 LC_CTYPE="zh_TW.utf8" LC_NUMERIC="zh_TW.utf8" LC_TIME="zh_TW.utf8" LC_COLLATE="zh_TW.utf8" LC_MONETARY="zh_TW.utf8" LC_MESSAGES="zh_TW.utf8" LC_PAPER="zh_TW.utf8" LC_NAME="zh_TW.utf8" LC_ADDRESS="zh_TW.utf8" LC_TELEPHONE="zh_TW.utf8" LC_MEASUREMENT="zh_TW.utf8" LC_IDENTIFICATION="zh_TW.utf8" LC_ALL=
時區
查看目前系統時區。(Google Cloud VM 安裝的 CentOS 時區預設為 UTC)
timedatectl
Local time: 六 2019-04-27 13:11:53 UTC
Universal time: 六 2019-04-27 13:11:53 UTC
RTC time: 六 2019-04-27 13:11:53
Time zone: UTC (UTC, +0000)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
將時區設定為亞洲/台北。
timedatectl set-timezone Asia/Taipei
重新登入後再查看目前系統時區,已設為亞洲/台北。
timedatectl
Local time: 六 2019-04-27 21:19:19 CST
Universal time: 六 2019-04-27 13:19:19 UTC
RTC time: 六 2019-04-27 13:19:19
Time zone: Asia/Taipei (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
使用 SendGrid 傳送電子郵件
雖然 Google Cloud VM 安裝的 CentOS 7 預設就包含 Postfix 電子郵件伺服器 (Mail Server),但 Google Cloud 的 Compute Engine 不允許 SMTP 通訊埠 25 與 SMTPS 通訊埠 465、587 傳出連線,也就是 SMTP 與 SMTPS 的通訊埠會遭到封鎖,因為這些通訊埠容易被大量濫用。
雖然可以自訂 Postfix 的其它通訊埠,但建議使用擁有信任的第三方供應商 (如 SendGrid),可減輕 Google Cloud 的 Compute Engine 與我們維持良好 IP 記錄的壓力。
訂閱 SendGrid
前往 Google Cloud Platform Marketplace 或至 Marketplace 來訂閱 SendGrid Email。


選擇 SendGrid Email 的 Free 方案。

前往註冊「SendGrid」。

填寫註冊資訊

允許 SendGrid 存取 Google 帳戶。

註冊完成。

Google Cloud SendGrid Email 訂閱完成。

申請 SendGrid API Key
點擊「在 SendGrid 網站上管理 API 金鑰」來申請 API Key。

認證 Email (預設為剛才允許存取 Google 的帳戶 Email)。

認證 Email 確認信。

產生 API Key。

選擇 API Key 的權限 (自己使用的,選取 Full Access 即可)。

產生的 API Key。

Postfix
安裝相關套件。
yum install postfix cyrus-sasl-plain cyrus-sasl-md5 -y
修改 Postfix 設定檔,在結尾處加入這些設定。
vim /etc/postfix/main.cf
# ... 以上省略 ... # 新增 SendGrid SMTP 服務與使用的通訊埠 (Compute Engine 不允許使用通訊埠 25 和 587,因此請務必使用通訊埠 2525) relayhost = [smtp.sendgrid.net]:2525 # 強制執行安全資料傳輸層 (SSL)/傳輸層安全標準 (TLS) 支援,並設定 STMP 驗證 smtp_tls_security_level = encrypt smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd header_size_limit = 4096000 smtp_sasl_security_options = noanonymous
使用 SendGrid 帳戶產生的 API Key 來產生 SASL 密碼對應 (將紅字改為您的 API Key 即可)。
vim /etc/postfix/sasl_passwd
[smtp.sendgrid.net]:2525 apikey:SG.xxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
產生 .db 檔。
postmap /etc/postfix/sasl_passwd
為確保安全性,刪除內含 API Key 的檔案 (該檔案已產生 .db 檔,因此也不需要了)。
rm /etc/postfix/sasl_passwd
設定 .db 檔案權限,並確認其它檔案都已刪除。
chmod 600 /etc/postfix/sasl_passwd.db
ls -al /etc/postfix/sasl_passwd*
-rw-------. 1 root root 12288 5月 2 23:24 /etc/postfix/sasl_passwd.db
重新載入 Postfix 設定:
postfix reload
SELinux 設定
SELinux (Security Enhanced Linux,安全增强式 Linux),預設不允許透過網頁伺服器傳送電子郵件:
getsebool -a | grep httpd_can_sendmail
httpd_can_sendmail --> off
設定 SELinux 允許網頁伺服器傳送電子郵件:
setsebool -P httpd_can_sendmail=1
確認設定:
getsebool -a | grep httpd_can_sendmail
httpd_can_sendmail --> on
寄發 Email 測試
安裝寄發 Email 套件。
yum install mailx -y
寄發 Email。
echo 'message' | mail -s subject a.b@gmail.com
查看系統 Email 記錄檔,在 status=sent
顯示成功的回應 250 ok
,代表已寄發 Email 成功。
tail -n 5 /var/log/maillog
May 2 23:52:41 centos7-lemp postfix/smtp[3890]: 304161012FE4: to=<a.b@gmail.com>, relay=smtp.sendgrid.net[167.89.123.58]:2525, delay=13141, delays=13140/0.04/0.36/0.06, dsn=2.0.0, status=sent (250 Ok: queued as eBBmgQliRvi0OTtWBfYm2Q) May 2 23:52:41 centos7-lemp postfix/qmgr[3432]: 304161012FE4: removed May 3 00:02:41 centos7-lemp postfix/qmgr[3432]: 49F701012FE5: from=<root@centos7-lemp.localdomain>, size=466, nrcpt=1 (queue active) May 3 00:02:41 centos7-lemp postfix/smtp[4094]: 49F701012FE5: to=<a.b@gmail.com>, relay=smtp.sendgrid.net[169.47.148.162]:2525, delay=13230, delays=13230/0.05/0.26/0.08, dsn=2.0.0, status=sent (250 Ok: queued as iaLpsQy0T7iWxSGWHPdc8g) May 3 00:02:41 centos7-lemp postfix/qmgr[3432]: 49F701012FE5: removed

本著作係採用創用 CC 姓名標示-相同方式分享 3.0 台灣 授權條款授權.