赞
踩
导语:汇总一下在wsl中扩容根路径大小 ,在局域网暴露端口,获取磁盘uuid
/
路径的大小powershell中执行
wsl --shutdown
diskpart.exe
因为我是用发行版安装的 直接扩容E:\ubuntu18_04-disk\ext4.vhdx
Select vdisk file="E:\ubuntu18_04-disk\ext4.vhdx"
detail vdisk
expand vdisk maximum=512000
detail vdisk
exit
powershell
wsl.exe
sudo mount -t devtmpfs none /dev
mount | grep ext4
# 可以通过lsblk看到sdb和sda都是挂到/的 所以扩容sdb就是扩容/,sda不支持ext4
sudo resize2fs /dev/sdb 512000M
参考
https://docs.microsoft.com/zh-cn/windows/wsl/vhd-size
因为交付的实际需要一个唯一的uuid 原先是使用/sys/class/dmi/id/product_uuid
,但是在wsl中没有这个文件。
更换为
blkid /dev/sda1
容器运行时需要特权模式 --privileged=true
否则就算通过挂载的方式 获取到的也为空值。
以管理员在powershell中执行
netsh interface ip add address "vEthernet (WSL)" 172.16.10.174 255.255.255.0
wsl -d Ubuntu-18.04 -u root ip addr add 172.16.10.174/24 broadcast 172.16.10.255 dev eth0 label eth0:1
这样执行后的确是绑定了对应的ip,但是在外网无法ping通,也不能通过对应ip的端口号访问docker中的服务。仅仅只能在docker中ping的通而已,不知道是不是我操作的问题。
参考
https://blog.csdn.net/weixin_32300981/article/details/112651567
以管理员在powershell中执行。不要关闭防火墙,否则会报错
和资料中的稍有改动,因为我用原来的命令获取winip和wslip,发现格式不对。自己调整了一下。大佬完整的脚本贴在最后了,我没测试。这个重启后wsl的ip会变,可以尝试固定wsl的ip,这样不用每次开机都执行。
# 获取 Windows 和 WSL2 的 ip # $winip = bash.exe -c "ip route | grep default | awk '{print `$3}'" # default via 172.20.16.1 dev eth0 $winip = bash.exe -c "ip route | grep default |cut -d ' ' -f 3" # $wslip = bash.exe -c "hostname -I | awk '{print `$1}'" # 172.20.30.87 172.16.10.174 172.18.0.1 172.17.0.1 $wslip = bash.exe -c "hostname -I |cut -d ' ' -f 1 " $found1 = $winip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; $found2 = $wslip -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; if( !($found1 -and $found2) ){ # 如果没找到 wsl 的 ip, 就退出执行 echo "The Script Exited, the ip address of WSL 2 cannot be found"; exit; } # 你需要映射到局域网中端口 $ports=@(80,81,22222,22,800,11380,11381); # 监听的 ip,这么写是可以来自局域网 $addr='0.0.0.0'; # 监听的端口,就是谁来访问自己 $ports_a = $ports -join ","; # 移除旧的防火墙规则 iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' " | Out-Null # 允许防火墙规则通过这些端口 iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP" | Out-Null iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP" | Out-Null # 使用 portproxy 让 Windows 转发端口 # https://docs.microsoft.com/en-us/windows-server/networking/technologies/netsh/netsh-interface-portproxy for( $i = 0; $i -lt $ports.length; $i++ ){ $port = $ports[$i]; iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr" | Out-Null iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$wslip" | Out-Null }
先安装一个ssh
# service ssh status 可以看到ssh服务并不存在 netstat -luntp 也看不到22端口或者其他22端口
apt update
apt install openssh-server
# 如有需要则修改 /etc/ssh/sshd_config中的Port 22
# 如果需要使用root登录需要修改/etc/ssh/sshd_config 修改PermitRootLogin prohibit-password 为 PermitRootLogin yes
service ssh start
测试连接
修改配置后用root登录
意外情况 174也可以访问
在windows外部的其他局域网中通过windows ip可以访问,但是额外加的ip 174不可以
typeset -U PATH export EDITOR=nvim export TIME_STYLE='long-iso' export CC=gcc-9 export GPG_TTY=$(tty) if type brew &>/dev/null; then FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH fi # alias alias l="exa -la" alias ls="exa" alias la="exa -lah" alias b=brew alias vim="nvim" alias y=yarn alias py="python3" alias ipy="ipython" alias ohmyzsh="cd ~/.oh-my-zsh" alias vizsh="vim ~/.zshrc" alias c="code-insiders" alias e="explorer.exe" alias q="cd $HOME && clear" alias cdtmp='cd `mktemp -d /tmp/artin-XXXXXX`' alias ws="cd ~/0Workspace" alias le="cd ~/0learn-everything" alias udtheme="cp -r ~/dotfiles/zsh-theme/. ~/.oh-my-zsh/custom/themes/" alias udwsl='powershell.exe "sudo powershell -ExecutionPolicy ByPass -File ~\dotfiles\windows\wsl2.ps1"' alias cls=clear alias rmrf="rm -rf" alias srmrf="sudo rm -rf" alias vimrc="vim ~/.config/nvim/init.vim" alias ping="nali-ping" alias dig="nali-dig" alias d=docker alias lzd=lazydocker alias dco="docker-compose" alias traceroute="nali-traceroute" alias tracepath="nali-tracepath" alias nslookup="nali-nslookup" alias shutdown="wsl.exe --shutdown" alias clip="clip.exe" alias apt-update="sudo apt-get update && sudo apt-get -y upgrade" alias ncdux="ncdu -X ~/dotfiles/_rc/ncduignore" alias lg=lazygit alias top=htop alias fd=fdfind alias g=git alias tk=take alias cg=cargo alias gcid="git log | head -1 | awk '{print substr(\$2,1,7)}'" alias src="source ~/.zshrc" alias -s gz='tar -xzvf' alias -s tgz='tar -xzvf' alias -s zip='unzip' alias -s bz2='tar -xjvf' # Enable aliases to be sudo’ed # http://askubuntu.com/questions/22037/aliases-not-available-when-using-sudo alias _='sudo ' # Avoid stupidity with trash-cli: # https://github.com/sindresorhus/trash-cli # or use default rm -i if (( $+commands[trash] )); then alias rm='trash' else alias rm='rm -i' fi if [ -d "$HOME/bin" ] ; then export PATH="$HOME/bin:$PATH" fi if [ -d "$HOME/.local/bin" ] ; then export PATH="$HOME/.local/bin:$PATH" fi eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv) export PATH="~/.npm-global/bin:$PATH" export PATH="$HOME/.poetry/bin:$PATH" export PATH="$HOME/.cargo/bin:$PATH" export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH" setopt no_nomatch # Proxy configuration getIp() { export winip=$(ip route | grep default | awk '{print $3}') export wslip=$(hostname -I | awk '{print $1}') export PROXY_SOCKS5="socks5://${winip}:7890" export PROXY_HTTP="http://${winip}:7890" } proxy_git() { ssh_proxy="${winip}:7890" git config --global http.https://github.com.proxy ${PROXY_HTTP} if ! grep -qF "Host github.com" ~/.ssh/config ; then echo "Host github.com" >> ~/.ssh/config echo " User git" >> ~/.ssh/config echo " ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" >> ~/.ssh/config else lino=$(($(awk '/Host github.com/{print NR}' ~/.ssh/config)+2)) sed -i "${lino}c\ ProxyCommand nc -X 5 -x ${ssh_proxy} %h %p" ~/.ssh/config fi } winip_() { getIp echo ${winip} } wslip_() { getIp echo ${wslip} } x11() { getIp if [ ! $1 ]; then # null export DISPLAY=${winip}:0.0 else export DISPLAY=${winip}:$1.0 fi echo $DISPLAY export XDG_SESSION_TYPE=x11 export XDG_RUNTIME_DIR=/tmp/runtime-root export LIBGL_ALWAYS_INDIRECT=1 export PULSE_SERVER=tcp:$winip } ip_() { getIp https --follow -b https://api.ip.sb/geoip/$1 echo "WIN ip: ${winip}" echo "WSL ip: ${wslip}" } proxy_npm() { getIp npm config set proxy ${PROXY_HTTP} npm config set https-proxy ${PROXY_HTTP} yarn config set proxy ${PROXY_HTTP} yarn config set https-proxy ${PROXY_HTTP} } unpro_npm() { npm config delete proxy npm config delete https-proxy yarn config delete proxy yarn config delete https-proxy } proxy() { getIp # pip can read http_proxy & https_proxy export http_proxy="${PROXY_HTTP}" export HTTP_PROXY="${PROXY_HTTP}" export https_proxy="${PROXY_HTTP}" export HTTPS_PROXY="${PROXY_HTTP}" export ftp_proxy="${PROXY_HTTP}" export FTP_PROXY="${PROXY_HTTP}" export rsync_proxy="${PROXY_HTTP}" export RSYNC_PROXY="${PROXY_HTTP}" export ALL_PROXY="${PROXY_SOCKS5}" export all_proxy="${PROXY_SOCKS5}" proxy_git if [ ! $1 ]; then ip_ fi echo "Acquire::http::Proxy \"${PROXY_HTTP}\";" | sudo tee /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1 echo "Acquire::https::Proxy \"${PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null 2>&1 } unpro () { unset http_proxy unset HTTP_PROXY unset https_proxy unset HTTPS_PROXY unset ftp_proxy unset FTP_PROXY unset rsync_proxy unset RSYNC_PROXY unset ALL_PROXY unset all_proxy sudo rm /etc/apt/apt.conf.d/proxy.conf git config --global --unset http.https://github.com.proxy ip_ } git-config() { echo -n "Please input Git Username: " read username echo -n "Please input Git Email: " read email echo -n "Done!" git config --global user.name "${username}" git config --global user.email "${email}" git config --global credential.helper store git config --global pull.rebase true git config --global alias.s status git config --global alias.sb "status -sb" git config --global alias.d diff git config --global alias.co checkout git config --global alias.br branch git config --global alias.last "log -1 HEAD" git config --global alias.cane "commit --amend --no-edit" git config --global alias.pr "pull --rebase" git config --global alias.lo "log --oneline -n 10" git config --global alias.a "add ." git config --global alias.cm "commit -m" git config --global alias.rh "reset --hard" git config --global alias.f "fetch" } ssh_start() { sshd_status=$(service ssh status) if [[ $sshd_status = *"is not running"* ]]; then sudo service ssh --full-restart fi } set_max_user_watches() { if ! grep -qF "max_user_watches" /etc/sysctl.conf ; then echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf fi sudo sysctl -p >/dev/null 2>&1 sudo sysctl --system >/dev/null 2>&1 } expose_local(){ sudo sysctl -w net.ipv4.conf.all.route_localnet=1 >/dev/null 2>&1 sudo iptables -t nat -I PREROUTING -p tcp -j DNAT --to-destination 127.0.0.1 } put_win_fonts() { # 将windows的字体放入ubuntu里 sudo mkdir /usr/share/fonts/windows sudo cp -r /mnt/c/Windows/Fonts/*.ttf /usr/share/fonts/windows/ fc-cache } bk() { cp ~/.zshrc ~/dotfiles/ubuntu_wsl/zshrc cp ~/.config/nvim/init.vim ~/dotfiles/ubuntu_wsl/init.vim cp ~/.config/nvim/coc-settings.json ~/dotfiles/ubuntu_wsl/coc-settings.json brew bundle dump --describe --force --no-upgrade --file="~/dotfiles/ubuntu_wsl/Brewfile" } ln_never_wrong() { ln -s "$1" "$2" symlinks -d $(dirname "$2") if [ ! -f "$2" ]; then echo "bloody hell… I got it wrong again" ln -s "$2" "$1" fi } gpg-login() { export GPG_TTY=$TTY echo "test" | gpg --clearsign > /dev/null 2>&1 } gpg-logout() { echo RELOADAGENT | gpg-connect-agent } u-clean() { echo "clean" sudo apt-get clean echo "auto clean" sudo apt-get autoclean -y echo "auto remove" sudo apt-get autoremove -y echo "purge deborphan" deborphan | xargs sudo apt-get purge -y deborphan sudo aptitude search ?obsolete echo "aptitude purge" sudo aptitude purge ~o } u-update() { sudo apt-get update && sudo apt-get -y upgrade brew upgrade --verbose nali update -y tldr --update } zsh_history_fix() { mv ~/.zsh_history ~/.zsh_history_bad strings ~/.zsh_history_bad > ~/.zsh_history fc -R ~/.zsh_history rm ~/.zsh_history_bad } cdlast() { cd - ls -lrth --color=auto | tail zle reset-prompt } zle -N cdlast bindkey '^Q' cdlast # Kills a process running on a specified tcp port killport() { for port in "$@" do echo "Killing process on port: $port" fuser -n tcp -k $port; done } # MVP # Move and make parent directories mvp() { source="$1" target="$2" target_dir="$(dirname "$target")" mkdir --parents $target_dir; mv $source $target } cleantmp() { sudo find /tmp -type f -atime +10 -delete } eval "$(pyenv init -)" eval "$(starship init zsh)" if [[ ! -f $HOME/.zinit/bin/zinit.zsh ]]; then print -P "%F{33}▓▒░ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}zdharma/zinit%F{220})…%f" command mkdir -p "$HOME/.zinit" && command chmod g-rwX "$HOME/.zinit" command git clone https://github.com/zdharma/zinit "$HOME/.zinit/bin" && \ print -P "%F{33}▓▒░ %F{34}Installation successful.%f%b" || \ print -P "%F{160}▓▒░ The clone has failed.%f%b" fi source "$HOME/.zinit/bin/zinit.zsh" autoload -Uz _zinit (( ${+_comps} )) && _comps[zinit]=_zinit zinit snippet OMZP::sudo zinit snippet OMZ::lib/history.zsh zinit snippet OMZP::extract zinit light zsh-users/zsh-completions zinit light zsh-users/zsh-autosuggestions zinit light zsh-users/zsh-syntax-highlighting zinit light sukkaw/zsh-ipip zinit light agkozak/zsh-z
参考
https://bytem.io/posts/wsl2-network-tricks/
https://github.com/bytemain/dotfiles/blob/master/ubuntu_wsl/zshrc
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。