查看vm列表
$ multipass list
Name State IPv4 Image
vm01 Running 192.168.64.2 Ubuntu 18.04 LTS
使用multipass遠程登錄vm
$ multipass shell vm01
ubuntu@vm01:~$
查看版本
# 內核版本
$ uname -a
Linux vm01 4.15.0-108-generic #109-Ubuntu SMP Fri Jun 19 11:33:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# 系統版本法一
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
# 系統版本法二
$ cat /proc/version
Linux version 4.15.0-108-generic (buildd@lcy01-amd64-013) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #109-Ubuntu SMP Fri Jun 19 11:33:10 UTC 2020
# 系統版本法三
$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l
替換鏡像源至阿里云
CN區(qū)可用鏡像列表: http://mirrors.ubuntu.com/CN.txt
$ sudo bash -c "cp /etc/apt/sources.list /etc/apt/sources.list.backup && cat > /etc/apt/sources.list" <<EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
更新系統(為什么加參數-y,就是不想太多人打擾朕)
$ sudo bash -c "apt-get update -y && apt-get upgrade -y && apt-get autoremove -y"
創(chuàng)建live組和賬號,開啟live賬號sudo無密碼(注意外層需用單引號)
$ sudo bash -c 'name="live" \
&& groupadd $name \
&& useradd -s /bin/bash -g $name $name \
&& mkdir -p /home/$name \
&& chown -R $name:$name /home/$name \
&& echo "$name ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers'
為live賬號添加ssh免密碼登錄(注意外層需用單引號)
$ sudo -iu live bash -c 'ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -N ""'
$ sudo -iu live bash -c 'cat >> $HOME/.ssh/authorized_keys && chmod 600 $HOME/.ssh/authorized_keys' <<EOF
ssh-rsa xxxxxxxx
EOF
修改ssh防斷線,關閉root登錄、ssh賬號密碼登錄
$ sudo sed -ri 's/^#?ClientAliveInterval [0-9]+$/ClientAliveInterval 15/g' /etc/ssh/sshd_config
$ sudo sed -ri 's/^#?ClientAliveCountMax [0-9]+$/ClientAliveCountMax 45/g' /etc/ssh/sshd_config
$ sudo sed -ri 's/^#?UseDNS.*$/UseDNS no/g' /etc/ssh/sshd_config
$ sudo sed -ri 's/^#?PermitRootLogin.*$/PermitRootLogin no/g' /etc/ssh/sshd_config
$ sudo sed -ri 's/^#?PasswordAuthentication.*$/PasswordAuthentication no/g' /etc/ssh/sshd_config
$ sudo /etc/init.d/ssh restart
為live賬號添加各種別名
$ sudo -iu live bash -c 'cat >> /home/live/.bash_profile' <<EOF
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
EOF
$ sudo -iu live bash -c 'cat >> /home/live/.bash_aliases' <<EOF
alias ..='cd ..'
alias ...='cd ../..'
alias cp='cp -i'
alias l='ls -Glah --color=auto'
alias ll='ls -Glah'
alias lt='ls -Glaht'
alias lh='ls -Glah'
alias mv='mv -i'
alias rm='rm -i'
alias tf='tail -f'
alias psg='ps aux|grep'
alias hg='history|grep'
EOF
在本機添加別名快速ssh登錄虛擬機vm01(在本機shell執(zhí)行)
# 找到vm01的ip地址
$ multipass list
Name State IPv4 Image
vm01 Running 192.168.64.2 Ubuntu 18.04 LTS
# 添加alias別名
$ echo "alias vm01='ssh live@192.168.64.2'" >> ~/.bash_aliases
# 立即生效(否則需要重新開shell窗口)
$ source ~/.bash_profile