Ubuntu 常用配置

整理一下比较常用的 Linux 配置文件更改记录,做个笔记。

获取系统发行版信息

1
lsb_release -a

执行结果示例:

1
2
3
4
5
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial

Ubuntu 更换软件源

在 Ubuntu 等 Debian 系 Linux 中,软件源一般由 /etc/apt/sources.list 文件记录,因此更改该文件即可实现换源。

建议在更换默认源之前先对默认源进行备份。

这里建议使用 USTC Mirrors 。

备份默认软件源:

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

Ubuntu 18.04 更改为 USTC Mirror :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo echo \ 
'deb http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
#deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
#deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
#deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
#deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse

## Not recommended
# deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse' \
> /etc/apt/sources.list

其他版本软件源:

Ubuntu 16.04 (xenial):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
deb http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse

## Not recommended
# deb http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu/ xenial-proposed main restricted universe multiverse

Ubuntu 14.04 (trusty):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
deb http://mirrors.ustc.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse

deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

## Not recommended
# deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

具体软件源信息请自行查看:

ubuntu更改主机名

1
2
$ sudo -s # 切换到超级用户
# echo node01 > /etc/hostname # 更改 hostname 文件

使用文本编辑器更改 /etc/hosts 文件中的 127.0.1.1 指向的主机名

1
sudo vim /etc/hosts

更改过的配置如下(主机名:node01)

1
2
3
4
5
6
7
8
9
127.0.0.1       localhost
127.0.1.1 node01

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

重启

1
sudo reboot

设置静态 IP

自动获取的 IP 地址如下:

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
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:8a:44:3f:99 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.129 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::9155:3ac:bc9e:ac3b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:ad:ad:2c txqueuelen 1000 (Ethernet)
RX packets 1075 bytes 84493 (84.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 869 bytes 94059 (94.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 172 bytes 12476 (12.4 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 172 bytes 12476 (12.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

设置静态 IP 地址

Ubuntu18 使用了 netplan 命令进行网络参数的管理

更改文件:

1
sudo vim /etc/netplan/01-network-manager-all.yaml

配置文件如下:

1
2
3
4
5
6
7
8
9
10
# Let NetworkManager manage all devices on this system
network:
version: 2
#renderer: NetworkManager
ethernets:
ens33: # 网卡
addresses: [192.168.100.201/24] # IP 以及子网掩码
gateway4: 192.168.100.2 # 默认网关
nameservers:
addresses: [192.168.100.2] # DNS 服务器

更改配置文件之后使用命令使其立即生效

1
sudo netplan apply

更改时区

1
sudo dpkg-reconfigure tzdata

然后选择对应的时区,系统会自动更新时间

关闭图形界面

方法一,直接卸载 lightdm (不推荐)

1
sudo systemctl disable lightdm.service

恢复:

重装 lightdm

1
sudo apt-get install --reinstall lightdm

方法二,可以随时开启 (推荐)

关闭,重启后生效,以后重启都将不开启图形界面:

1
sudo systemctl set-default multi-user.target

开启,重启后生效:

1
sudo systemctl set-default graphical.target

临时开启:

1
sudo systemctl start lightdm

设置 tmux

更改快捷键并设置 256 位颜色

1
vim ~/.tmux.conf

输入以下内容

1
2
3
set -g prefix C-a       # 设置前缀为Ctrl + a
unbind C-b # 解除Ctrl+b 与前缀的对应关系
set -g default-terminal "screen-256color" # 设置 256 位颜色

新建用户

1
adduser codingfanlt

这样就会自动创建用户主目录,创建用户同名的组。

到此,用户添加成功。如果需要让此用户有root权限,执行命令:

1
sudo vim /etc/sudoers

修改文件如下:

代码如下复制代码

1
2
3
4
5
# User privilege specification

root ALL=(ALL) ALL

codingfanlt ALL=(ALL) ALL

保存退出,codingfanlt 用户就拥有了root权限。

禁止 root 用户远程登陆

编辑配置文件:

1
/etc/ssh/sshd_config

更改配置文件:

1
2
# PermitRootLogin yes
PermitRootLogin no