建议直接使用root
用户,由于不是用于服务器,所以连密码也不必设置,可以省去很多操作
如果使用新建其他用户如youruser
,建议不要将需要提权操作的文件和写入环境变量的路径放在/home/youruser/
即该用户的~
路径中,否则sudo
操作可能会找不到文件
# CMD/PowerShell 指定root为默认用户
ubuntu1804.exe config --default-user root
cp /etc/apt/sources.list /etc/apt/sources.list.backup
vim /etc/apt/sources.list
#阿里源
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
vim ~/.bashrc
host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
alias setproxy="export all_proxy=http://$host_ip:7890;export http_proxy=http://$host_ip:7890;export https_proxy=http://$host_ip:7890;"
alias unsetproxy="unset all_proxy;unset http_proxy;unset https_proxy;"
由于wsl2的pid 1被windows占用,默认情况不支持systemctl
我们可以利用nsenter和daemonize,在新的namespace里给systemd一个pid 1
安装daemonizeapt install -y fontconfig daemonize
配置systemd,编辑 /etc/profile
添加以下内容
# systemd
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
if [ -z "$SYSTEMD_PID" ]; then
sudo /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
fi
if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
fi
配置免密sudo,编辑/etc/sudoers
添加以下内容chmod 755 /etc/sudoers
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
%sudo ALL=(ALL) NOPASSWD: /usr/bin/nsenter -t [0-9]* -a su - [a-zA-Z0-9]*
chmod 440 /etc/sudoers
重启 WSL2 or source profile
source /etc/profile
此时window的命令将失效,如explorer.exe
输入exit可以退出此namespace,此时可以使用windows的命令
注意如果sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
改成exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
则输入exit会直接退出终端
mongodb-org
apt-get install gnupg
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 4B7C549A058F8B6B
apt-get update
apt install mongodb-org
systemctl enable mongod
git make svnapt-get install git make subversion
redisapt-get install redis-server
安装可能报错,需要关闭redis默认开启的ipv6,然后再运行一次安装命令vim /etc/redis/redis.conf
- bind 127.0.0.1 ::1
+ bind 127.0.0.1
go
wget -c https://dl.google.com/go/go1.16.10.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
echo -e "\n# GoPath\nexport PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
etcd nats
wget -c https://github.com/etcd-io/etcd/releases/download/v3.5.1/etcd-v3.5.1-linux-amd64.tar.gz -O - | sudo tar -xz -C ~/go/bin
wget -c https://github.com/nats-io/nats-server/releases/download/v2.2.0/nats-server-v2.2.0-linux-amd64.tar.gz -O - | sudo tar -xz -C ~/go/bin
wget -c https://github.com/nats-io/nats-streaming-server/releases/download/v0.21.1/nats-streaming-server-v0.21.1-linux-amd64.tar.gz -O - | sudo tar -xz -C ~/go/bin
vim nat.conf
# 最大的有效负载
max_payload: 134217728
# 日志选项
debug: false
trace: false
logtime: true
log_file: "./gnatsd.log"
静默启动
nohup ./etcd 2>&1 > etcd.log &
nohup ./nats-server -c ./nat.conf 2>&1 > nats_start.log &
nohup ./nats-streaming-server -p 4333 -m 8222 2>&1 > nats_streaming.log &
mercurial
add-apt-repository ppa:mercurial-ppa/releases
sudo add-apt-repository -y -r ppa:chris-lea/node.js
sudo rm -f /etc/apt/sources.list.d/chris-lea-node_js-*.list
apt-get update
apt-get install mercurial python-nautilus tortoisehg
gohugo
apt install hugo
go get -v github.com/spf13/hugo
git生成sshssh-keygen -o
也可以从自己的其他计算机拷贝至~/.ssh
git配置用户名和https转sshvim ~/.gitconfig
[user]
name = yourname
email = yourmail
[url "[email protected]:"]
insteadOf = https://gitlab.example.com
由于wsl2每次启动ip都会变化,所以在每次启动时将ip写入wslhost使外部软件访问wsl不必频繁修改ipvim ~/.bashrc
# wslhost
hostname="wslhost"
wslip=$(ip address show eth0 | grep 'inet ' | awk '{print $2}' | awk -F/ '{print $1}')
cp /mnt/c/Windows/System32/drivers/etc/hosts ~/hosts
sed -i "/$hostname/d" ~/hosts
echo "$wslip $hostname" >> ~/hosts
cp ~/hosts /mnt/c/Windows/System32/drivers/etc/hosts
source ~/.bashrc
apt install trash-cli
echo -e "\n# trash-cli\nalias rm=trash-put" >> ~/.bashrc
source ~/.bashrc
trash-put: 删除文件和目录(仅放入回收站中)
trash-list :列出被删除了的文件和目录
trash-restore:从回收站中恢复文件或目录
trash-rm:删除回收站中的文件
trash-empty:清空回收站
由于wsl2使用127.0.0.1的本地端口,其他计算机无法访问wsl2内开启的服务,所以需要设置端口转发到0.0.0.0
写一个bat文件批量设置需要转发的端口即可,端口再括号内可以用回车、空格或逗号分隔,我比较喜欢用回车分隔,可以清楚看清有几个端口
@echo off
for %%i in (
8080
8081
) do (
netsh interface portproxy add v4tov4 listenport=%%i listenaddress=0.0.0.0 connectport=%%i connectaddress=wslhost protocol=tcp
)
netsh interface portproxy show all
pause
以下bat可以批量删除所有的端口转发
@echo off
for /f "skip=5 tokens=2" %%i in ('netsh interface portproxy show all') do (
netsh interface portproxy delete v4tov4 listenport=%%i listenaddress=0.0.0.0
)
netsh interface portproxy show all
pause
WSL2 ubuntu 18.04 配置开发环境 - 点击领取