常用软件常用命令

常用软件常用命令

产生随机字符串

head -c 32 /dev/random | base64

sudo免密码设置

sudo visudo

压缩相关

tar -cvf temp.tar temp.txt temp/
tar -xvf temp.tar -C temp/
tar cvzf - pic | split -b 10m -d - pic
cat pic* > pic.tar.gz
tar xvzf pic.tar.gz

Conda

描述 命令
查看都有什么环境 conda info --envs
查看当前环境有什么包 conda list
创建环境 conda create -n env_name python=version package_names
安装包 conda install name=version
离线安装包 conda install --use-local name
导出当前环境 conda env export > name.yaml
导出base环境需要更换名称 conda create -n new_name --clone base
复制后导入 conda env create -f name.yaml
删除环境下的某个包 conda env remove -n your_env_name package_name
删除环境 conda env remove --name your_env_name
打包环境 conda pack -n my_env -o out_name.tar.gz
利用打包的环境复现 tar -xzf my_env.tar.gz -C my_env && source my_env/bin/activate

Anaconda环境离线迁移移植

Linux 系统安装

sudo apt update
sudo apt install vim
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
sudo apt update
sudo apt upgrade
sudo apt install python3-pip


sudo mkdir ~/.pip
cd ~/.pip
sudo vim pip.conf
# [global]
# index-url=https://mirrors.aliyun.com/pypi/simple/ 
# https://pypi.org/simple


vim ~/.bashrc
export PATH=~/mypath/bin:$PATH
source ~/.bashrc

添加%zhangzhao ALL=(ALL:ALL) NOPASSWD: ALL 到最后一行 , 后 ctrl+o, 回车 , ctrl+x 退出

VSCode

How can I install vscode-server in linux offline

科学上网相关

V2ray安装

安装说明:https://www.v2fly.org/guide/install.html

脚本下载地址:https://github.com/v2fly/fhs-install-v2ray

执行脚本前首先临时更改hosts,避免github连接不上的情况

参考仓库:https://github.com/521xueweihan/GitHub520

执行脚本下载安装V2ray

bash install-release.sh

安装后的文件位置:

installed: /usr/local/bin/v2ray
installed: /usr/local/share/v2ray/geoip.dat
installed: /usr/local/share/v2ray/geosite.dat
installed: /usr/local/etc/v2ray/config.json
installed: /var/log/v2ray/
installed: /var/log/v2ray/access.log
installed: /var/log/v2ray/error.log
installed: /etc/systemd/system/v2ray.service
installed: /etc/systemd/system/v2ray@.service

按照上面的配置文件路径写入配置文件(可以直接从Windows客户端中copy过来)

V2ray命令:

# 启动V2ray
systemctl start v2ray
# 检查V2ray状态
systemctl status v2ray
# 设置V2ray开机自启动
systemctl enable v2ray

测试:

curl -x socks5://127.0.0.1:10808 https://www.google.com -v

clash安装

下载地址:https://github.com/Dreamacro/clash/releases/tag/v1.12.0

解压并赋予权限:

gzip -d clash-linux-amd64-v1.11.4.gz
chmod a+x clash-linux

有的代理服务商会直接给出配置文件config.yaml,如果没有,可以将订阅链接直接粘贴在浏览器网址栏,然后搜索,会直接下载下来文件或者展示出配置文件,如果搜索到的是一大堆字符则需要在订阅链接的后面添加 &flag=clash ,然后会下载下来一个文件,将其更名为config.yaml即可

然后替换~/.config/clash下自动生成的config.yaml,删除Country.mmdb文件,然后再次执行 ./clash-linux

即可以使用

释放9090端口后可以通过Web端查看:http://clash.razord.top

(WSL2 git push时候可能会遇到错误,解决方法:将下述代码粘贴到~/.ssh/config文件中)

Host github.com
Hostname ssh.github.com
Port 443

proxychains安装

安装proxychains从而避免全局代理

apt install proxychains4

配置文件:(/etc/proxychains.conf)

# proxychains.conf  VER 4.x
#
#        HTTP, SOCKS4a, SOCKS5 tunneling proxifier with DNS.


# The option below identifies how the ProxyList is treated.
# only one option should be uncommented at time,
# otherwise the last appearing option will be accepted
#
# dynamic_chain
#
# Dynamic - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped)
# otherwise EINTR is returned to the app
#
strict_chain
#
# Strict - Each connection will be done via chained proxies
# all proxies chained in the order as they appear in the list
# all proxies must be online to play in chain
# otherwise EINTR is returned to the app
#
#round_robin_chain
#
# Round Robin - Each connection will be done via chained proxies
# of chain_len length
# all proxies chained in the order as they appear in the list
# at least one proxy must be online to play in chain
# (dead proxies are skipped).
# the start of the current proxy chain is the proxy after the last
# proxy in the previously invoked proxy chain.
# if the end of the proxy chain is reached while looking for proxies
# start at the beginning again.
# otherwise EINTR is returned to the app
# These semantics are not guaranteed in a multithreaded environment.
#
#random_chain
#
# Random - Each connection will be done via random proxy
# (or proxy chain, see  chain_len) from the list.
# this option is good to test your IDS :)

# Make sense only if random_chain or round_robin_chain
#chain_len = 2

# Quiet mode (no output from library)
# quiet_mode

# Proxy DNS requests - no leak for DNS data
proxy_dns

# set the class A subnet number to use for the internal remote DNS mapping
# we use the reserved 224.x.x.x range by default,
# if the proxified app does a DNS request, we will return an IP from that range.
# on further accesses to this ip we will send the saved DNS name to the proxy.
# in case some control-freak app checks the returned ip, and denies to 
# connect, you can use another subnet, e.g. 10.x.x.x or 127.x.x.x.
# of course you should make sure that the proxified app does not need
# *real* access to this subnet. 
# i.e. dont use the same subnet then in the localnet section
#remote_dns_subnet 127 
#remote_dns_subnet 10
remote_dns_subnet 224

# Some timeouts in milliseconds
tcp_read_time_out 15000
tcp_connect_time_out 8000

### Examples for localnet exclusion
## localnet ranges will *not* use a proxy to connect.
## Exclude connections to 192.168.1.0/24 with port 80
# localnet 192.168.1.0:80/255.255.255.0

## Exclude connections to 192.168.100.0/24
# localnet 192.168.100.0/255.255.255.0

## Exclude connections to ANYwhere with port 80
# localnet 0.0.0.0:80/0.0.0.0

## RFC5735 Loopback address range
## if you enable this, you have to make sure remote_dns_subnet is not 127
## you'll need to enable it if you want to use an application that 
## connects to localhost.
# localnet 127.0.0.0/255.0.0.0

## RFC1918 Private Address Ranges
# localnet 10.0.0.0/255.0.0.0
# localnet 172.16.0.0/255.240.0.0
# localnet 192.168.0.0/255.255.0.0

# ProxyList format
#       type  ip  port [user pass]
#       (values separated by 'tab' or 'blank')
#
#       only numeric ipv4 addresses are valid
#
#
#        Examples:
#
#            	socks5	192.168.67.78	1080	lamer	secret
#		http	192.168.89.3	8080	justu	hidden
#	 	socks4	192.168.1.49	1080
#	        http	192.168.39.93	8080
#
#
#       proxy types: http, socks4, socks5
#        ( auth types supported: "basic"-http  "user/pass"-socks )
#
[ProxyList]
# add proxy here ...
# meanwile
# defaults set to "tor"
socks5 127.0.0.1 10808
http 127.0.0.1 10809

需要走代理的命令在命令开头添加proxychains即可

全局代理:(似乎对软件内部,例如go没有作用)

export http_proxy=http://127.0.0.1:10809
export https_proxy=https://127.0.0.1:10809

Go安装

下载安装

网站说明:https://golang.google.cn/doc/install

wget https://golang.google.cn/dl/go1.19.5.linux-amd64.tar.gz

删除旧版本并解压安装包:

rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.5.linux-amd64.tar.gz

编辑配置文件,增加环境变量:

vim ~/.bashrc
export PATH=$PATH:/usr/local/go/bin
source ~/.bashrc

验证安装:

go version

配置

查看配置:

go env

修改配置:

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct

拉取私有仓库的包

go env -w GOPRIVATE=gitlab.appshahe.com

除配置私有仓库的地址外,还需要将走http或者https的协议转到git协议上

具体命令:

git config --global url."git@gitlab.appshahe.com:".insteadOf "https://gitlab.appshahe.com/"
git config --global url."git@gitlab.appshahe.com:".insteadOf "http://gitlab.appshahe.com/"

具体的更改会体现在 ~/.gitconfig里面

MySQL

docker安装直接可以远程访问,不需要任何配置操作

apt install mysql-server

运行mysql服务并查看是否正在运行

service mysql start
service mysql status

刚开始安装不能使用用户名和密码访问,需要更换为原来的密码验证方式

mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';
FLUSH PRIVILEGES;

创建数据库:

CREATE DATABASE simpledy

增加远程访问的用户

取消bind-address=127.0.0.1

vim /etc/mysql/mysql.conf.d/mysqld.cnf

密码生成为随机字符串:

head -c 8 /dev/random | base64

创建用户:

CREATE USER 'dymysql'@'%' IDENTIFIED BY 'gxnw21XxRhY';

更改密码验证方式:

ALTER USER 'dymysql'@'%' IDENTIFIED WITH mysql_native_password BY 'gxnw21XxRhY';

授予用户某个数据库的全部权限:

GRANT ALL PRIVILEGES ON `simpledy`.* TO `dymysql`@`%` WITH GRANT OPTION;

撤销某个用户对某个数据库的全部权限:

REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'dymysql';

刷新缓存:

FLUSH PRIVILEGES;

展示某个用户的权限:

SHOW GRANTS FOR 'dymysql'@'%';

查看已有用户以及是否可以远程登录:

select host,user,plugin from mysql.user;

Redis

docker安装配置成功

apt install redis-server

运行并查看是否正在运行

service redis-server start
service redis-server status

设置redis密码

打开redis配置文件 /etc/redis/redis.conf

找到requirepass,修改即可

配置 Redis 远程访问

默认情况下,Redis 不允许远程连接。只能从127.0.0.1(localhost)连接 Redis 服务器

打开redis配置文件 /etc/redis/redis.conf

注释掉 bind 127.0.0.1 ::1

关闭保护模式 protected-mode no

重启Redis服务:service redis-server restart

(注意WSL的ip要到WSL里面去看)

RabbitMQ

官网安装脚本:https://www.rabbitmq.com/install-debian.html#apt-cloudsmith

注意修改apt-get为apt,将软件源设置为对应版本(如Ubuntu22.04为jammy)

查看安装状态:

service rabbitmq-server status

打开管理界面:

rabbitmq-plugins enable rabbitmq_management

通过http://localhost:15672/#/进行查看

默认的guest用户,密码为guest,具有超级管理员权限,无法远程登录

创建用户并设置密码:

add_user root QxdkQeiIUNY

管理用户角色:

  • 超级管理员(administrator):可登陆管理控制台(启用management plugin的情况下),可查看所有的信息,并且可以对用户,策略(policy)进行操作。
  • 监控者(monitoring):可登陆管理控制台(启用management plugin的情况下),同时可以查看rabbitmq节点的相关信息(进程数,内存使用情况,磁盘使用情况等)。
  • 策略制定者(policymaker):可登陆管理控制台(启用management plugin的情况下), 同时可以对policy进行管理。但无法查看节点的相关信息。
  • 普通管理者(management):仅可登陆管理控制台(启用management plugin的情况下),无法看到节点信息,也无法对策略进行管理。
  • 其他:无法登陆管理控制台,通常就是普通的生产者和消费者。(最后项目中使用的)
rabbitmqctl set_user_tags root administrator

查看当前的用户及角色:

rabbitmqctl list_users

不需要开启远程连接,自动支持

然后进入到管理页面中,对virtual hosts进行设置(相当于数据库中的db)

然后即可使用程序等跑通

FFmpeg

apt install ffmpeg

Nginx

apt install nginx

配置文件:/etc/nginx/nginx.conf

增加mp4支持:

apt install nginx-extras

vsftpd

apt install vsftpd

Protobuf

下载protobuf官方的protoc工具(tar.gz版本

编译安装:

# 安装需要的工具包
apt install autoconf automake libtool curl make g++ unzip
# 解压安装包
tar xvf protobuf-21.12.tar.gz && cd protobuf-21.12
# 编译安装
./autogen.sh
./configure
make && make install
ldconfig
# 验证安装
protoc --version

安装go语言插件:

go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc

将执行文件添加到环境变量中:

export PATH=$PATH:/root/go/bin

执行:

protoc --go_out=. video.proto

Consul

下载地址:https://developer.hashicorp.com/consul/downloads

解压后直接执行即可

Docker

官网安装方法

核心思想:

  1. Add Docker’s official GPG key
root@hecs-296470:/etc/apt/keyrings# cd /etc/apt/keyrings
root@hecs-296470:/etc/apt/keyrings# ls
docker.gpg
  1. 添加可以下载docker的源
root@hecs-296470:/etc/apt/sources.list.d# ls
docker.list
root@hecs-296470:/etc/apt/sources.list.d# cat docker.list
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu   jammy stable
root@hecs-296470:/etc/apt/sources.list.d#
  1. 安装docker
apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

换源:

vim /etc/docker/daemon.json

写入源:

{
    "registry-mirrors": [
	"https://hub-mirror.c.163.com",
	"https://ustc-edu-cn.mirror.aliyuncs.com",
	"https://ghcr.io",
	"https://mirror.baidubce.com"
    ]
}

重启docker:

systemctl daemon-reload
systemctl restart docker

环境相关

准备重装编程环境,以Docker为基础,既能开发,又能方便部署,同时不损害原有的其他环境

但是Docker Desktop坑点太多,且占用资源巨大,因此不安装Windows环境下面的Docker,而是在WSL内部安装Docker,VSCode通过SSH方式跨过WSL访问容器。

Docker安装

与上面的Docker安装基本相同,不过注意每一次重启WSL的时候要手动重启Docker,否则无法使用Docker

service docker start

网络桥接

由于WSL的ip会总变化,这里准备配桥接模式,我的理解是WSL与主机的地位相同,在内网中都有自己的ip,这样无论是互相访问还是访问外网都没有什么问题。

参考资料:

官方文档

WSL2 网络的最终解决方案

WSL2 静态IP(固定IP)不需要自动化脚本的设置方案

常用命令

描述 命令
查询容器 sudo docker ps -a
删除容器 sudo docker rm 容器ID
查询镜像 sudo docker images
删除镜像 sudo docker rmi 镜像ID(要先删除掉容器才能删除掉镜像)
拉取镜像 sudo docker pull python:3.8.13(去dockerhub上找合适的版本)
根据镜像启动容器并挂载数据 docker run -v 绝对路径:/mnt --gpus all --shm-size=6g -it python:3.8.13 /bin/bash
启动已经停止的容器 sudo docker start ID
进入某个容器的终端 sudo docker exec -it ID /bin/bash
将容器转为镜像并上传到dockerhub-登录 docker login
将容器转为镜像并上传到dockerhub-提交 sudo docker commit 容器ID zhangzhao219/仓库名(也是将容器写回到镜像中的操作)
将容器转为镜像并上传到dockerhub-打标签 sudo docker tag zhangzhao219/仓库名 zhangzhao219/TAG
将容器转为镜像并上传到dockerhub-上传 sudo docker push zhangzhao219/仓库名:TAG
导出容器到文件 sudo docker export -o *.tar 容器ID
从文件导入容器(会直接变为镜像) sudo docker import IR.tar 名称

常用软件常用命令
https://zhangzhao219.github.io/2023/01/13/Software-Commands/
作者
Zhang Zhao
发布于
2023年1月13日
许可协议