Linux 常用命令(持续更新中...)

目录

Ubuntu系统

Ubuntu 添加用户

# 新建用户
useradd ubuntu -m

# 为新用户设置密码
passwd ubuntu

# 修改新用户指向为/bin/bash
usermod -s /bin/bash ubuntu

# 在Ubuntu终端中输入
su ubuntu

# 删除
userdel -r ubuntu

配置普通用户可以执行 sudo

# 解决问题:"xxx is not in the sudoers file.This incident will be reported" 

# 切换到root用户

# 添 sudoers 文件的写权限
chmod u+w /etc/sudoers

# 编辑 sudoers 文件
vim /etc/sudoers

...
# 允许用户 youuser 执行 sudo 命令(需要输入密码)。
youuser ALL=(ALL) ALL
# 允许用户组 youuser 里面的用户执行 sudo 命令(需要输入密码)。
%youuser ALL=(ALL) ALL
# 允许用户 youuser 执行 sudo 命令,并且在执行的时候不输入密码。
youuser ALL=(ALL) NOPASSWD: ALL
# 允许用户组 youuser 里面的用户执行 sudo 命令,并且在执行的时候不输入密码。
%youuser ALL=(ALL) NOPASSWD: ALL
...

# 最后 撤销 sudoers 文件写权限
chmod u-w /etc/sudoers

安装nodejs

官网:https://nodejs.org/zh-cn/download ,选择对应的系统,以及工具(nvm)。

# Download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

# in lieu of restarting the shell
\. "$HOME/.nvm/nvm.sh"

# Download and install Node.js:
nvm install 22

# Verify the Node.js version:
node -v # Should print "v22.15.0".
nvm current # Should print "v22.15.0".

# Verify npm version:
npm -v # Should print "10.9.2".


安装 Golang

官方下载包位置: https://golang.google.cn/dl/

wget https://golang.google.cn/dl/go1.24.1.linux-amd64.tar.gz
tar -xvf go1.24.1.linux-amd64.tar.gz
mv go /usr/local

# 配置环境变量
# 打开环境变量配置文件
vim ~/.bashrc
 
...
# 粘贴以下内容
export GOROOT=/usr/local/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
 
# 建议再增加以下两项配置
# 1.13之后的版本都是用mod了,所以gopath指定个地方就可以了,三方依赖会下载到那里
export GOPATH=/go
# 配置国内镜像,下载依赖速度会很快
export GOPROXY=https://mirrors.aliyun.com/goproxy/,direct 
...

# 保存关闭后刷新一下即可
source ~/.bashrc
 
# 建议打开gomod开关
go env -w GO111MODULE=on

go version

启动 SSH

sudo apt update
sudo apt install openssh-server -y

sudo systemctl status ssh

# 生成ssh密钥对,生成目录~/.ssh
ssh-keygen

Screen命令

screen               # 创建新匿名会话
screen -S <会话名>    # 创建命名会话(推荐)
screen -r <会话名/PID> # 重新连接已断开的会话
screen -d <会话名/PID> # 强制分离会话(不终止进程)
screen -ls           # 列出所有会话(显示 PID 和状态)
Ctrl+a d             # 分离当前会话(后台运行,非终止)
Ctrl+a k             # 终止当前会话(需确认)

Nohup后台启动

nohup ./start.sh > xxx.log 2>&1 &
# nohub 忽略 SIGHUP 信号,确保终端关闭后进程仍运行
# 2>&1	将标准错误(stderr)合并到标准输出(即也写入 xxx.log)
# & 在后台运行命令

Conda常用命令行

conda -V
conda update conda
conda env list/conda info -e
conda create -n deeplearn python
conda activate deeplearn
conda list
conda install numpy
conda search numpy
conda remove numpy
conda deactivate

# 删除环境
conda remove -n deeplearn --all

使用 chrony(高精度时间同步)

  1. 安装 chrony

    sudo apt update
    sudo apt install chrony
    
  2. 启动并启用服务:

    sudo systemctl enable --now chrony
    
  3. 检查同步状态:

    chronyc tracking
    
    • 输出中的 Last offset 应接近 0,表示时间已同步。
  4. 手动强制同步:

    sudo chronyc -a makestep