服务器装机

服务器停电后,重启顺序

1、将外置移动硬盘或者机械硬盘拔出;
2、进入系统,选择内核版本139的系统;
3、重新插入拔出的硬盘;
4、检查显卡情况;

watch -n 1 nvidia-smi

5、启动VNCserver;

vncserver

每运行一次,多开一个端口
6、查看其他保存情况。

ubuntu 新建用户

  1. 切换至root
sudo su
  1. 添加新用户
sudo useradd -r -m -s /bin/bash lhz

创建用户,自动在/home/目录下创建相应的文件夹

sudo adduser lhz
  1. 配置新用户密码
sudo passwd lhz
  1. 给新添加的用户增加ROOT权限
sudo vi /etc/sudoers

然后添加:
用户名 ALL=(ALL:ALL) ALL

linux中用adduser和useradd命令创建用户

  1. 删除用户
    一开始报错,后来关闭进程后删掉用户
(base) lhzouc@s-desktop:~$ sudo userdel -r  public
userdel: user public is currently used by process 18219

(base) lhzouc@s-desktop:~$ sudo su
root@s-desktop:/home/lhzouc# userdel public
userdel: user public is currently used by process 18219
root@s-desktop:/home/lhzouc# kill -9 18219
root@s-desktop:/home/lhzouc# userdel public
userdel: user public is currently used by process 18250
root@s-desktop:/home/lhzouc# kill -9 18250
root@s-desktop:/home/lhzouc# userdel public
root@s-desktop:/home/lhzouc# exit
(base) lhzouc@s-desktop:~$ su public
No passwd entry for user 'public'
(base) lhzouc@s-desktop:~$ sudo public
sudo: public: command not found
  1. 查看所有用户信息
sudo vim /etc/passwd

7.切换用户/root

su lhz

8.查看当前用户所在的用户组

id -Gn
groups
  1. 将用户加入到某个用户组/sudo组
sudo adduser lhz scw4750
sudo adduser lhz sudo

或者

su scw4750
groups lhz
sudo usermod -G sudo lhz
groups lhz
  1. 修改文件夹所有者
chown -R scw4750:lhz liuhz/
  1. 删除用户
sudo deluser lhz --remove-home

显卡丢失问题解决

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

解决参考

sudo apt-get update
sudo apt-get install --no-install-recommends nvidia-384 libcuda1-384 nvidia-opencl-icd-384
sudo reboot

由于我们服务器驱动为390.87,因此按照如下指令

sudo apt-get update
sudo apt-get install --no-install-recommends nvidia-390 libcuda1-390 nvidia-opencl-icd-390
sudo reboot

如果重启后光标闪烁,不进入系统,则切换内核版本

NVIDIA显卡报错并重装

NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA driver. 
Make sure that the latest NVIDIA driver is installed and running.
  1. 卸载原驱动
sudo apt-get --purge remove nvidia*
  1. 更新源
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
  1. 查看当前系统推荐你安装的驱动版本
ubuntu-drivers devices

4.安装

sudo apt-get install nvidia-384

5.查看是否成功

nvidia-smi

Anaconda 虚拟环境

1、环境配置
下载anaconda.sh文件并运行脚本

sudo sh Anaconda3-5.3.0-Linux-x86_64.sh 

... # 多次按空格读完license。
Do you accept the license terms? [yes|no]
[no] >>> yes # 输入yes

[/home/andrew/anaconda3] >>> /usr/local/anaconda3 # 输入自定义目录

Do you wish the installer to initialize Anaconda3
in your /home/andrew/.bashrc ? [yes|no]
[no] >>> # 选择yes

进入bashrc文件中

ouc@ouc:~/Desktop$ gedit ~/.bashrc

添加需要补充的Anaconda环境

# added by Miniconda3 installer
export PATH="/home/ouc/miniconda3/bin:$PATH"

#export PATH  ="/home/ouc/anaconda2/bin:$PATH"

#export PATH="/usr/bin/python2.7:$PATH"

最后修改完成后执行语句

ouc@ouc:~/Desktop$ source ~/.bashrc

2、查看当前用户安装的虚拟环境

conda create -n envname python=3.7

lhz@s-desktop:/media/disk/liuhongzhi/EncripGAN/EncripGAN0215$ conda info -e
WARNING: The conda.compat module is deprecated and will be removed in a future release.
# conda environments:
#
py36tf14                 /home/lhz/.conda/envs/py36tf14
py36tf18                 /home/lhz/.conda/envs/py36tf18
py36torch04              /home/lhz/.conda/envs/py36torch04
sphere                   /home/lhz/.conda/envs/sphere
base                  *  /media/disk/liuhongzhi/anaconda3
con1                     /media/disk/liuhongzhi/anaconda3/envs/con1
encrip                   /media/disk/liuhongzhi/anaconda3/envs/encrip

lhz@s-desktop:/media/disk/liuhongzhi/EncripGAN/EncripGAN0215$ conda activate encrip
(encrip) lhz@s-desktop:/media/disk/liuhongzhi/EncripGAN/EncripGAN0215$
(encrip) lhz@s-desktop:/media/disk/liuhongzhi/EncripGAN/EncripGAN0215$ conda deactivate

3、添加清华镜像源,修改~/.condarc

首先在终端中运行

conda config --set show_channel_urls yes

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/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
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
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

tuna添加清华镜像源

lhz@scw4750:~$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
lhz@scw4750:~$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

4、conda: command not found 问题

lhz@scw4750:~$ vi ~/.bashrc

最后一行加上

export PATH=$PATH:/media/scw4750/liuhz/anaconda3/bin

保存更改运行

source ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/media/scw4750/liuhz/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/media/scw4750/liuhz/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/media/scw4750/liuhz/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/media/scw4750/liuhz/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
export PATH=$PATH:/media/scw4750/liuhz/anaconda3/bin
                                                       

5.显示conda信息

lhz@scw4750:~$ conda info

     active environment : None
            shell level : 0
       user config file : /home/lhz/.condarc
 populated config files : /home/lhz/.condarc
          conda version : 4.7.12
    conda-build version : 3.18.9
         python version : 3.7.4.final.0
       virtual packages : __cuda=10.1
       base environment : /media/scw4750/liuhz/anaconda3  (read only)
           channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
          package cache : /media/scw4750/liuhz/anaconda3/pkgs
                          /home/lhz/.conda/pkgs
       envs directories : /media/scw4750/liuhz/anaconda3/envs
                          /home/lhz/.conda/envs
               platform : linux-64
             user-agent : conda/4.7.12 requests/2.22.0 CPython/3.7.4 Linux/4.15.0-72-generic ubuntu/16.04.6 glibc/2.23
                UID:GID : 1002:1002
             netrc file : None
           offline mode : False

如果保存环境到/home/lhz/.conda/envs路径时,需要修改~/.condarc中的pkgs_dirs和envs_dirs
修改conda环境和缓存默认路径

lhz@scw4750:~$ vi ~/.condarc

auto_activate_base: false
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/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
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
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pkgs_dirs:
  - /media/scw4750/liuhz/anaconda3/pkgs
  - /home/lhz/.conda/pkgs
envs_dirs:
  - /media/scw4750/liuhz/anaconda3/envs
  - /home/lhz/.conda/envs
  1. 让Anaconda不再自动进入虚拟环境base
conda config --set auto_activate_base false
  1. 卸载Anaconda环境
# 卸载包
# 删除指定环境中的指定包
conda remove --name snowflakes biopython

# 卸载环境
# --all参数表示移除环境中的所有软件包,即删除整个环境
conda remove --name envname --all

Ubuntu 命令行方式安装、启动、设置和链接 teamviewer

1、下载软件
wget http://download.teamviewer.com/download/teamviewer_i386.deb

2、更新依赖及安装指令
sudo apt-get update
出现错误:sudo apt-get update -f
sudo dpkg -i teamviewer_i386.deb

3、停止teamviewer
sudo teamviewer –daemon stop

4、修改配置文件
打开文件:vim /opt/teamviewer/config/global.conf
增加内容:
EulaAccepted = 1
EulaAcceptedRevision = 6

5、启动teamviewer
sudo teamviewer –daemon start

6、设置登录密码
sudo teamviewer –passwd 123456

7、获取TeamviewerID
teamviewer –info print id

teamviewer –info

8、teamviewer启动失败

s@s-desktop:/home/lhz/.vnc$ teamviewer –daemon start
Init...
CheckCPU: SSE2 support: yes
XRandRWait: No value set. Using default.
XRandRWait: Started by user.
Checking setup...
Launching TeamViewer ...
Launching TeamViewer GUI ...

解决办法:

s@s-desktop:/home/lhz/.vnc$ htop
s@s-desktop:/home/lhz/.vnc$ sudo kill -9 1480
s@s-desktop:/home/lhz/.vnc$ teamviewer –daemon start

Init...
CheckCPU: SSE2 support: yes
XRandRWait: No value set. Using default.
XRandRWait: Started by user.
Checking setup...
Launching TeamViewer ...
Launching TeamViewer GUI ...
s@s-desktop:/home/lhz/.vnc$ teamviewer &
[1] 22653
s@s-desktop:/home/lhz/.vnc$ 
Init...
CheckCPU: SSE2 support: yes
XRandRWait: No value set. Using default.
XRandRWait: Started by user.
Checking setup...
Launching TeamViewer ...
Launching TeamViewer GUI ...

Ubuntu 常用软件包安装

  • Ristretto image viewer
# Install Howto
# Update the package index:
sudo apt-get update
# Install ristretto deb package:
sudo apt-get install ristretto

参考链接 ristretto_0.8.0-1_amd64.deb

VNC常见问题

Ubuntu VNCserver

  1. 打开VNC,查看端口可用
vncserver -list

查看vncserver已用端口的命令

(base) lhz@s-desktop:~$ ps -ef | grep vnc
  1. 删除端口
vncserver -kill :3
  1. VNC 黑屏和accept clipboard from viewer
    连接上VNC后桌面黑屏什么都没有,又比如连接上后桌面左上角有3个选择框关不掉,选择内容为 Accept clipboard from viewer Send clipboard to viewer Send premary seletion to viewer

解决方法:
(1). 编辑vnc的配置文件,文件位置为: /root/.vnc/xstartup

vi  /root/.vnc/xstartup 

(2).修改部分内容

#!/bin/sh

# Uncomment the following two lines for normal desktop:
#unset SESSION_MANAGER
#exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &

改为

#!/bin/sh

# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
#xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#twm &
gnome-session &

4.修改分辨率
查看vnc进程:

ps -ef | grep vnc

该命令可以列出当前系统上所有用户vnc界面的端口号,分辨率等信息。
我们要用到的只是端口号。

杀掉vnc端口对应的进程

vncserver -kill :5

我的端口号是5

启动vnc服务器
vncserver :5
启动后发现,与自己的电脑桌面相比界面比较小,点击全屏后,原来多余的部分都变成黑色的了。
通过上面说的查看vnc进程命令后发现有个信息-geometry 1024x768,这说明vnc的默认分辨率是1024x768,而我的笔记本分辨率是1366x768。所以要全屏,只需要修改分辨率即可。

修改vnc分辨率
修改分辨率的命令为:

vncserver -geometry 1920x1080 :5

在这里要注意一点,在vnc server运行期间,使用该命令是会报错的:‘A VNC server is already running as :5’

所以,在修改vnc分辨率之前,我们需要先将vnc server关闭。用上面说过的-kill命令杀掉vnc端口对应的进程即可。

或者可以采用以下方法:

lhz@s-desktop:~$ sudo vi /usr/bin/vncserver

#
# Global variables.  You may want to configure some of these for your site.
#

$geometry = "1920x1080";      #可以修改分辨率
$depth = 16;

然后再运行命令修改分辨率即可。之后登录vnc界面就可以看到界面的大小已经改变了。
参考链接 修改VNC的分辨率

  1. 登录VNC桌面时,发现在终端中使用tab补全相关命令时,会不起作用
    方法是在远程桌面中进行设置,路径是:打开菜单—设置—窗口管理器,Settings-Window manager-Keyboard-Switch window-clear,由此可以在终端进行指令补全
    参考链接 学习ubuntu远程桌面(一):配置远程桌面

阿里云服务器反向端口转发

外网服务器 IP: remote-host
外网用户名: ouc03

指令含义

-f 后台执行ssh指令
-C 允许压缩数据
-N 不执行远程指令
-R 将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口
-L 将本地机(客户机)的某个端口转发到远端指定机器的指定端口
-p 指定远程主机的端口
  1. 在内网服务器上操作,建立内网到外网的反向代理,并检验是否已经启动

7275 为阿里云服务器绑定端口号(需要在阿里云工作台配置安全组规则开启)
22为 SSH 连接固定端口号(本地实验室服务器端口)

lhz@scw4750:~$ ssh -fCNR 7275:localhost:22 ouc03@remote-host
lhz@scw4750:~$ ps aux | grep ssh
root      1268  0.0  0.0  65512  6304 ?        Ss   1月03   0:00 /usr/sbin/sshd -D
root      1440  0.0  0.0  94928  6988 ?        Ss   08:55   0:00 sshd: videoudsd [priv]
videoud+  1544  0.0  0.0  94928  4456 ?        S    08:55   0:01 sshd: videoudsd@pts/5
root      6961  0.0  0.0  94928  6896 ?        Ss   1月16   0:00 sshd: lhz [priv]
lhz       7071  0.0  0.0  94928  3456 ?        S    1月16   0:10 sshd: lhz@pts/1
root      8629  0.0  0.0  94928  6840 ?        Ss   1月16   0:00 sshd: lhz [priv]
lhz       8666  0.0  0.0  94928  3424 ?        S    1月16   0:05 sshd: lhz@pts/2
root     16107  0.0  0.0  94928  6916 ?        Ss   13:16   0:00 sshd: lhz [priv]
lhz      16216  0.0  0.0  94928  3220 ?        S    13:16   0:00 sshd: lhz@pts/9
root     17142  0.0  0.0  95008  7124 ?        Ss   10:20   0:00 sshd: lhz [priv]
lhz      17201  0.0  0.0  95296  4304 ?        S    10:20   0:00 sshd: lhz@notty
lhz      20787  0.0  0.0  45212  2864 ?        Ss   13:41   0:00 ssh -fCNR 7275:localhost:22 [email protected]
root     21230  0.0  0.0  94928  6960 ?        Ss   13:44   0:00 sshd: lhz [priv]
lhz      21289  0.0  0.0  94928  3592 ?        S    13:44   0:00 sshd: lhz@pts/8
root     23343  0.0  0.0  94928  7020 ?        Ss   13:54   0:00 sshd: lhz [priv]
lhz      23389  0.0  0.0  94928  3096 ?        S    13:54   0:00 sshd: lhz@pts/10
lhz      24981  0.0  0.0  45212   780 ?        Ss   14:03   0:00 ssh -fCNR 7275:localhost:22 [email protected]
lhz      25019  0.0  0.0  21292   972 pts/9    S+   14:03   0:00 grep --color=auto ssh

(base) [intern2@172-10-60-160 ssh]$ ssh -o serveraliveinterval=60 -fCNR 7257:localhost:22 airia160@remote-host
  1. 在外网服务器上建立正向代理,用来做转发。
    7274端口为本地转发端口,负责和外网进行通信,并将数据转发的7275这个端口,实现了可以从其他机器访问的功能,并从7274端口登录
ouc03@iZuf6bd4deudifh44e6p12Z:~$ ssh -fCNL *:7274:localhost:7275 localhost
ouc03@iZuf6bd4deudifh44e6p12Z:~$ ps aux | grep ssh
root     19700  0.0  0.2  59420  5920 ?        Ss   13:22   0:00 /usr/sbin/sshd -D
root     20035  0.0  0.3  93460  7016 ?        Ss   13:28   0:00 sshd: ouc03 [priv]
ouc03    20065  0.0  0.1  93460  3300 ?        S    13:28   0:00 sshd: ouc03@pts/0
root     20082  0.0  0.3  93428  6892 ?        Ss   13:34   0:00 sshd: ouc02 [priv]
ouc02    20113  0.0  0.2  93836  5068 ?        S    13:34   0:00 sshd: ouc02
root     20114  0.0  0.3  93428  7112 ?        Ss   13:34   0:00 sshd: ouc02 [priv]
ouc02    20137  0.0  0.1  93428  3492 ?        S    13:34   0:00 sshd: ouc02@pts/1
root     20164  0.0  0.3  93460  6736 ?        Ss   13:35   0:00 sshd: ouc02 [priv]
ouc02    20166  0.0  0.0  37380   740 ?        Ss   13:35   0:00 ssh -fCNL *:7279:localhost:7278 localhost
ouc02    20188  0.0  0.2  93636  4952 ?        S    13:35   0:00 sshd: ouc02
ouc02    20225  0.0  0.2  36680  5276 pts/1    S+   13:36   0:00 ssh -p7278 [email protected]
root     20226  0.0  0.3  93540  7132 ?        Ss   13:38   0:00 sshd: ouc02 [priv]
ouc02    20249  0.0  0.1  93540  3464 ?        S    13:38   0:00 sshd: ouc02@pts/2
ouc02    20269  0.0  0.2  36684  4452 pts/2    S+   13:39   0:00 ssh -p7278 [email protected]
root     20338  0.0  0.3  93432  6856 ?        Ss   13:42   0:00 sshd: ouc02 [priv]
ouc02    20361  0.0  0.2  93704  4656 ?        S    13:42   0:00 sshd: ouc02
root     20362  0.0  0.3  93432  7064 ?        Ss   13:43   0:00 sshd: ouc02 [priv]
ouc02    20386  0.0  0.1  93432  3232 ?        S    13:43   0:00 sshd: ouc02@pts/3
root     20402  0.0  0.3  93460  6708 ?        Ss   13:43   0:00 sshd: ouc03 [priv]
ouc03    20404  0.0  0.1  37380  2444 ?        Ss   13:43   0:00 ssh -fCNL *:7274:localhost:7275 localhost
ouc03    20426  0.0  0.2  93636  4924 ?        S    13:43   0:00 sshd: ouc03
ouc02    20429  0.0  0.2  36684  4412 pts/3    S+   13:43   0:00 ssh -p7278 [email protected]
ouc03    20430  0.0  0.2  36676  5484 pts/0    S+   13:44   0:00 ssh -p7275 [email protected]
root     20506  0.0  0.3  93460  6932 ?        Ss   13:51   0:00 sshd: ouc03 [priv]
ouc03    20529  0.0  0.1  93460  3288 ?        S    13:51   0:00 sshd: ouc03@pts/5
root     20618  0.0  0.3  93432  6856 ?        Ss   13:52   0:00 sshd: ouc02 [priv]
ouc02    20641  0.0  0.2  93704  4644 ?        S    13:52   0:00 sshd: ouc02
root     20777  0.0  0.3  93432  7064 ?        Ss   14:03   0:00 sshd: ouc02 [priv]
ouc02    20800  0.0  0.2  93704  4900 ?        S    14:03   0:00 sshd: ouc02
root     20801  0.0  0.3  93428  6896 ?        Ss   14:03   0:00 sshd: ouc03 [priv]
ouc03    20824  0.0  0.2  93704  4680 ?        S    14:03   0:00 sshd: ouc03
root     20828  0.0  0.3  93460  7028 ?        Ss   14:05   0:00 sshd: ouc03 [priv]
ouc03    20851  0.0  0.1  93460  3300 ?        S    14:05   0:00 sshd: ouc03@pts/4
root     20867  0.0  0.3  93460  6600 ?        Ss   14:05   0:00 sshd: ouc03 [priv]
ouc03    20869  0.0  0.0  37380   732 ?        Ss   14:05   0:00 ssh -fCNL *:7274:localhost:7275 localhost
ouc03    20891  0.0  0.2  93636  4744 ?        S    14:05   0:00 sshd: ouc03
ouc03    20893  0.0  0.0  14220   928 pts/4    S+   14:06   0:00 grep --color=auto ssh
  1. 用SSH连接
ouc03@iZuf6bd4deudifh44e6p12Z:~$ ssh -p7274 lhz@remote-host
lhz@remote-host's password: 
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-72-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

95 packages can be updated.
0 updates are security updates.

New release '18.04.3 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

*** System restart required ***
Last login: Fri Jan 17 14:09:34 2020 from 127.0.0.1

  1. 建立稳定隧道
lhz@scw4750:~$ autossh -M 7273 -fCNR 7275:localhost:22 -o ServerAliveInterval=60 -o TCPKeepAlive=yes ouc03@remote-host

利用ssh反向代理以及autossh实现从外网连接内网服务器

autossh的参数与ssh的参数是一致的,但是不同的是,在隧道断开的时候,autossh会自动重新连接而ssh不会。另外不同的是需要指出的-M参数,这个参数指定一个端口,这个端口是外网的B机器用来接收内网A机器的信息,如果隧道不正常而返回给A机器让他实现重新连接。

  1. 查看端口使用情况
ouc03@iZuf6bd4deudifh44e6p12Z:~$ netstat -tln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:7273            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:7274            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:7278            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:7279            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp6       0      0 :::7274                 :::*                    LISTEN     
tcp6       0      0 :::7279                 :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     

  1. 配置开机自启
    Ubuntu14.04以下写入 /etc/rc.local 的 exit 0 之前
su - lab-user -c autossh -M 7274 -fCNR 7275:localhost:22 -o ServerAliveInterval=60 -o TCPKeepAlive=yes ouc03@remote-host

在 Ubuntu16.04+ 中实现开机自启要复杂一点

sudo vim /lib/systemd/system/autossh.service
[Unit]
Description=Start autossh to aliyun at startup.
After=network.target
[Service]
User=lab-user
Group=lab-user
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 7281 -CNR 7280:localhost:22 -o ServerAliveInterval=60 -o TCPKeepAlive=yes remote-user@remote-host
[Install]
WantedBy=multi-user.target

注意:
a. 上面 autossh 命令没有 -f 选项,因为 systemd 不支持,"AUTOSSH_GATETIME=0" 已经有这个作用了。
b. 上面的 User、Group 字段对应 lab-user 用户的用户名、用户组,意思是使用 lab-user 这个用户执行上面的 service。如果不加 User、Group 字段,上面的 service 在开机时会由 root 用户执行,所以 root 用户应该能够免密码登录 remote-host 才行,否则会报 Host key verification failed 的错误。

[Unit]
Description=Start autossh to aliyun at startup.
After=network.target
[Service]
User=lhz
Group=lhz
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -M 7273 -CNR 7275:localhost:22 -o ServerAliveInterval=60 -o TCPKeepAlive=yes ouc03@remote-host
[Install]
WantedBy=multi-user.target

然后运行:

sudo systemctl daemon-reload
sudo systemctl enable autossh.service
sudo systemctl start autossh.service

利用 sudo systemctl status autossh.service 检查运行状态。

  1. 将服务器某些端口内容转发到本地端口
    例如,服务器正在用 tensorboard 在 6006 端口可视化 loss,然后将该端口转发到阿里云服务器的 5000 端口:
# 在实验室服务器执行
$ ssh -CNR *:5000:localhost:6006 remote-user@remote-host

在浏览器打开 remote-host:5000 即可访问。

  1. 将本地文件传到远程服务器
    (1) 不需要连代理服务器
scp filename user@HostName:route
scp -r filedir user@HostName:route

(2)连接代理服务器

scp filename Host:route
scp -r filedir Host:route
  1. 将远程服务器文件传到本地
    (1) 不需要连代理服务器
scp user@HostName:route filename 
scp -r  user@HostName:route filedir

(2)连接代理服务器

scp  Host:route filename
scp  -r  Host:route  filedir

hzq@hzq:~/Medical$ scp -r -P 7256 intern2@remote-host:/data/jay/lhz/Methods/ScaledYOLOv4/ScaledYOLOv4/runs/exp3_yolov4-csp/*.jpg ./
  1. 注意事项
  • ssh认第一个输入指令,autossh前清掉之前的ssh命令(直到连接不上),否则连接会断
  • 可以运行长时间交互命令,比如ifstat等
  • 目前可以连接27和38服务器
(base) lhzouc@s-desktop:~$ autossh -M 7260 -fCNR 7261:localhost:22 -o ServerAliveInterval=60 -o TCPKeepAlive=yes ouc03@remote-host

参考链接
利用ssh反向代理以及autossh实现从外网连接内网服务器
利用阿里云服务器配置反向端口转发 <实验室内部论坛>

VSCode 远程开发含免密操作

  1. 在本地生成密钥对
ssh-keygen -t rsa -C "your email"

# 或者 
ssh-keygen -t rsa -b 4096

在远程服务器进行相同的操作

ssh-keygen -t rsa

2.将本地公钥保存到远程服务器上,让远程机器的authorized_keys中包含公钥内容,用于免密操作

# 本地
scp ~/.ssh/id_rsa.pub ouc03@remote-host:/home/authorized_keys

# 远程服务器
cd ~/.ssh/
scp ouc03@remote-host:/home/authorized_keys ./
chmod 600 ~/.ssh/authorized_keys
# 可选操作
sudo service ssh restart
  1. VSCode 配置

选择远程资源资源管理器(电脑带箭头)->配置(configure 齿轮形状)
选择横条里选择:/home/henry/.ssh/config

Host lab
    HostName XX.XXX.XXX.XXX
    User henry
    Port 7251

Host 显示在连接选项中的名字,
HostName 你的ssh服务器的地址
User 你登录ssh时的用户名
即可完成配置

参考链接:
vscode remote ssh 远程开发免密登陆方法
VSCode 远程开发(带免密)

  1. 打开服务器端的html文件

连接远程服务器后,按 shift+ctrl+x 安装 HTML Preview 插件。
右键选择 Open preview 或者 ctrl + shift + v即可打开远程端的html文件。


html预览图

常见系统问题

1.Tab键无法补全命令
(1)修改bashrc

 sudo vi /etc/bash.bashrc 

将以下注释的部分,取消注释

1.  #enable bash completion in interactive shells  
2.  #if ! shopt -oq posix; then  
3.  #      if [-f  /usr/share/bash-completion/bash_completion ]; then  
4.  #          . /usr/share/bash-completion/bash_completion  
5.  #      elif [ -f /etc/bash_completion]; then  
6.  #           . /etc/bash_completion  
7.  #      fi  
8.  #fi  

改为

1.  #enable bash completion in interactive shells  
2.  if ! shopt -oq posix; then  
3.  if [-f  /usr/share/bash-completion/bash_completion ]; then  
4.  . /usr/share/bash-completion/bash_completion  
5.  elif [ -f /etc/bash_completion]; then  
6.  . /etc/bash_completion  
7.  fi  
8.  fi

最后 进行一步source更新

sudo source /etc/bash.bashrc

参考 Ubuntu系统Tab键不能自动补全问题解决

(2)在远程桌面中设置,打开菜单->设置->窗口管理器,或者在终端中输入xfwm4-settings打开(xfwm4就是xfce4 window manger的缩写)
选择键盘,可以看到窗口快捷键中动作一列有“切换同一应用程序的窗口”选项,将该选项的快捷键清除后关闭窗口即可。
到此,Tab键自动补全功能配置完成。

Window manager设置修改.png

参考 windows远程桌面ubuntu中Tab键不能补全命令的解决办法

  1. 在服务器上挂载U盘
    (1) 查看磁盘情况
liangju@fuwuqi-Z370-HD3:~$ sudo fdisk -l
[sudo] password for liangju: 

Disk /dev/sdb: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x99d0c6d2

(2) 查看块设备的文件系统,LABEL和UUID

liangju@fuwuqi-Z370-HD3:/media/hd$ sudo blkid
[sudo] password for liangju: 
/dev/sda4: UUID="02f0c150-9b43-4480-9b0c-d5e0e019de63" TYPE="ext4" PARTUUID="667f5f84-c211-4912-a737-194c65d8482a"
/dev/sda2: UUID="95300fdd-8a59-47c7-8ccf-41593ccd0751" TYPE="swap" PARTUUID="15774072-4714-4080-b85e-8791a10279e8"
/dev/loop0: TYPE="squashfs"
/dev/loop1: TYPE="squashfs"
/dev/loop2: TYPE="squashfs"
/dev/loop4: TYPE="squashfs"
/dev/loop5: TYPE="squashfs"
/dev/loop6: TYPE="squashfs"
/dev/loop7: TYPE="squashfs"
/dev/sda1: UUID="9cbe1c6c-1da2-4943-8396-41647fc1e8c3" TYPE="ext4" PARTUUID="aab40112-d765-45d1-be56-6455c9e5078d"
/dev/loop8: TYPE="squashfs"
/dev/loop9: TYPE="squashfs"
/dev/loop10: TYPE="squashfs"
/dev/loop11: TYPE="squashfs"
/dev/loop12: TYPE="squashfs"
/dev/loop13: TYPE="squashfs"
/dev/loop14: TYPE="squashfs"
/dev/loop15: TYPE="squashfs"
/dev/loop16: TYPE="squashfs"
/dev/sda3: PARTUUID="bcb440e8-a202-4529-8683-940e67c69a60"
/dev/sdb4: LABEL="Ubuntu 16.0" UUID="B4FE-5315" TYPE="vfat" PARTUUID="cad4ebea-04"

其中/dev/sdb4是我们需要挂载的U盘
(3) 新建挂载目录 /media/hd

liangju@fuwuqi-Z370-HD3:~$ sudo mkdir /media/hd

(4) U盘挂载到指定路径

liangju@fuwuqi-Z370-HD3:/media/hd$ sudo mount /dev/sdb4 /media/hd

(5) 查看U盘内容

liangju@fuwuqi-Z370-HD3:/media/hd$ ls
 boot       'FW150US 4.0'                      pics
 casper      install                           pool
 compare     isolinux                          preseed
 config      K40                               res
 dists       movie                            'RICOH SP 325SNw_windows???????.pdf'
 EFI         outpainting_1017_with_colorLoss  'System Volume Information'
 FOUND.000   outpainting_car_slice16_Alpha     uname
 FOUND.001   PEN_3                             XMPSetup_5.3.1.6065-VZqqpcmgr.exe

(6) 卸载U盘

liangju@fuwuqi-Z370-HD3:/media$ sudo umount /media/hd

(7) 设备繁忙

liangju@fuwuqi-Z370-HD3:/media/hd$ sudo umount /media/hd
umount: /media/hd: target is busy.

解决方式

liangju@fuwuqi-Z370-HD3:/media/hd$ sudo umount /media/hd
[sudo] password for liangju: 
umount: /media/hd: target is busy.
liangju@fuwuqi-Z370-HD3:/media/hd$ cd ..
liangju@fuwuqi-Z370-HD3:/media$ fuser -km hd
liangju@fuwuqi-Z370-HD3:/media$ sudo umount /media/hd
liangju@fuwuqi-Z370-HD3:/media$ 

你可能感兴趣的:(服务器装机)