This post is just for personal convenience while using ubuntu server. Some configurations and tools below are very useful but easy to forget. So I write this post to record those command lines.

目录

  1. How to build an ubuntu server quickly
    1. docker
    2. some basic tools
    3. openssh
    4. zsh
    5. conda
    6. pip
    7. git
    8. github
  2. cheatsheet for ubuntu
    1. nginx
    2. tmux
    3. ubuntu desktop

How to build an ubuntu server quickly

When I create a new account, this order will help me to handle the blank ubuntu server very fast.

docker

1
2
3
4
5
6
7
8
9
10
11
12
13
docker ps -a

docker images

docker stop <docker container name>

docker rm -f <container id>

docker run --name wgt-pytorch --runtime=nvidia -itd -v /home/wuguotong/wgt-pytorch:/wgt-pytorch -p 3320:22 -p 3321:3321 --shm-size 8G pytorch/pytorch:1.9.0-cuda11.1-cudnn8-devel

docker start wgt-pytorch

docker exec -it wgt-pytorch /bin/bash

some basic tools

Before use apt-get install, you should:

1
apt-get update

For sudo and vim

1
apt-get install sudo vim curl git htop

openssh

Change root password

1
passwd

Install openssh

1
2
3
4
5
sudo apt-get install openssh-server

sudo /etc/init.d/ssh start # start ssh server

ps -e | grep ssh

Then modify ssh configuration, use vim /etc/ssh/sshd_config, change PermitRootLogin no to PermitRootLogin yes, restart ssh:

1
sudo /etc/init.d/ssh restart

On your remote computer, the ssh configuration should be like this:

1
2
3
4
5
Host sustech-root
HostName 10.20.47.117
User root
ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p # socket5 proxy
Port 3320

Then you can use vscode to connect remote

zsh

First change the apt source, reference on aliyun mirror help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# code /etc/apt/sources.list
# on ubuntu 20.04
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

Install zsh

1
2
sudo apt-get install zsh
chsh -s /bin/zsh # change as default

Install oh-my-zsh

1
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

conda

Before use, make conda useful for your shell env:

1
conda init zsh
1
2
3
4
# copy base env
conda create -n <new name> --clone <base name>
# activate your current env
conda activate <new name>

Change conda package source, reference on tsinghua mirror help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# touch /$USER/.condarc

channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

pip

1
$ pip config set global.index-url https://mirrors.sustech.edu.cn/pypi/simple

git

Initialize the configuration when first using git

1
2
git config --global user.name "XXX"
git config --global user.email "XXX@email.com"

github

Add ssh key to github, ref github page

Check the directory listing to see if you already have a public SSH key.

1
$ ls -al ~/.ssh

Generating a new SSH key

1
$ ssh-keygen -t ed25519 -C "your_email@example.com"

Copy the public key, and add it to github website

1
$ cat ~/.ssh/id_ed25519.pub

Test ssh connection

1
2
$ ssh -T git@github.com
# Attempts to ssh to GitHub

cheatsheet for ubuntu

This part records some important tools and command lines.

nginx

nginx简单配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name your_domain_name; ##,绑定域名
rewrite ^(.*)$ https://$host$1; # 将所有HTTP请求通过rewrite指令重定向到HTTPS
index /root/guotongwu.github.io/index.html; # 默认文件
location / {
proxy_pass http://127.0.0.1:4000; ## 指定端口号 4000
}
error_page 404 /root/guotongwu.github.io/404.html;
}

#以下属性中,以ssl开头的属性表示与证书配置有关。
server {
listen 443 ssl;
#配置HTTPS的默认访问端口为443。
#如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
#如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
server_name your_domain_name; #需要将yourdomain替换成证书绑定的域名。
root html;
index /root/guotongwu.github.io/index.html; #默认文件
ssl_certificate pem_path; #需要将cert-file-name.pem替换成已上传的证书文件的名称。
ssl_certificate_key key_path; #需要将cert-file-name.key替换成已上传的证书私钥文件的名称。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
ssl_prefer_server_ciphers on;
location / {
root html; #Web网站程序存放目录。
index /root/guotongwu.github.io/index.html; #默认文件
proxy_pass http://127.0.0.1:4000; ## 指定端口号 4000
}
}

nginx重启服务

1
nginx -s reload

tmux

参考:阮一峰网络日志

基本操作

1
2
3
4
5
6
7
8
9
10
11
# 新建会话
tmux new -s <session name>
# 分离会话
tmux detach # Ctrl+b, d
# 查看会话
tmux attach -t <session name>
# 结束会话
tmux kill-session -t <session name>
# 重命名
tmux rename-session -t <session number> <new name>
Ctrl+b, $ # 重命名当前会话

快捷键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Ctrl+b %:划分左右两个窗格。
Ctrl+b ":划分上下两个窗格。
Ctrl+b <arrow key>:光标切换到其他窗格。<arrow key>是指向要切换到的窗格的方向键,比如切换到下方窗格,就按方向键↓。
Ctrl+b ;:光标切换到上一个窗格。
Ctrl+b o:光标切换到下一个窗格。
Ctrl+b {:当前窗格与上一个窗格交换位置。
Ctrl+b }:当前窗格与下一个窗格交换位置。
Ctrl+b Ctrl+o:所有窗格向前移动一个位置,第一个窗格变成最后一个窗格。
Ctrl+b Alt+o:所有窗格向后移动一个位置,最后一个窗格变成第一个窗格。
Ctrl+b x:关闭当前窗格。
Ctrl+b !:将当前窗格拆分为一个独立窗口。
Ctrl+b z:当前窗格全屏显示,再使用一次会变回原来大小。
Ctrl+b Ctrl+<arrow key>:按箭头方向调整窗格大小。
Ctrl+b q:显示窗格编号。

窗口操作

1
2
3
4
5
6
7
8
9
10
11
# 新建窗口
tmux new-window -n <window name>
Ctrl+b c
# 切换窗口
tmux select-window -t <window name>
Ctrl+b p # 上一个
Ctrl+b n # 下一个
Ctrl+b w # 列表选择
# 重命名
tmux rename-window <new-name>
Ctrl+b , # 窗口重命名

tmux设置

1
2
# 打开鼠标
set-option -g mouse on

ubuntu desktop

默认使用TCP协议,需要放行5901端口,参考腾讯云文档

1
2
3
apt-get update
apt install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal ubuntu-desktop
apt-get install tightvncserver # ubuntu 20.04

启动服务,设置密码

1
vncserver

更改配置文件:~/.vnc/xstartup

1
2
3
4
5
#!/bin/sh
export XKL_XMODMAP_DISABLE=1
export XDG_CURRENT_DESKTOP="GNOME-Flashback:GNOME"
export XDG_MENU_PREFIX="gnome-flashback-"
gnome-session --session=gnome-flashback-metacity --disable-acceleration-check &

重启

1
2
vncserver -kill :1 #杀掉原桌面进程,输入命令(其中的:1是桌面号)
vncserver -geometry 1280x720 :1 #生成新的会话