Windows子系统WSL 2 部署与应用

WSL 2将支持GPU,还能运行GUI应用!

1. 安装WSL 2

1.1 Windows系统版本需要

WSL 2仅适用于Windows 10版本18917或更高版本。

Windows版本验证,命令常规中用ver检测:

C:\Users\kylin>ver

Microsoft Windows [版本10.0.18362.778] 

1.2 Windows更新与Windows预览体验计划开启

具体相关操作可以参考https://docs.microsoft.com/zh-cn/windows/wsl/wsl2-install下的相关视频

Important
Currently to update to Windows 10, version 2004 (Build 19041), you will need to join the Windows Insider program and select the “Release Preview” ring. The public release should arrive by late May.

在这里插入图片描述
Windows系统更新完毕后检测系统版本:

Microsoft Windows [版本 10.0.19624.1000]
(c) 2020 Microsoft Corporation. 保留所有权利。

C:\Users\kylin>ver

Microsoft Windows [版本 10.0.19624.1000] 

或者选择Windows徽标键+ R来检查Windows版本,键入winver,然后选择“ 确定”。
Windows系统版本符合WSL 2安装使用,继续往下…

1.3启用“虚拟机平台”可选组件并确保启用了WSL

如果使用过WSL 1,则系统已经确保启用了WSL,WSL使用需要开启开发人员模式;

  • 没有使用过WSL 1的话,需要确保同时安装了”适用于 Linux 的 Windows 子系统”和”虚拟机平台”可选组件。 可以通过在 PowerShell 中运行以下命令来执行以下操作:
    	Windows PowerShell
    	版权所有 (C) Microsoft Corporation。保留所有权利。
    	
    	尝试新的跨平台 PowerShell https://aka.ms/pscore6
    	
    	PS C:\WINDOWS\system32> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    	
    	部署映像服务和管理工具
    	版本: 10.0.18362.1
    	
    	映像版本: 10.0.18362.778
    	
    	启用一个或多个功能
    	[==========================100.0%==========================]
    	操作成功完成。
    	PS C:\WINDOWS\system32> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    	
    	部署映像服务和管理工具
    	版本: 10.0.18362.1
    	
    	映像版本: 10.0.18362.778
    	
    	启用一个或多个功能
    	[=========================== 100.0%==========================] 
    	操作成功完成。
    	PS C:\WINDOWS\system32>
    
  • 启用或关闭Windows功能中选择“适用于Linux的Windows操作系统”和“虚拟机平台”可选组件:
    在这里插入图片描述
    组件选择之后,需要重启计算机来完成两个组件的安装。

1.4下载安装适用于Linux的Windows操作系统

我这里选择Ubuntu,在Windows应用商店中搜索“ Linux”,选择Ubuntu发布相关版本进行下载安装:
在这里插入图片描述
下载安装完成之后初次启动需要初始化发行版

安装,这可能需要几分钟的时间..。
请创建一个默认的UNIX用户帐户。用户名不需要与您的Windows用户名匹配。
有关更多信息,请访问:https : //aka.ms/wslusers 
输入新的UNIX用户名:Ubuntu 
adduser:请输入与通过NAME_REGEX [ _SYSTEM ]配置变量配置的正则表达式匹配的用户名。使用` --force-badname `
选项放松这个检查或重新配置NAME_REGEX。
输入新的UNIX用户名:ubuntu 
新密码:
重新输入新密码:
passwd:密码更新成功
安装成功!
要以管理员身份(用户“ root” )运行命令,请使用“ sudo <command>” 。见“人sudo_root” 的详细信息。欢迎使用Ubuntu 20.04 LTS ( GNU / Linux 4.4.0-19624-Microsoft x86_64 ) *文档:https: //help.ubuntu.com *管理:https: //landscape.canonical.com *支持:https:// ubuntu .COM /优势  系统信息,周二5月12日11点44分32秒CST 2020   系统负载:0.52用户登录:0   用法/家:未知的IPv4地址为
ETH4:192.168.189.1 
  内存使用:44%的IPv4地址为 eth5:192.168.234.1 
  交换用法:0%IPv4地址用于 wifi0:192.168.0.8 
  流程:7次0更新可以立即进行安装。这些更新中有0个是安全更新。可用更新的列表是更超过一个星期了。要检查是否有新更新,请运行:sudo apt update 该消息每天显示一次。要禁用它,请创建/home/ubuntu/.hushlogin文件。
ubuntu @ kylin:〜$ 

上述被翻译出问题!!!
此用户帐户用于启动发行版本时的登录的非管理员用户。

更新和升级分发版的包
大多数分发版随附了一个空的/精简的包目录。 我们强烈建议定期更新包目录,并使用分发版的首选包管理器升级已安装的包。 在 Debian/Ubuntu 上使用 apt:

ubuntu@kylin:$ sudo apt update && sudo apt upgrade

Windows不会自动更新或升级Linux发行版:Linux用户经常意外自行控制此任务。

ubuntu@kylin:〜$ wslfetch
                          ./+o+-      Windows 10 Linux Subsystem
                  yyyyy. 'yyyyyy+     root@kylin
              .;//+/h yyyyyyo     BUILD:    19624
           .++ .:/++++++/-.`sss/`     BRANCH:   rs_prerelease
         .:++o: `\++++++++/:---:/-    RELEASE:  Ubuntu 20.04 LTS
        o:+o+:++. `````'-/ooo+++++\   KERNEL:   Linux 4.19.104-microsoft-standard
       .:+o:+o/.          `+sssooo+\  UPTIME:   0d 0h 2m
  .++/+ +oo+o:`             \sssooo;
 /+++//+: oo+o
 \+/+o+++ o++o               ydddhh+
  .++.o+ +oo+:`             /dddhhh;
       .+.o+oo:.           oddhhhh+
        \+.++o+o` -,,,,.:ohdhhhhh+
         `:o+++  ohhhhhhhhyo++os:
           .o: .syhhhhhhh'.oo++o.
               /osyyyyyyy.oooo+++\
                   `````+oo+++o:/
                          `oo++'`

root@kylin:~# lsb_release -a | lolcat
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal
ubuntu @ kylin:/ $ sudo apt install lolcat 

在这里插入图片描述

1.5 使用命令行设置要由WSL 2支持的发行版

Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

PS C:\Users\kylin> wsl -help
命令行选项无效: -help
版权所有 (c) Microsoft Corporation. 保留所有权利。

使用情况: wsl.exe [Argument] [Options...] [CommandLine]

用于运行 Linux 二进制文件的参数:

    如果未提供命令行,wsl.exe 将启动默认 shell。

    --exec、-e <命令行>
        执行指定的命令而不使用默认的 Linux shell。

    --
        按原样传递剩余的命令行。

选项:
    --distribution、-d <发行版本>
        运行指定的分发。

    --user、-u <UserName>
        以指定用户身份运行。

用于管理适用于 Linux 的 Windows 子系统的参数:

    --export <发行版本> <文件名>
        将分发导出到 tar 文件。
        对于标准输出,文件名可以是 -。

    --import <发行版> <安装位置> <文件名> [Options]
        将指定的 tar 文件作为新分发进行导入。
        对于标准输入,文件名可以是 -。

        选项:
            --version <版本>
                指定用于新分发的版本。

    --list、-l [Options]
        列出分发。

        选项:
            --all
                列出所有分发,包括当前正在
                安装或卸载的分发。

            --running
                只列出当前正在运行的分发。

            --quiet、-q
                只显示分发名称。

            --verbose、-v
                显示有关所有分发的详细信息。

    --set-default、-s <发行版本>
        将分发设置为默认值。

    --set-default-version <版本>
        更改新分发的默认安装版本。

    --set-version <发行版本> <版本>
        更改指定分发的版本。

    --shutdown
        立即终止所有正在运行的分发和 WSL 2 轻型工具虚拟机。

    --terminate, -t <发行版本>
        终止指定的分发。

    --unregister <发行版本>
        注销分发并删除启动文件系统。

    --help
        显示使用情况信息。

Windows PowerShell
版权所有(C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell,了解新功能和改进!https://aka.ms/PSWindows

PS C:\Users\kylin> wsl -l --all
适用于 Linux 的 Windows 子系统分发版:
Legacy (默认)
Ubuntu-20.04
PS C:\Users\kylin> wsl -l -v
  NAME            STATE           VERSION
* Legacy          Stopped         1
  Ubuntu-20.04    Running         1
PS C:\Users\kylin> wsl --set-version Ubuntu-20.04 2
正在进行转换,这可能需要几分钟时间...
有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
转换完成。
PS C:\Users\kylin> wsl -l -v
  NAME            STATE           VERSION
* Legacy          Stopped         1
  Ubuntu-20.04    Stopped         2
PS C:\Users\kylin> wsl --set-version Legacy 2
正在进行转换,这可能需要几分钟时间...
有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
旧分发不支持 WSL 2。
PS C:\Users\kylin> wsl -l
适用于 Linux 的 Windows 子系统分发版:
Legacy (默认)
Ubuntu-20.04
PS C:\Users\kylin> wsl -s Ubuntu-20.04
PS C:\Users\kylin> wsl -l
适用于 Linux 的 Windows 子系统分发版:
Ubuntu-20.04 (默认)
Legacy

Legacy (默认)是WSL初期发行时的Bash on Ubuntu on Windows,该发行版不支持WSL 2。

1.6 下载安装Windows Terminal

下载安装Windows Terminal方便同时使用PowerShell、Ubuntu、Bash on Ubuntu on Windows等CLI。

在应用商店中搜索“Terminal”,进行下载安装。

给Windows Terminal设置像cmd一样的快捷启动方式:

  • 打开“编辑系统环境变量”;
  • 新建用户变量:变量名 wt ,值 C:\Users\kylin(用户名)\AppData\Local\Microsoft\WindowsApps\wt.exe

在这里插入图片描述
保存成功之后就可以在运行中输入wt打开Windows Terminal。
在这里插入图片描述

2. WSL 2 Linux系统设置

2.1 WSL 修改默认登录用户为root

#WSL修改默认登录用户为根本,管理员用户打开CMD运行以下命令:
Microsoft Windows [版本 10.0.19624.1000]
(c) 2020 Microsoft Corporation. 保留所有权利。

C:\WINDOWS\system32> C:\Users\kylin(用户名)\AppData\Local\Microsoft\WindowsApps\ubuntu2004.exe config --default-user root
C:\WINDOWS\system32> net stop lxssmanager 
LxssManagerManager服务正在停止。
LxssManager服务已启动成功停止。
C:\WINDOWS\system32>

再次打开Ubuntu-20.04就是root用户。

2.2 root用户设置密码

ubuntu@kylin:~$ sudo passwd root
[sudo] password for ubuntu:
New password:
Retype new password:
passwd: password updated successfully
ubuntu@kylin:~$ su root
Password:
root@kylin:/home/ubuntu#

2.3 Ubuntu- 20.04更新阿里源

root@kylin:/# cp /etc/apt/sources.list /etc/apt/sourses.list.bak
root@kylin:/# vi /etc/apt/sources.
#删除文件中内容并添加以下内容:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
#更新升级
root@kylin:$ sudo apt update && sudo apt upgrade

2.4 Ubuntu 检测ssh与ufw服务

root@kylin:/# service ssh status
 * sshd is not running
root@kylin:/# service ssh start
 * Starting OpenBSD Secure Shell server sshd                                                          
 sshd: no hostkeys available -- exiting.
root@kylin:/# sshd -T
sshd: no hostkeys available -- exiting.
root@kylin:/# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
root@kylin:/# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

root@kylin:/etc/ssh# sshd -T
port 22
addressfamily any
listenaddress [::]:22
listenaddress 0.0.0.0:22
usepam yes
logingracetime 120
x11displayoffset 10
maxauthtries 6
maxsessions 10
clientaliveinterval 0
clientalivecountmax 3
streamlocalbindmask 0177
permitrootlogin without-password
ignorerhosts yes
ignoreuserknownhosts no
hostbasedauthentication no
hostbasedusesnamefrompacketonly no
pubkeyauthentication yes
kerberosauthentication no
kerberosorlocalpasswd yes
kerberosticketcleanup yes
gssapiauthentication no
gssapicleanupcredentials yes
gssapikeyexchange no
gssapistrictacceptorcheck yes
gssapistorecredentialsonrekey no
gssapikexalgorithms gss-gex-sha1-,gss-group14-sha1-
passwordauthentication no
kbdinteractiveauthentication no
challengeresponseauthentication no
printmotd no
printlastlog yes
x11forwarding yes
x11uselocalhost yes
permittty yes
permituserrc yes
strictmodes yes
tcpkeepalive yes
permitemptypasswords no
compression yes
gatewayports no
usedns no
allowtcpforwarding yes
allowagentforwarding yes
disableforwarding no
allowstreamlocalforwarding yes
streamlocalbindunlink no
fingerprinthash SHA256
exposeauthinfo no
pidfile /run/sshd.pid
xauthlocation /usr/bin/xauth
ciphers chacha20-poly1305@openssh.com,aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm@openssh.com,aes256-gcm@openssh.com
macs umac-64-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-64@openssh.com,umac-128@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-sha1
banner none
forcecommand none
chrootdirectory none
trustedusercakeys none
revokedkeys none
securitykeyprovider internal
authorizedprincipalsfile none
versionaddendum none
authorizedkeyscommand none
authorizedkeyscommanduser none
authorizedprincipalscommand none
authorizedprincipalscommanduser none
hostkeyagent none
kexalgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256
casignaturealgorithms ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256
hostbasedacceptedkeytypes ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
hostkeyalgorithms ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
pubkeyacceptedkeytypes ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,ssh-ed25519-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,sk-ssh-ed25519@openssh.com,rsa-sha2-512,rsa-sha2-256,ssh-rsa
rdomain none
loglevel INFO
syslogfacility AUTH
authorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2
hostkey /etc/ssh/ssh_host_rsa_key
hostkey /etc/ssh/ssh_host_ecdsa_key
hostkey /etc/ssh/ssh_host_ed25519_key
acceptenv LANG
acceptenv LC_*
authenticationmethods any
subsystem sftp /usr/lib/openssh/sftp-server
maxstartups 10:30:100
permittunnel no
ipqos lowdelay throughput
rekeylimit 0 0
permitopen any
permitlisten any
permituserenvironment no
pubkeyauthoptions none

root@kylin:/etc/ssh# service ssh start
 * Starting OpenBSD Secure Shell server sshd                                           [ OK ]
 
#Ubuntu的防火墙状态检测,防火墙可能限制SSH端口22
root@kylin:~# service ufw status
 * Firewall is not running...                                                          [fail]
root@kylin:~#

2.5 ssh本地免密登陆

root@kylin:/# ssh localhost
root@localhost: Permission denied (publickey).
root@kylin:/# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:9VAHvCaC5qpmdHnyQ67jo2uhRwChWHKYuLUDVFIDAD8 root@kylin
The key`s randomart image is:
+---[RSA 3072]----+
|XB*+       .o..  |
|O=o .      ...   |
|++E.   .  o  .   |
|o o.  o ...oo    |
| . . +  S. o.    |
|  + + +          |
| + o B           |
|. = + +          |
| =+=o+ .         |
+----[SHA256]-----+
root@kylin:/# cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
root@kylin:/# chmod 0600 ~/.ssh/authorized_keys
root@kylin:/# ssh localhost
Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.19.104-microsoft-standard x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed May 13 12:35:01 CST 2020

  System load:  0.0                Processes:             22
  Usage of /:   0.5% of 250.98GB   Users logged in:       0
  Memory usage: 1%                 IPv4 address for eth0: 172.28.150.27
  Swap usage:   0%


0 updates can be installed immediately.
0 of these updates are security updates.



The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@kylin:~# exit
logout
Connection to localhost closed.

2.6 Ubuntu 远程ssh失败问题解决

# 其他主机 ssh 失败,scp 远程拷贝文件失败

root@kylin:~# ssh root@172.28.150.27
root@172.28.150.27: Permission denied (publickey).

#远程主机修改 /etc/ssh/sshd_config 
PasswordAuthentication no 改为 PasswordAuthentication yes

root@kylin:~# ssh root@172.28.150.27
root@172.28.150.27's password:
Permission denied, please try again.
root@172.28.150.27's password:
Permission denied, please try again.
root@172.28.150.27's password:
root@172.28.150.27: Permission denied (publickey,password).'

#远程主机修改 /etc/ssh/sshd_config 
PermitRootLogin prohibit-password 改为 PermitRootLogin yes 

root@kylin:~# service ssh restart
 * Restarting OpenBSD Secure Shell server sshd                                                                                                        [ OK ]

2.7 Ubuntu安装jdk

root@kylin:~# mkdir scpfile
#解压文件
root@kylin:~/scpfile# tar -zxvf jdk-8u181-linux-x64.tar.gz -C /usr/java/
root@kylin:~/scpfile# cd /usr/java/
#更改文件夹用户组和用户名
root@kylin:/usr/java# chown -R root:root jdk1.8.0_181/
root@kylin:/usr/java# ln -sf /usr/java/jdk1.8.0_181 /usr/java/latest
root@kylin:/usr/java# ln -sf /usr/java/latest /usr/java/default
root@kylin:/usr/java# ll
total 12
drwxr-xr-x  3 root root 4096 May 13 17:29 ./
drwxr-xr-x 15 root root 4096 May 13 17:12 ../
lrwxrwxrwx  1 root root   16 May 13 17:29 default -> /usr/java/latest/
drwxr-xr-x  7 root root 4096 Jul  7  2018 jdk1.8.0_181/
lrwxrwxrwx  1 root root   22 May 13 17:29 latest -> /usr/java/jdk1.8.0_181/
root@kylin:/usr/java# sudo vim /etc/profile
#---
#添加 Java 相关的环境变量到/etc/profile
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
#---
root@kylin:/usr/java# source /etc/profile
root@kylin:/usr/java# echo $JAVA_HOME
/usr/java/latest
root@kylin:/usr/java# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
root@kylin:/usr/java#

2.8 Ubuntu 添加用户组和用户

root@kylin:~# sudo groupadd hadoop
root@kylin:~# sudo useradd -s /bin/bash -g hadoop -d /home/hadoop -m hadoop
root@kylin:~# echo hadoop | passwd --stdin hadoop
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]

Options:
  -a, --all                     report password status on all accounts
  -d, --delete                  delete the password for the named account
  -e, --expire                  force expire the password for the named account
  -h, --help                    display this help message and exit
  -k, --keep-tokens             change password only if expired
  -i, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --lock                    lock the password of the named account
  -n, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -q, --quiet                   quiet mode
  -r, --repository REPOSITORY   change password in REPOSITORY repository
  -R, --root CHROOT_DIR         directory to chroot into
  -S, --status                  report password status on the named account
  -u, --unlock                  unlock the password of the named account
  -w, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS
  -x, --maxdays MAX_DAYS        set maximum number of days before password
                                change to MAX_DAYS

root@kylin:~# sudo passwd hadoop
New password:
Retype new password:
passwd: password updated successfully

# 给hadoop用户与root一般的权限
root@kylin:~# echo "%hadoop        ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers
root@kylin:~# cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
%hadoop        ALL=(ALL)       NOPASSWD: ALL

2.9 创建系统初始化和服务初始化文件

# 2.9.1 创建系统初始化文件
root@kylin:~# touch InitSystem
root@kylin:~# chmod u+x InitSystem
root@kylin:~# vim InitSystem
root@kylin:~# cat InitSystem | lolcat

#!/bin/bash
#InitSystem.sh
###########################
#Linux(Ubuntu) System Init
#Author kylin
###########################

WORK_DIR=$(pwd)
#Only root
[[ $EUID -ne 0 ]] && echo 'Error: This script must be run as root!' && exit 1

#Enable ssh
service ssh start

root@kylin:~# ./InitSystem
 * Starting OpenBSD Secure Shell server sshd               [ OK ]


# 2.9.2 创建系统服务Enable文件,方便开启相关服务
root@kylin:~# touch InitService
root@kylin:~# chmod u+x InitService
root@kylin:~# cat InitSystem >> InitService
root@kylin:~# vim InitService
root@kylin:~# cat InitService | lolcat
#!/bin/bash
#InitService 
##############################
#Linux(Ubuntu) Service Enable
#Author kylin
##############################

WORK_DIR=$(pwd)
#Only root
[[ $EUID -ne 0 ]] && echo 'Error: This script must be run as root!' && exit 1

#IP Addr.
ip=`ifconfig eth0 | egrep -o "inet [^ ]*" | grep -o "[0-9.]*"`
echo "$ip  cloudera.kylin.com  cloudera" >> /etc/hosts
# 由于WSL目前不知道如何使用静态IP,并且WSL中的/etc/hosts文件是根据Windows系统自动生成的,每次重启会清除上述IP的添加信息,
# 所以上述配置在这里合时宜,行的通。如果需要固定一些配置,则可以将内容写入/etc/wsl.conf。
#Enable ssh
service ssh restart

#Disnable ufw
service ufw stop

#Enable mysql service
service mysql restart

#Enable Httpd
service apache2 restart

WSL 2 不能使用systemctl 指令,可以选择书写服务等初始化文件:

root@kylin:~# systemctl status mysql
System has not been booted with systemd as init system (PID 1). Can`t operate.
Failed to connect to bus: Host is down

# 这是因为WSL是Windows的一个子服务,终端中无法重启Windows的服务.我们无法使用reboot或使用systemctl命令来管理systemd服务。
# net stop lxssmanager

WSL 2 相关链接:
Docker Desktop WSL2 backend
相关博文-WSL2作为生产力工具

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
WSLWindows Subsystem for Linux)是在Windows平台上运行Linux子系统的一种技术。Docker是一种容器化平台,能够方便地打包、交付和运行应用程序。 要在WSL部署Java应用程序,可以使用Docker进行容器化处理。首先,在WSL中安装Docker,并启动Docker服务。通过在命令行界面运行`docker version`命令,可以确保Docker成功安装并运行。 接下来,创建一个Dockerfile,以描述如何构建Java应用程序的Docker镜像。Dockerfile是一个文本文件,包含一系列指令,用于定义容器的环境和配置。例如,可以使用以下内容创建Dockerfile: ``` FROM openjdk:11-jre-slim WORKDIR /app COPY your-app.jar . CMD ["java", "-jar", "your-app.jar"] ``` 上述Dockerfile使用了openjdk镜像作为基础镜像,并将当前目录下的应用程序.jar文件拷贝到容器的/app目录下。最后,通过CMD指令运行Java应用程序。 在WSL终端中,使用`docker build -t your-image-name .`命令构建Docker镜像,其中your-image-name是自定义的镜像名称。构建完成后,可以使用`docker images`命令来查看已创建的镜像。 最后,通过运行容器实例来部署Java应用程序。使用`docker run -it -p 8080:8080 your-image-name`命令,将容器的8080端口映射到主机的8080端口。这将在后台运行容器,并在WSL终端中显示应用程序的日志。 通过访问http://localhost:8080,即可在本地主机的浏览器中访问Java应用程序。 通过以上步骤,就可以在WSL上使用Docker部署Java应用程序了。使用Docker可以实现便捷的应用程序交付和部署,并且能够有效隔离应用程序环境,提高应用程序的可移植性和环境一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值