Category: Linux

10 Posts

在win10(win11)搭建开发环境
本人nodejs主力开发语言,使用vim编辑器。搭建环境包括: wsl2zsh / powerlevel10k theamtmuxneovim / SpaceVimnvm / nodejsWindows Terminal 安装wsl2 参考链接 先安装Windows Terminal(win11自带了),然后使用管理员身份打开 Windows Powershell,执行命令: dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart 然后重启电脑,重启后下载安装wsl内核。然后执行: wsl --set-default-version 2 安装内核后即可到Microsoft Store安装Ubuntu 20.04。安装后点击打开,第一次打开会自动安装linux,输入用户名、密码。 初始化wsl2 mv /etc/apt/sources.list /etc/apt/sources.list.bak sudo su 输入密码后,再执行: cat > /etc/apt/sources.list <<EOF # 中科大 @see https://www.cnblogs.com/leeyazhou/p/12976814.html deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse EOF exit 然后把系统更新一下: sudo apt update sudo apt upgrade 安装一些基础库: sudo apt install automake build-essential pkg-config libevent-dev ncurses-dev bison 配置文件 # 我的配置文件整理在GitHub cd ~ mkdir Applications workspace cd Applications/ git clone git@git.zhlh6.cn:ilaipi/dotfiles.git git仓库地址 安装zsh / oh-my-zsh sudo apt install zsh # 这里如果下载不了,那就到windows打开浏览器,手动下载脚本,到wsl中执行脚本即可 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" cd ~/Applications/dotfiles/ # 安装zsh的主题powerlevel10k并完成配置 sh -x install.sh ln…
Linux系统挂载系统盘
参考链接 ,根据参考链接,判断是否满足扩容条件。 参考阿里云的文档,总结: # 1 创建分区 fdisk /dev/vdb # 依次输入 n(新建分区) p(主分区) # 回车(默认1个分区) 回车(默认起点) 回车(默认终点) # p(查看分区信息) w(写入) # 然后执行以下命令,实现开机挂载硬盘 # 可修改 /mnt 为真实的挂载点,比如 修改为 /data 则把硬盘挂载到 /data目录 echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/\"//g'` /mnt ext4 defaults 0 0 >> /etc/fstab
使用NPS
GitHub仓库 部署架构 公网服务器A,负载均衡B,内网服务器C 在A上安装nps的服务端,并通过B访问nps服务。 服务端 appname = nps #Boot mode(dev|pro) runmode = dev #HTTP(S) proxy port, no startup if empty http_proxy_ip=0.0.0.0 http_proxy_port= https_proxy_port= https_just_proxy=true #default https certificate setting https_default_cert_file=conf/server.pem https_default_key_file=conf/server.key ##bridge bridge_type=tcp bridge_port=${YOUR_BRIDGE_PORT} bridge_ip=0.0.0.0 # Public password, which clients can use to connect to the server # After the connection, the server will be able to open relevant ports and parse related domain names according to its own configuration file. public_vkey=${YOUR_PUBLIC_VKEY} #Traffic data persistence interval(minute) #Ignorance means no persistence #flow_store_interval=1 # log level LevelEmergency->0 LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7 log_level=7 #log_path=nps.log #Whether to restrict IP access, true or false or ignore #ip_limit=true #p2p #p2p_ip=127.0.0.1 #p2p_port=6000 #web web_host= web_username=npsadmin web_password=${YOUR_ADMIN_PASSWORD} web_port = ${YOUR_WEB_PORT} web_ip=0.0.0.0 web_base_url= web_open_ssl=false web_cert_file=conf/server.pem web_key_file=conf/server.key # if web under proxy use sub path. like http://host/nps need this. #web_base_url=/nps #Web API unauthenticated IP address(the len of auth_crypt_key must be 16) #Remove comments if needed #auth_key=test auth_crypt_key =${YOUR_AUTH_CRYPT_KEY} #allow_ports=9001-9009,10001,11000-12000 #Web management multi-user login allow_user_login=false allow_user_register=false allow_user_change_username=false #extension allow_flow_limit=false allow_rate_limit=false allow_tunnel_num_limit=false allow_local_proxy=false allow_connection_num_limit=false allow_multi_ip=false system_info_display=false #cache http_cache=false http_cache_length=100 #get origin ip http_add_origin_header=false #pprof debug…
Ubuntu 非root远程登录
服务器使用Ubuntu,做点简单处理,使用非root用户。 ssh远程登录 首先保证能通过root连接,可用root用户执行以下命令: chown root:root -R /root/.ssh/ chmod 700 /root/.ssh/ chmod 600 /root/.ssh/authorized_keys chown me:me -R /home/lingdou/.ssh/ chmod 700 /home/me/.ssh/ chmod 600 /home/me/.ssh/authorized_keys 其中 me 是要登录的用户名,需要提前创建好。 将用户加入sudo gpasswd -a me sudo echo "me ALL=(ALL) NOPASSWD: ALL" | (EDITOR="tee -a" visudo) 禁用密码和root登录 修改 /etc/ssh/sshd_config : PermitRootLogin no PasswordAuthentication no 重启服务:service sshd restart
tmux plugins tpm启动时报错解决
使用的时zsh,配合oh-my-zsh,在wsl2 Ubuntu20 环境,使用Windows Terminal作为终端。 每次启动电脑后,打开终端,执行tmux后会报错: ~/.tmux/plugins/tpm/tpm' returned 1 网上找的解决方案是需要执行: cd .tmux/plugins/tpm sh -x tpm 试了在zsh启动时自动执行这个命令,但是没用,暂时只能每次手动执行。。
Ubuntu修改hostname
什么是hostname? 打开终端,使用bash,每行开始都是 用户名@xxxx,@后面的就是hostname。 当你有多台服务器,每台服务器作用不一样,并且需要同时登录多台服务器的时候,通过hostname来区分服务器,还是非常有必要的。 修改 使用hostnamectl命令: # 直接回车 可查看当前的hostname hostnamectl # 设置,执行后会修改/etc/hostname文件 hostnamectl set-hostname new-hostname # 然后手动修改/etc/hosts文件 # 替换其中的原hostname为新的hostname # 修改后,不需要重启服务器,退出ssh连接,重新连接即可 参考 点击打开参考链接
初始化WSL 2 + nodejs
Deprecated @see 发行版:Ubuntu 20.04 系统更新 # cat EOF 用法:https://blog.csdn.net/lym152898/article/details/83306993 mv /etc/apt/sources.list /etc/apt/sources.list.bak sudo su cat > /etc/apt/sources.list <<EOF # 中科大 @see https://www.cnblogs.com/leeyazhou/p/12976814.html deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse EOF exit sudo apt update sudo apt upgrade 编译安装vim https://www.ilaipi.top/2019/11/22/vim-%e7%bc%96%e8%af%91%e5%ae%89%e8%a3%85-python3%e6%94%af%e6%8c%81/ 安装zsh sudo apt install zsh 安装nvm&node https://github.com/nvm-sh/nvm 下载install.sh,然后执行: bash install.sh 安装vim套件 mkdir Applications && cd Applications git clone git@git.zhlh6.cn:vim/vim.git --depth=1 # github加速插件提供的加速地址 git clone git@github.com:ilaipi/k-vim.git --depth=1 git clone git@github.com:ilaipi/k-tmux.git --depth=1 https://github.com/ohmyzsh/ohmyzsh/tree/master/tools # 先下载 install.sh 然后执行 sh -c install.sh git clone git@git.zhlh6.cn:ilaipi/dotfiles.git cd dotfiles && sh -x intall.sh git clone git@git.zhlh6.cn:tmux/tmux.git --depth=1 # install tmux @see https://github.com/tmux/tmux sudo apt install automake sudo apt install build-essential sudo apt-get install pkg-config sudo apt install libevent-dev sudo…
mongodb备份脚本
DUMP=/usr/local/mongodb/bin/mongodump #mongodump命令路径 OUT_DIR=/data/mongodb_bak/mongodb_bak_now #临时备份目录 TAR_DIR=/data/mongodb_bak/mongodb_bak_list #备份存放路径 DATE=`date +%Y_%m_%d_%H_%M` #获取当前系统时间 DB_USER=YOUR_DB_USERNAME #数据库账号 DB_PASS=YOUR_DB_USER_PASSWORD #数据库密码 DAYS=20 #DAYS=20代表删除20天前的备份,即只保留近20天的备份 TAR_BAK="mongodb_bak_$DATE.tar.gz" #最终保存的数据库备份文件 cd $OUT_DIR rm -rf $OUT_DIR/* mkdir -p $OUT_DIR/$DATE $DUMP -h YOUR_DB_HOST -u $DB_USER -p $DB_PASS --authenticationDatabase "YOUR_DB_NAME" -o $OUT_DIR/$DATE #备份全部数据库 tar -zcvf $TAR_DIR/$TAR_BAK $OUT_DIR/$DATE #压缩为.tar.gz格式 find $TAR_DIR/ -mtime +$DAYS -delete #删除20天前的备份文件 exit 复制代码,命名为mongodb_bak.sh,加入系统定时任务。 crontab -e
acme.sh证书同步到华为云
项目代码 https://github.com/ilaipi/acme.sh-dockerhttps://github.com/ilaipi/huawei-cloud 环境 华为云 ubuntu 18.04Docker version 19.03.8, build afacb8b7f0docker-compose version 1.25.4, build 8d51620image: neilpang/acme.sh v2.8.6image: jenkins/jenkins:lts Jenkins ver. 2.204.5 目标 acme.sh自动更新证书使用华为云的负载均衡 https监听证书更新后自动同步到华为云 实现方案 server上通过docker部署acme.sh使用nodejs调用华为云api实现更新证书使用jenkins实现证书更新后自动调用nodejs 部署acme.sh实现自动更新证书 acme.sh可以通过aliyun_dns_api完成证书颁发,需要阿里云的app_key和app_secret。 参考链接 你可以直接安装acme.sh就能实现。但是当你有多个域名并且分布在多个阿里云帐号的时候,直接安装acme.sh可能解决不了问题(尝试过,但是没配置成功),而用docker跑acme.sh可以完美解决这个问题。所以这里用docker来跑acme.sh。 参考 ilaipi/acme.sh-docker 完成部署 自动更新证书到华为云 到 GitHub Repo 看源码部署 Jenkins自动同步到华为云 acme.sh有notify机制,notify到个人邮箱,jenkins配置一个任务,通过邮件触发(需要安装一个邮件触发的插件Poll Mailbox Trigger Plugin),检查到邮件就执行服务器上的自动更新代码。 (目前还在实验阶段) 在acme的容器内配置邮件发送服务,使用acme支持的mailgun,注册帐号并完成邮件认证。邮件认证的时候可能需要google voice号码(我认证的时候没有看到中国的选项)。最后调用acme.sh --set-notify --notify-hook mailgun来确认是否配置成功,配置成功的话会发邮件到接收邮箱。 在jenkins中配置的是outlook邮箱(host: imap-mail.outlook.com)。 Jenkins Advanced Email Properties subjectContains=Renew *.xxxxxxx.com successreceivedXMinutesAgo=600 # 意思是10小时内发送的邮件。一般acme是凌晨0点后执行任务 Jenkins Schedule TZ=Asia/ShanghaiH H(3-7)/3 * * *# 这样的意思应该是早上3-7点之间执行3次
thumbnail
ssh 连接保持不断
环境:MBP iTerm2 阿里云ecs 网络:三大运营商没法进入物业,只能用物业提供的一家服务商,8000/年,可能是铁通的网络,非常不稳定,经常GitHub提交代码Push不上去,对外IP地址变化非常快。 平时工作经常要操作阿里云ecs,就通过iTerm2 开个Tab,通过ssh连接服务器。苦恼的是,一段时间不用,ssh连接就会断开。 一直以为是网络IP的问题,不知道怎么解决。 今天突然搜了一下,找到了一个解决办法,在~/.ssh/config文件中增加: # 15 值可以改 大于1即可 表示每隔多少秒发送心跳 ServerAliveInterval 15 放在Host同级。效果图: 测试了一下,出去吃了顿饭回来,连接没有断。