0%

如何在Windows中正確地使用“真正的”Linux

本文簡單介紹我的環境setup,隨緣更新。

0x00 初衷

出於種種原因,需要在Win10中的terminal中使用一個“完整版”的Linux。衆所周知,WSL并無過多Linux内核特性的實現,而WSL2還沒有被巨硬推送。(三綫師弟的易升升級已經完美翻車)於是有了本文。

本文采用的方案則是:Windows Terminal + Terminal settings + wsl + VMware VM + VMware自啓脚本,實現開機自啓no gui的ubuntu 18.04,其後運行terminal的時候無感知對該vm發起ssh。

0x01 設置VMware虛擬機

  • 我下載的是Desktop版,安裝時選擇Mini版本,時區選擇HongKong,最後skip。

(原以爲選中國時區,可以裝完,沒想到還是有147個包,所以還是skip吧。

  • 配置橋接網絡靜態IP 192.168.99.96。

0x02 設置WSL

其實這一步不是必須,但是經過測試,powershell或者cmd在結束連接后,會在下一次連接時出現令人費解的[?1;0c符號,因强迫症而改爲用wsl.exe進行跳板連接。

1
2
Win10安装Ubuntu子系统教程(附安装图形化界面) - Windows10.Pro
https://windows10.pro/bash-on-ubuntu-on-windows/

之後在wsl中生成wsl環公密鑰並傳送給VMware.

1
2
# ssh-keygen -t rsa -b 2048 -C "m4d3bug@wsl"
# ssh-copy-id m4d3bug@192.168.99.96

0x03 設置Windows Terminal

  • 下載,安裝。下面是它的配置文件。(ctrl + ,)

  • 由於Terminal默認啓動powershell,需要修改default的guid,使其自動連接。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    // This file was initially generated by Windows Terminal 1.0.1401.0
    // It should still be usable in newer versions, but newer versions might have additional
    // settings, help text, or changes that you will not see unless you clear this file
    // and let us generate a new one for you.

    // To view the default settings, hold "alt" while clicking on the "Settings" button.
    // For documentation on these settings, see: https://aka.ms/terminal-documentation
    {
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,

    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,

    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    // and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles":
    {
    "defaults":
    {
    // Put settings here that you want to apply to all profiles.
    },
    "list":
    [
    {
    "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
    "hidden": false,
    "name": "Ubuntu-18.04",
    "source": "Windows.Terminal.Wsl",
    "commandline": "wsl.exe ssh -X m4d3bug@192.168.99.96"
    },
    {
    "guid": "{a8202b0e-781a-5dab-98e2-e9d469a63619}",
    "hidden": false,
    "name": "CentOS8",
    "source": "Windows.Terminal.Wsl",
    "commandline": "wsl.exe ssh -X m4d3bug@192.168.99.93"
    },
    {
    // Make changes here to the powershell.exe profile.
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "name": "WSL",
    //"commandline": "powershell.exe wsl.exe ssh -X m4d3bug@192.168.99.96",
    "commandline": "powershell.exe wsl.exe",
    "hidden": false
    },
    {
    // Make changes here to the cmd.exe profi le.
    "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
    "name": "CMD",
    "commandline": "cmd.exe",
    "hidden": false
    },
    {
    "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
    "hidden": false,
    "name": "Azure Cloud Shell",
    "source": "Windows.Terminal.Azure"
    },

    ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [],

    // Add custom keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
    "keybindings":
    [
    // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
    // These two lines additionally bind them to Ctrl+C and Ctrl+V.
    // To learn more about selection, visit https://aka.ms/terminal-selection
    { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
    { "command": "paste", "keys": "ctrl+v" },

    // Press Ctrl+Shift+F to open the search box
    { "command": "find", "keys": "ctrl+shift+f" },

    // Press Alt+Shift+D to open a new pane.
    // - "split": "auto" makes this pane open in the direction that provides the most surface area.
    // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
    // To learn more about panes, visit https://aka.ms/terminal-panes
    { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
    }

0x04 VMware VM開機自啓

  • 創建脚本,注意替換自己的路徑以及雙引號

    1
    "D:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe" start "G:\Virtual Machines\ubuntu18.madebug.net\ubuntu18.madebug.net.vmx" nogui
  • 之後保存在以下路徑:

    1
    C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

0x05 個人Ubuntu的使用設置

  • 關閉sudo的密碼驗證

    1
    2
    3
    4
    5
    6
    7
    8
    $ sudo visudo 
    26 %sudo ALL=(ALL:ALL) NOPASSWD:ALL
    $ sudo hostnamectl set-hostname ubuntu18.madebug.net
    $ sudo vim /etc/hosts
    127.0.0.1 localhost
    127.0.0.1 ubuntu18
    127.0.0.1 ubuntu18.madebug.net
    ...
  • 安裝fish

    1
    $ sudo apt install fish
  • 安裝omf

    1
    2
    $ curl -x http://192.168.99.99:1081 -L https://get.oh-my.fish | fish
    $ chsh -s /usr/bin/fish
  • 為m4d3bug的fish shell和root的bash shell設置proxy

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    $ cat /etc/profile.d/bash_proxy.sh                                                         #!/bin/bash
    function proxy_off() {
    unset HTTP_PROXY
    unset HTTPS_PROXY
    unset FTP_PROXY
    unset SOCKS5_PORXY
    unset http_proxy
    unset https_proxy
    unset ftp_proxy
    unset socks5_proxy
    echo -e "已关闭代理"
    }

    function proxy_on() {
    export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
    export MY_PROXY_URL="http://192.168.99.99:1081"
    export HTTP_PROXY=$MY_PROXY_URL
    export HTTPS_PROXY=$MY_PROXY_URL
    export FTP_PROXY=$MY_PROXY_URL
    export SOCKS5_PORXY=$MY_PROXY_URL
    export http_proxy=$MY_PROXY_URL
    export https_proxy=$MY_PROXY_URL
    export ftp_proxy=$MY_PROXY_URL
    export socks5_proxy=$MY_PROXY_URL
    echo -e "已开启代理"
    }
    $ cat /etc/fish/conf.d/proxy.fish #!/bin/bash
    function proxy_off
    set -e HTTP_PROXY
    set -e HTTPS_PROXY
    set -e FTP_PROXY
    set -e SOCKS5_PORXY
    set -e http_proxy
    set -e https_proxy
    set -e ftp_proxy
    set -e socks5_proxy
    echo -e "已关闭代理"
    end

    function proxy_on
    export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
    export MY_PROXY_URL="http://192.168.99.99:1081"
    export HTTP_PROXY=$MY_PROXY_URL
    export HTTPS_PROXY=$MY_PROXY_URL
    export FTP_PROXY=$MY_PROXY_URL
    export SOCKS5_PORXY=$MY_PROXY_URL
    export http_proxy=$MY_PROXY_URL
    export https_proxy=$MY_PROXY_URL
    export ftp_proxy=$MY_PROXY_URL
    export socks5_proxy=$MY_PROXY_URL
    echo -e "已开启代理"
    end
    $ npm config set proxy http://192.168.99.99:1081
    $ npm config set https-proxy http://192.168.99.99:1081
    $ git config --global http.proxy http://192.168.99.99:1081
    $ git config --global https.proxy http://192.168.99.99:1081
  • 設置bash prompt,root用紅色提示

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    Ubuntu
    # cat >> .profile << EOF
    function prompt_color_on (){
    if [ $(id -u) -eq 0 ]; then
    # export PROMPT='%F{10}%n@%m%f:%F{12}%~%f%# '
    export PROMPT="%F{135}%n%f@%F{166}%m%f %F{118}%~%f %# "
    export PS1='\[\e[01;31m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]\# '
    else
    # export PROMPT='%F{10}%n@%m%f:%F{12}%~%f\$ '
    export PROMPT="%F{135}%n%f@%F{166}%m%f %F{118}%~%f %$ "
    export PS1='\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[00m\]\$ '
    fi
    }

    if [ -n "$BASH_VERSION" ]; then
    prompt_color_on
    else
    prompt_color_on
    fi
    EOF

    CentOS
    # cat >> /etc/profile.d/prompt.sh << EOF
    #!/bin/bash
    function prompt_color_on (){
    if [ $(id -u) -eq 0 ]; then
    # export PROMPT='%F{10}%n@%m%f:%F{12}%~%f%# '
    # export PROMPT="%F{135}%n%f@%F{166}%m%f %F{118}%~%f \# "
    export PS1='[\[\e[01;31m\]\u@\h\[\e[01;31m\] \[\e[01;31m\]\w\[\e[00m\]]# '
    else
    # export PROMPT='%F{10}%n@%m%f:%F{12}%~%f\$ '
    # export PROMPT="%F{135}%n%f@%F{166}%m%f %F{118}%~%f %$ "
    export PS1='[\[\e[01;32m\]\u@\h\[\e[00m\] \[\e[01;34m\]\w\[\e[00m\]\$ '
    fi
    }

    if [ -n "$BASH_VERSION" ]; then
    prompt_color_on
    else
    prompt_color_on
    fi
    EOF
  • 設置byobu

    1
    2
    3
    4
    $ apt install byobu -y
    $ byobu-select-backend
    1 tmux
    $ byobu-enable
  • 设置虚拟机共享宿主机目录,能share自然無須copy。

https://img.madebug.net/m4d3bug/images-of-website/master/blog/sharewithVM.png

0x06 設置x11轉發

xorg - Setting up X11 forwarding over SSH on Windows 10 Subsystem for Linux? - Super User

0x07 結語

實在受不了WSL這半成品了,WSL2就讓它先飛一會吧。

0x08 鳴謝