標籤: ubuntu

  • 製作 Ubuntu 遠端桌面伺服器

    有時我們會需要安裝一個雲端的伺服器是有 GUI 介面可以操作的,舉例像是用模擬器爬爬蟲,或是需要遠端協作需求的人,會需要在雲端伺服器上安裝一個桌面伺服器。

    先提一下會有缺點:

    1. 通常雲端 VPS 廠商不會有顯卡,所以遠端桌面勢必顏色和顯示上不會太好。
    2. 雲端廠商的規格和價格成正比,勢必無法和自己使用電腦上比較。
    3. 即便使用 GUI 介面,他畢竟是一台伺服器,你能連上表示其他人也能連,常常很容易忽略了伺服器需要設定的安全機制。

    這邊使用 Linode Ubuntu 22.04 安裝,安裝目標:

    1. Gnome desktop 環境
    2. XRDP 遠端桌面連線

    捨棄 Linode 文件中描述的 VNC 連線,是因為怎麼裝都連不起來…曾經懷疑是不是 Gnome 安裝錯了,後來發現根本是 VNC 設定有問題,果斷使用 XRDP 來處理。

    步驟:

    1. 首先參照 Linode 文件 確認購買一個伺服器,並且設定好伺服器時區\hostname\更新等等。
    2. 如果不使用 tasksel 安裝,則使用以下指令:
      sudo apt install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal
    3. 如果使用 tasksel 安裝,請參考這裡 
      sudo apt install tasksel
      sudo tasksel install ubuntu-desktop
    4. 安裝 xrdp 功能,文件中使用的 OS 是 ubuntu 18.04 不過用 22.04 也行的:
      sudo apt-get install xrdp -y
    5. 如果使用 ufw 套件(可用 ufw status 或是 iptabes -h 指令檢查):
      sudo ufw allow 3389/tcp
    6. 如果使用 iptables 套件:
          1. 編輯設定檔案:
            nano /etc/v4
          2. 輸入內容並儲存(注意要開啟 3389 port):
            *filter
            
            
            # Allow all loopback (lo0) traffic and reject traffic
            # to localhost that does not originate from lo0.
            -A INPUT -i lo -j ACCEPT
            -A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT
            
            
            # Allow ping.
            -A INPUT -p icmp -m state --state NEW --icmp-type 8 -j ACCEPT
            
            
            # Allow SSH connections.
            -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
            
            
            # Allow HTTP and HTTPS connections from anywhere
            # (the normal ports for web servers).
            #-A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
            #-A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
            -A INPUT -p tcp --dport 3389 -m state --state NEW -j ACCEPT
            
            # Allow inbound traffic from established connections.
            # This includes ICMP error returns.
            -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
            
            
            # Log what was incoming but denied (optional but useful).
            -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7
            
            
            # Reject all other inbound.
            -A INPUT -j REJECT
            
            
            # Log any traffic that was sent to you
            # for forwarding (optional but useful).
            -A FORWARD -m limit --limit 5/min -j LOG --log-prefix "iptables_FORWARD_denied: " --log-level 7
            
            
            # Reject all traffic forwarding.
            -A FORWARD -j REJECT
            
            
            COMMIT
          3. 編輯設定檔案:
            nano /etc/v6
          4. 輸入內容並儲存:
            *filter
            
            
            # Allow all loopback (lo0) traffic and reject traffic
            # to localhost that does not originate from lo0.
            -A INPUT -i lo -j ACCEPT
            -A INPUT ! -i lo -s ::1/128 -j REJECT
            
            
            # Allow ICMP
            -A INPUT -p icmpv6 -j ACCEPT
            
            
            # Allow HTTP and HTTPS connections from anywhere
            # (the normal ports for web servers).
            #-A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
            #-A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT
            
            
            # Allow inbound traffic from established connections.
            -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
            
            
            # Log what was incoming but denied (optional but useful).
            -A INPUT -m limit --limit 5/min -j LOG --log-prefix "ip6tables_INPUT_denied: " --log-level 7
            
            
            # Reject all other inbound.
            -A INPUT -j REJECT
            
            
            # Log any traffic that was sent to you
            # for forwarding (optional but useful).
            -A FORWARD -m limit --limit 5/min -j LOG --log-prefix "ip6tables_FORWARD_denied: " --log-level 7
            
            
            # Reject all traffic forwarding.
            -A FORWARD -j REJECT
            
            
            COMMIT
        1. 執行指令:
          iptables-restore < /etc/v4;/sbin/iptables-save;ip6tables-restore < /etc/v6;/sbin/ip6tables-save;
    7. 設定 polkit 添加授權規則,編輯檔案:
      sudo nano /etc/polkit-1/localauthority.conf.d/02-allow-colord.conf
    8. 輸入以下內容後儲存,這主要功能是色彩管理避免需要多次輸入密碼:
      polkit.addRule(function(action, subject) { if ((action.id == "org.freedesktop.color-manager.create-device" || action.id == "org.freedesktop.color-manager.create-profile" || action.id == "org.freedesktop.color-manager.delete-device" || action.id == "org.freedesktop.color-manager.delete-profile" || action.id == "org.freedesktop.color-manager.modify-device" || action.id == "org.freedesktop.color-manager.modify-profile") && subject.isInGroup("{group}")) { return polkit.Result.YES; } });
    9. 重啟 XRDP:
      sudo /etc/init.d/xrdp restart

    連線時 windows OS 用戶使用內建的遠端桌面連線工具即可, MacOS 用戶推薦使用 Microsoft Remote Desktop 這套軟體。

    其他額外你可能需要的東西

    1. 安裝 Chrome: https://linuxize.com/post/how-to-install-google-chrome-web-browser-on-ubuntu-20-04/
    2. 安裝 Telegram:https://linuxhint.com/install-telegram-desktop-messenger-linux/

     

    參考文件:

    1. https://www.linode.com/docs/guides/set-up-and-secure/
    2. https://www.linode.com/docs/guides/install-vnc-on-ubuntu-20-04/
    3. https://operavps.com/linux-vps-with-gui-and-rdp/
    4. https://documentation.suse.com/zh-cn/sles/15-SP2/html/SLES-all/cha-security-policykit.html
  • Linux SSH 斷線後保持 session 的工具 – Screen

    紀錄一下之前用很久的一個 linux 套件,主要用途是作爲讓 Server 在再斷線後,依然可以跑池長時間非背景運作。

    安裝方法,權限問題請自行加上 sudo:

    # ubuntu \ debian
    apt-get install screen
    
    # centos
    yum install screen

    一些基本的操作:

    # 建立一個新的 screen 
    screen -U -S SCREEN_NAME
    
    # 進入已開啟的 screen
    screen -drU SCREEN_NAME
    
    # 列出所有 screen
    screen -ls

    至於進入 screen 內的操作,請參考:

    大概可以記住:所有指令都是 ctrl + a 開始。

    • c 是關閉一個建立一個新視窗。另外要離開當前視窗,就和一般 ssh 一樣輸入 exit 就行了,會殺掉當前視窗的 session。
    • d 是離開當前的 screen (keep session)
    • A 大寫的 a 可以重新命名當前的視窗,這很好用

    以前同事有分享一個 screenrc 設定檔案,不錯用。請自行取代 /etc/screenrc 檔案:

    startup_message off
    defencoding utf8
    encoding utf8 utf8
    #caption always "%{= wk} %{= KY} [%n]%t @ %H %{-} %= %{= KR} %l %{-} | %{= KG} %Y-%m-%d %{-} "
    #hardstatus alwayslastline " %-Lw%{= Bw}%n%f %t%{-}%+Lw %=| %0c:%s "
    #bindkey ^[z prev
    #bindkey ^[x next
    termcapinfo xterm*|rxvt* 'ti@:te@'
    termcapinfo xterm ti@:te@
    vbell off
    # C + left : prev
    # C + right : next
    bindkey "^[[1;5C" next
    bindkey "^[O5C" next
    bindkey "^[[C" next
    bindkey "^[[1;5D" prev
    bindkey "^[O5D" prev
    bindkey "^[[D" prev
    
    
    # C-a b : encoding big5
    # C-a u : encoding utf8
    bind b encoding big5 utf8
    bind u encoding utf8 utf8
    
    
    # C-b $num : move current window to number $num
    bind -c move 0 number 0
    bind -c move 1 number 1
    bind -c move 2 number 2
    bind -c move 3 number 3
    bind -c move 4 number 4
    bind -c move 5 number 5
    bind -c move 6 number 6
    bind -c move 7 number 7
    bind -c move 8 number 8
    bind -c move 9 number 9
    bindkey "^b" command -c move
    
    
    # F12 : fast kill
    #bindkey "^[[24~" kill
    
    
    #termcapinfo xterm 'hs:ts=\E]0;:fs=\007:ds=\E]0;bash\007'
    #caption always "%{= bk} %{= wk} %-Lw%{by}%n+%f %t%{wk}%{wk}%+Lw %=%{kw}%{= R}%{-}"
    
    
    shelltitle '$ |csh'
    
    
    defhstatus "\005t"
    #hardstatus on
    #caption always "%{= wk} %{= KY} [%n]%t @ %H %{-} %= %{= KR} %l %{-} | %{= KG} %Y-%m-%d %{-} "
    #hardstatus alwayslastline " %-Lw%{= Bw}%n%f %t%{-}%+Lw %=| %0c:%s "
    
    
    #buttom status bar
    caption always "%{= .G} %-w%<%{=ub .y}[%n %t]%{= .G}%+w "
    hardstatus alwaysignore
    #hardstatus alwayslastline "%{= .K} [%l]%<%=%{= .W}@%H %=%{= .y} %Y/%m/%d%{= .m} %C %A"
    
    
    #shelltitle '$|csh'
    #caption always "%{bw}%M/%d %c %{wb} %-w%{c}%n %t%{w}%+w%{k}"
    
    
    shell -$SHELL