这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

基本配置

Ubuntu Server安装后的基本配置工作

1 - 设置更新源

设置Ubuntu Server的更新源

设置更新源

在进行任何更新和软件安装前,建议先设置更新源,以保证速度。

如果服务器在国内,则可以考虑设置apt源为国内代理,这样速度要好很多。

首先备份源列表:

sudo cp /etc/apt/sources.list /etc/apt/sources.list_original
sudo vi /etc/apt/sources.list

然后修改 /etc/apt/sources.list 文件.

ubuntu 23.04

阿里云源 :

deb http://mirrors.aliyun.com/ubuntu/ lunar main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ lunar main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ lunar-security main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ lunar-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ lunar-updates main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ lunar-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ lunar-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ lunar-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ lunar-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ lunar-backports main restricted universe multiverse

中科大源:

deb https://mirrors.ustc.edu.cn/ubuntu/ lunar main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ lunar main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ lunar-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ lunar-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ lunar-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ lunar-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ lunar-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ lunar-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ lunar-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ lunar-proposed main restricted universe multiverse

ubuntu 22.04

阿里云源 (不知道为什么特别慢):

deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

中科大源(非常快):

deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

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

中科大镜像源:

deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

对于腾讯云/阿里云上的服务器,默认都配置好了腾讯云和阿里云的源,直接用即可,速度超好。

偶尔会遇到阿里云或者中科大源不可用的情况(以前没遇到过,最近很频繁),可以切换其他源试试。

网易163源:

deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse
# deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse
# deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse

清华源:

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-proposed main restricted universe multiverse

更新系统

先更新各种软件到最新,简单的 apt upgrade 搞定:

sudo apt update
sudo apt upgrade

取消PPA仓库

当添加太多的 PPA 仓库之后,apt update 的速度就会慢很多。

考虑到大多数软件不会经常更新,而且我们也没有立即更新的迫切需求,因此建议取消这些 PPA 仓库。

终端操作

PPA仓库存储位置:

$ cd /etc/apt/sources.list.d
$ ls -l
git-core-ubuntu-ppa-focal.list

打开具体要取消的PPA仓库,如上面的git的ppa仓库文件,注释掉相关的内容。

不要直接删,以后如果需要更新,可以手工取消注释而不用重新找ppa仓库地址再添加。

UI操作

“开始菜单” -> “系统管理” -> “软件源” -> “PPA”, 将不需要及时更新的软件的 PPA 取消。

2 - 修改hostname

修改Ubuntu Server的hostname

背景

在 esxi 等虚拟平台上复制虚拟机时,就会遇到 hostname 重复的问题,这时最好是能在虚拟机复制之后永久性的修改 hostname 。

查看当前 hostname :

hostname

ubuntu 20.04

可以通过 hostnamectl 来修改:

sudo hostnamectl set-hostname newNameHere

完成后再额外修改一下 hosts 文件中的 hostname:

sudo nano /etc/hosts

完成后重启即可:

sudo reboot

参考资料

3 - SSH登录

使用新用户SSH登录服务器

安装openssh(可选)

安装ssh软件,以便从外部直接ssh进来,而不用在服务器上操作。通常选择安装openssh,有些服务器默认有安装openssh,如果服务器默认没有安装ssh软件,则:

sudo apt-get install openssh-server

使用密码远程登录

执行:

ssh sky@ubuntu.server.ip

输入密码即可。

使用密钥远程登录

为了进一步方便使用,不必每次输入密码, 还可以通过authorized_keys 的方式来自动登录。

上传本机的 .ssh/id_isa.pub 文件到ubuntu server服务器端:

scp ~/.ssh/id_rsa.pub sky@192.168.0.10:/home/sky 

在ubuntu server服务器上运行:

mkdir -p .ssh
touch ~/.ssh/authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

以后简单输入 “ssh ubuntu.server.ip” 即可自动登录。

快捷登录

如果服务器ip地址固定,则可以进一步简化,修改本机的/etc/hosts文件,加入下面内容:

# local machine list
192.168.100.10            skywork
192.168.100.20            skywork2
192.168.100.30            skydownload
192.168.100.40            skyserver
192.168.100.50            skyserver2
192.168.100.60            skyserver3

以后简单输入 “ssh skyserver” 即可。

指定特定端口登录

ssh默认采用22端口,对于需要进行端口映射导致不能访问22端口的情况,需要在ssh时通过 -p 参数指定具体的端口。

如下面的命令,有一台服务器的22端口是通过路由器的2122端口进行端口映射,则远程ssh连接的命令为:

ssh -p 2122 sky@dev.sky.io

修改本机的 ~/.bash_profile 或者 ~/.zshrc 文件,加入以下内容,以后就可以一个简单命令直接ssh到远程服务器了:

# ssh to home
alias sshwork="ssh sky@skywork"
alias sshwork2="ssh sky@skywor2"
alias sshserver="ssh sky@skyserver"
alias sshserver2="ssh sky@skyserver2"
alias sshserver3="ssh sky@skyserver3"

特别补充

腾讯云

在腾讯云上购买的linux主机,使用 SSH 登录:

https://cloud.tencent.com/document/product/1207/44643

特别提醒:首次通过本地 SSH 客户端登录 Linux 实例之前,您需要重置默认用户名(root)的密码,或者绑定密钥。否则会直接报错。

4 - 安装配置git

安装配置git

参见:

https://skyao.io/learning-git/docs/installation.html

5 - 安装配置zsh作为默认shell

安装配置zsh和ohmyzsh,替代默认的bash

背景

zsh的功能极其强大,只是配置过于复杂,起初只有极客才在用。后来,一些极客为了让更多人受益,开发了Oh-my-zsh这个插件,用来简化zsh的配置,让zsh更加容易上手。

官网地址:

https://github.com/ohmyzsh/ohmyzsh

安装

首先安装 zsh:

sudo apt install zsh zsh-doc

然后安装ohmyzsh:

sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

DNS 污染问题:

如果遇到 DNS 污染,导致 raw.githubusercontent.com 被解析到 127.0.0.1 或者 0.0.0.1 导致无法访问。需要修改 hosts 文件:

sudo vi /etc/hosts

增加一行:

199.232.68.133 raw.githubusercontent.com

中途询问是否把zsh作为默认 shell 时选择Y:

Do you want to change your default shell to zsh? [Y/n] Y
Changing the shell...

配置

关闭自动粘贴转义

Oh-my-zsh 默认开启自动粘贴转义,容易造成问题,建议关闭。

vi ~/.zshrc

修改 .zshrc 文件:

# Uncomment the following line if pasting URLs and other text is messed up.
# 把这里的注释取消即可
DISABLE_MAGIC_FUNCTIONS="true"

配置插件

https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins

Oh-my-zsh 默认将插件存放在 ~/.oh-my-zsh/plugins 目录下,数量非常多:

➜  ~ cd .oh-my-zsh/plugins 
➜  plugins git:(master) ls
adb                composer        frontend-search           ipfs              n98-magerun            redis-cli      terraform
ag                 copybuffer      fzf                       isodate           nanoc                  repo           textastic
aliases            copydir         gas                       iterm2            ng                     ripgrep        textmate
alias-finder       copyfile        gatsby                    jake-node         nmap                   ros            thefuck
ansible            cp              gb                        jenv              node                   rsync          themes
ant                cpanm           gcloud                    jfrog             nomad                  ruby           thor
apache2-macports   dash            geeknote                  jhbuild           npm                    rust           tig
arcanist           debian          gem                       jira              npx                    rustup         timer
archlinux          deno            genpass                   jruby             nvm                    rvm            tmux
asdf               dircycle        gh                        jsontools         oc                     safe-paste     tmux-cssh
autoenv            direnv          git                       jump              octozen                salt           tmuxinator
autojump           dirhistory      git-auto-fetch            kate              osx                    samtools       torrent
autopep8           dirpersist      git-escape-magic          keychain          otp                    sbt            transfer
aws                django          git-extras                kitchen           pass                   scala          tugboat
battery            dnf             gitfast                   knife             paver                  scd            ubuntu
bazel              dnote           git-flow                  knife_ssh         pep8                   screen         ufw
bbedit             docker          git-flow-avh              kops              percol                 scw            universalarchive
bedtools           docker-compose  github                    kubectl           per-directory-history  sdk            urltools
bgnotify           docker-machine  git-hubflow               kubectx           perl                   sfdx           vagrant
boot2docker        doctl           gitignore                 kube-ps1          perms                  sfffe          vagrant-prompt
bower              dotenv          git-lfs                   lando             phing                  shell-proxy    vault
branch             dotnet          git-prompt                laravel           pip                    shrink-path    vim-interaction
brew               droplr          glassfish                 laravel4          pipenv                 singlechar     vi-mode
bundler            drush           globalias                 laravel5          pj                     spring         virtualenv
cabal              eecms           gnu-utils                 last-working-dir  please                 sprunge        virtualenvwrapper
cake               emacs           golang                    lein              pm2                    ssh-agent      vscode
cakephp3           ember-cli       gpg-agent                 lighthouse        pod                    stack          vundle
capistrano         emoji           gradle                    lol               postgres               sublime        wakeonlan
cargo              emoji-clock     grails                    lxd               pow                    sublime-merge  wd
cask               emotty          grc                       macports          powder                 sudo           web-search
catimg             encode64        grunt                     magic-enter       powify                 supervisor     wp-cli
celery             extract         gulp                      man               profiles               suse           xcode
chruby             fabric          hanami                    marked2           pyenv                  svcat          yarn
chucknorris        fancy-ctrl-z    helm                      mercurial         pylint                 svn            yii
cloudfoundry       fasd            heroku                    meteor            python                 svn-fast-info  yii2
codeclimate        fastfile        history                   microk8s          rails                  swiftpm        yum
coffee             fbterm          history-substring-search  minikube          rake                   symfony        z
colemak            fd              hitchhiker                mix               rake-fast              symfony2       zbell
colored-man-pages  firewalld       hitokoto                  mix-fast          rand-quote             systemadmin    zeus
colorize           flutter         homestead                 mongocli          rbenv                  systemd        zoxide
command-not-found  fnm             httpie                    mosh              rbfu                   taskwarrior    zsh-interactive-cd
common-aliases     forklift        invoke                    mvn               react-native           terminitor     zsh-navigation-tools
compleat           fossil          ionic                     mysql-macports    rebar                  term_tab       zsh_reload

比较常用的:

  • git
  • golang
  • rust / rustup
  • docker / docker-compose / docker-machine
  • kubectl
  • npm / node
  • mvn
  • sudo
  • helm
  • redis-cli
  • ubuntu / ufw
  • wd
  • zsh-autosuggestions
  • zsh-syntax-highlighting
  • history-substring-search

小结

最后启动的插件如下所示:

plugins=(git golang rust docker docker-compose docker-machine kubectl npm node mvn sudo helm redis-cli ubuntu ufw wd zsh-autosuggestions zsh-syntax-highlighting history-substring-search)

配置样式

https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

暂时先用默认。

附录:常用插件用法

git插件

开启 git 插件后就可以使用以下简写命令了:

更多细节参见:

https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git

wd插件

可以快速切换到常用目录。

先安装 wd:

wget --no-check-certificate https://github.com/mfaerevaag/wd/raw/master/install.sh -O - | sh

使用方法:

# 进入某个目录
cd work/code/learning
pwd
/home/sky/work/code/learning
# 添加到wd
wd add learning
# 之后就可以用wd命令直接进入了
wd learning

# 方便期间,常见的学习笔记都加入wd,以l为前缀
/home/sky/work/code/learning/learning-rust
wd add lrust

详细使用参考:https://github.com/mfaerevaag/wd

sudo插件

按 2 次 esc 会在命令前自动输入 sudo

zsh-autosuggestions插件

根据历史记录智能自动补全命令,输入命令时会以暗色补全,按方向键右键完成输入。

安装命令:

git clone https://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

zsh-syntax-highlighting插件

fish shell 风格的语法高亮插件。输入的命令根据主题自动高亮。

安装命令:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

history-substring-search 插件

历史命令搜索插件,如果和 zsh-syntax-highlighting 插件共用,要配置到语法高亮插件之后。

安装命令:

git clone https://github.com/zsh-users/zsh-history-substring-search.git $ZSH_CUSTOM/plugins/history-substring-search

参考资料

6 - 配置网络

配置网络

问题:卡住两分钟

开机启动时出现两分钟的停顿,显示提醒如下:

A start job is running for wait for Network to be configured

然后2分钟超时才能继续启动。

登录之后检查:

$ sudo systemctl status systemd-networkd-wait-online.service

输出为:

$ sudo systemctl status systemd-networkd-wait-online.service
systemd-networkd-wait-online.service - Wait for Network to be Configured
     Loaded: loaded (/lib/systemd/system/systemd-networkd-wait-online.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sat 2022-01-15 08:12:57 UTC; 7min ago
       Docs: man:systemd-networkd-wait-online.service(8)
    Process: 1272 ExecStart=/lib/systemd/systemd-networkd-wait-online (code=exited, status=1/FAILURE)
   Main PID: 1272 (code=exited, status=1/FAILURE)

Jan 15 08:10:57 skywork2 systemd[1]: Starting Wait for Network to be Configured...
Jan 15 08:11:28 skywork2 systemd-networkd-wait-online[1272]: managing: ens1
Jan 15 08:11:28 skywork2 systemd-networkd-wait-online[1272]: managing: enp6s0
Jan 15 08:11:28 skywork2 systemd-networkd-wait-online[1272]: managing: ens1
Jan 15 08:11:28 skywork2 systemd-networkd-wait-online[1272]: managing: enp6s0
Jan 15 08:12:57 skywork2 systemd-networkd-wait-online[1272]: Event loop failed: Connection timed out
Jan 15 08:12:57 skywork2 systemd[1]: systemd-networkd-wait-online.service: Main process exited, code=exited, status=1/FAILURE
Jan 15 08:12:57 skywork2 systemd[1]: systemd-networkd-wait-online.service: Failed with result 'exit-code'.
Jan 15 08:12:57 skywork2 systemd[1]: Failed to start Wait for Network to be Configured.

问题所在在于网卡的配置:

networkctl 
IDX LINK   TYPE       OPERATIONAL SETUP      
  1 lo     loopback   carrier     unmanaged  
  2 enp5s0 ether      no-carrier  configuring
  3 enp6s0 ether      routable    configured 
  4 ens1   ether      routable    configured 
  5 ibs1d1 infiniband off         unmanaged  

5 links listed.

解决方案1-减少超时时间

cd /etc/systemd/system/network-online.target.wants/
sudo vi systemd-networkd-wait-online.service

[Service]下添加一行 TimeoutStartSec=2sec

[Service]
Type=oneshot
ExecStart=/lib/systemd/systemd-networkd-wait-online
RemainAfterExit=yes
TimeoutStartSec=15sec			# 增加这一行

这样15秒钟之后就会继续启动,而不是卡住两分钟,虽然治标不治本。

TBD: 发现我的40G网络会有dhcp获取IP地址很慢的问题,基本要30秒左右才能拿到IP地址,导致启动时很慢。即使这里设置timeout可以继续启动操作系统, 但是进入桌面之后由于40G网络的IP尚未能获取,用40G网络的IP地址会无法访问。千兆网络dhcp的速度就非常快。

解决方案2-配置网络

终极解决方案还是要配置好网络。用 ip 命令查看当前网卡情况:

ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:e0:4c:68:f7:da brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.50/24 brd 192.168.0.255 scope global dynamic enp4s0
       valid_lft 81706sec preferred_lft 81706sec
    inet6 fe80::2e0:4cff:fe68:f7da/64 scope link 
       valid_lft forever preferred_lft forever
3: enp5s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:e0:4c:54:17:3a brd ff:ff:ff:ff:ff:ff
4: enp6s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:e0:4c:54:17:3b brd ff:ff:ff:ff:ff:ff
5: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 48:0f:cf:ef:08:11 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.50/24 brd 10.0.0.255 scope global dynamic ens4
       valid_lft 38533sec preferred_lft 38533sec
    inet6 fe80::4a0f:cfff:feef:811/64 scope link 
       valid_lft forever preferred_lft forever
6: ibs4d1: <BROADCAST,MULTICAST> mtu 4092 qdisc noop state DOWN group default qlen 256
    link/infiniband a0:00:03:00:fe:80:00:00:00:00:00:00:48:0f:cf:ff:ff:ef:08:12 brd 00:ff:ff:ff:ff:12:40:1b:ff:ff:00:00:00:00:00:00:ff:ff:ff:ff

参考资料:

但上面的方案对于不准备使用的网口(比如连网线都没插)来说是无效的。对于这样的网口,我们需要彻底的禁用他们。

systemctl | grep net-devices        
  sys-subsystem-net-devices-enp4s0.device                                                   loaded active plugged   RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller                                      
  sys-subsystem-net-devices-enp5s0.device                                                   loaded active plugged   RTL810xE PCI Express Fast Ethernet controller                                                  
  sys-subsystem-net-devices-enp6s0.device                                                   loaded active plugged   RTL810xE PCI Express Fast Ethernet controller                                                  
  sys-subsystem-net-devices-ens4.device                                                     loaded active plugged   MT27520 Family [ConnectX-3 Pro] (InfiniBand FDR/Ethernet 10Gb/40Gb 2-port 544+FLR-QSFP Adapter)
  sys-subsystem-net-devices-ibs4d1.device                                                   loaded active plugged   MT27520 Family [ConnectX-3 Pro] (InfiniBand FDR/Ethernet 10Gb/40Gb 2-port 544+FLR-QSFP Adapter)

实践中发现网上的很多方法都无效,比如 ifconfig / ip 中的 down/up 命令,重启之后就无效了。

目前 systemd 接管的网卡情况如下:

networkctl 
$ networkctl 
IDX LINK   TYPE       OPERATIONAL SETUP      
  1 lo     loopback   carrier     unmanaged  
  2 enp4s0 ether      routable    configured 
  3 enp5s0 ether      no-carrier  configuring
  4 enp6s0 ether      no-carrier  configuring
  5 ens4   ether      routable    configured 
  6 ibs4d1 infiniband off         unmanaged  

在这里,enp5s0 和 enp6s0 这两张网卡是我们希望禁用的。

参考以下资料的说明:

我们在 /usr/lib/systemd/network 目录下放置两个文件来申明我们要禁用两块网卡,操作如下:

cd /usr/lib/systemd/network
sudo vi 01-disable-enp5s0.network

创建文件,内容如下:

[Match]
MACAddress=00:e0:4c:54:17:3a

[Link]
Unmanaged=yes
cd /usr/lib/systemd/network
sudo vi 02-disable-enp6s0.network

创建文件,内容如下:

[Match]
MACAddress=00:e0:4c:54:17:3b

[Link]
Unmanaged=yes

注意:[Match] 这里用 mac 地址来匹配,不要用 name,实际测试中我发现用 name 做匹配和设置 Unmanaged 后,系统中的网卡会发生名字变化的情况,导致匹配出现问题。切记用 mac 地址进行匹配。

重启机器之后,看效果:

$ networkctl
$ networkctl
IDX LINK   TYPE       OPERATIONAL SETUP     
  1 lo     loopback   carrier     unmanaged 
  2 enp4s0 ether      routable    configured
  3 enp5s0 ether      off         unmanaged 			# 被禁用了
  4 enp6s0 ether      off         unmanaged  			# 被禁用了
  5 ens4   ether      routable    configured
  6 ibs4d1 infiniband off         unmanaged 			# 这个口本来被禁用了

增加要管理的网卡

有某台机器,出现了网卡状态为 down 的情况,造成无法使用,原因不明。

$ ip addr                                     
......
5: ens4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 70:10:6f:aa:2a:81 brd ff:ff:ff:ff:ff:ff
    
$ networkctl
IDX LINK   TYPE       OPERATIONAL SETUP     
  1 lo     loopback   carrier     unmanaged 
  2 enp4s0 ether      routable    configured
  3 enp5s0 ether      off         unmanaged 
  4 enp6s0 ether      off         unmanaged 
  5 ens4   ether      off         unmanaged 
  6 ibs4d1 infiniband off         unmanaged 

解决方法:

cd /usr/lib/systemd/network
vi 03-ens4-dhcp.network

输入以下内容:

[Match]
MACAddress=70:10:6f:aa:2a:81

[Link]
Unmanaged=no

[Network]
DHCP=yes

重启即可。

7 - 安装Homebrew

Homebrew 是一个linux和mac上的应用管理工具,方便安装其他软件

安装

准备工作:

sudo apt-get install build-essential

开始安装:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> This script will install:
/home/linuxbrew/.linuxbrew/bin/brew
/home/linuxbrew/.linuxbrew/share/doc/homebrew
/home/linuxbrew/.linuxbrew/share/man/man1/brew.1
/home/linuxbrew/.linuxbrew/share/zsh/site-functions/_brew
/home/linuxbrew/.linuxbrew/etc/bash_completion.d/brew
/home/linuxbrew/.linuxbrew/Homebrew
==> The following new directories will be created:
/home/linuxbrew/.linuxbrew/bin
/home/linuxbrew/.linuxbrew/etc
/home/linuxbrew/.linuxbrew/include
/home/linuxbrew/.linuxbrew/lib
/home/linuxbrew/.linuxbrew/sbin
/home/linuxbrew/.linuxbrew/share
/home/linuxbrew/.linuxbrew/var
/home/linuxbrew/.linuxbrew/opt
/home/linuxbrew/.linuxbrew/share/zsh
/home/linuxbrew/.linuxbrew/share/zsh/site-functions
/home/linuxbrew/.linuxbrew/var/homebrew
/home/linuxbrew/.linuxbrew/var/homebrew/linked
/home/linuxbrew/.linuxbrew/Cellar
/home/linuxbrew/.linuxbrew/Caskroom
/home/linuxbrew/.linuxbrew/Frameworks

==> Downloading and installing Homebrew...

==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/sky/.zprofile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
    sudo apt-get install build-essential
  For more information, see:
    https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
    brew install gcc
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

按照提示执行:

echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/sky/.zprofile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

建议安装 gcc,那就装吧:

brew install gcc

参考资料

8 - [归档]添加新用户

添加新用户以便日常使用

ubuntu server 20.04 版本在安装时就需要设置用户名,而不是默认只有root账号。

添加新用户

默认安装后只有root账户,肯定不能直接用root。

注: 如果是用vmware自动安装,则会提前录入一个用户名,安装完成之后就可以直接用这个用户名。这样就可以跳过这个步骤。

因此增加一个日常使用的用户,这个用户需要拥有 sudo 的权限,以便在必要时可以得到 root 权限:

sudo adduser sky
sudo adduser sky sudo

后续可以通过 passwd 命令修改密码:

sudo passwd sky