@[TOC](# Ubuntu 网络详细配置)
Ubuntu ⽹络详细配置
Ubuntu Server系统基础配置
更改主机名
//编辑配置文件
vim /etc/hostname
//文件内容可修改
ubuntu-server.bokebi.cn
------------------------------------------------------
//使用命令修改
hostnamectl set-hostname ubuntu-server.bokebi.cn
--------------------------------------------------
//修改配置文件
vim /etc/hosts
//修改文件内容为
127.0.0.1 localhost
127.0.1.1 bj-magedu-v-study-234-157.bokebi.cn
-------------------------------------------------
#主机名修改规范
地区-机房简称-虚拟机or物理机-业务名称-IP地址后两位-域名
#范例
bj-magedu-v-study-234-157.bokebi.cn
更改⽹卡名称为eth*
如果没有在安装系统之前传递内核参数将⽹卡名称更改为eth*,则可以在安装系统之后使⽤以下⽅式修改
//编辑配置文件
sudo vim /etc/default/grub
//修改文件内容为
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=2
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT=""
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" #修改此行参数
//跟新内核参数文件
sudo update-grub
//一定要重启才能生效
sudo reboot
配置root远程登录
默认情况下,ubuntu不允许root⽤⼾远程ssh,如果有实际场景需要允许root⽤⼾远程ssh,则需要设置root密码,并且编辑/etc/ssh/sshd_config⽂件修改如下:
//修改配置文件
sudo vim /etc/ssh/sshd_config
//修改配置文件
32 #PermitRootLogin prohibit-password #默认为禁⽌登录
33 PermitRootLogin yes #改为允许登录
57 #PasswordAuthentication yes
58 PasswordAuthentication yes #打开密码认证,其实默认就是允许通过密码认证登录
--------------------------------------------------------------------------
//切换至root用户
sudo su - root #切换到root⽤⼾环境
//修改root密码
passwd #设置密码
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
-------------------------------------------------------------
//重启ssh服务并测试root⽤⼾远程ssh连接
systemctl restart sshd
Ubuntu 18.04⽹络配置
官方文档: netplan.io/
Ubuntu 从 17.10 开始,已放弃在 /etc/network/interfaces ⾥固定IP的配置,
⽽是改成 netplan ⽅式,配置⽂件是:/etc/netplan/01-netcfg.yaml
- ubuntu 17.04及之前的静态IP配置⽅式:
//配置文件路径
sudo vim /etc/network/interfaces
//可根据需要修改配置文件
auto eth0 #⽹卡⾃启动,写⾃⼰要配置IP的实际⽹卡名称
iface eth0 inet static #配置静态IP,写⾃⼰要配置IP的实际⽹卡名称
address 172.18.3.12 #IP地址
netmask 255.255.0.0 #掩码
gateway 172.18.0.1 #⽹关
dns-nameservers 223.6.6.6 #DNS
dns-nameservers 223.5.5.5
//重启⽹络服务
/etc/init.d/networking restart
systemctl restart networking.service
配置阿里云epel源
epel源地址: developer.aliyun.com/mirror/ubun…
//将原文件进行改名留作备份
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
//新建配置文件
sudo vim /etc/apt/sources.list
//复制对应版本源信息,粘贴
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
//更新本地软件包列表索引,修改了apt仓库后必须执⾏
sudo apt update
Ubuntu 18.04vim 个性配置
//编辑配置文件
vim ~/.vimrc
//根据需要编辑配置文件
set ai " 自动缩进,新行与前面的行保持?致的自动空格
set cursorline " 显示光标行 "
set aw " 自动写,转入shell或使用:n编辑其他文件时,当前的缓冲区被写入
set flash " 在出错处闪烁但不呜叫(缺省)
set ic " 在查询及模式匹配时忽赂大小写
set nu
set number " 屏幕左边显示行号
set showmatch " 显示括号配对,当键入“]”“)”时,高亮度显示匹配的括号
set showmode " 处于文本输入方式时加亮按钮条中的模式指示器
set warn " 对文本进行了新的修改后,离开shell时系统给出显示(缺省)
set ws " 在搜索时如到达文件尾则绕回文件头继续搜索
set wrap " 长行显示自动折行
colorscheme evening " 设定背景为夜间模式
filetype plugin on " 自动识别文件类型,自动匹配对应的, “文件类型Plugin.vim”文件,使用缩进定义文件
set autoindent " 设置自动缩进:即每行的缩进值与上一行相等;使用 noautoindent 取消设置
set cindent " 以C/C++的模式缩进
set noignorecase " 默认区分大小写
set ruler " 打开状态栏标尺
set scrolloff=5 " 设定光标离窗口上下边界 5 行时窗口自动滚动
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
set softtabstop=4 " 使得按退格键时可以一次删掉 4 个空格,不足 4 个时删掉所有剩下的空格)
set tabstop=4 " 设定 tab 长度为 4
set wrap " 自动换行显示
syntax enable
syntax on " 自动语法高亮
set autoindent "自动对齐"
set smartindent "智能对齐"
set mouse=a "使用鼠标"
inoremap ( ()<ESC>i "括号自动补齐"
inoremap [ []<ESC>i
inoremap { {}<ESC>i
inoremap < <><ESC>i
系统资源显示在优化
//配置文件路径
cat /etc/security/limits.conf
//文件内容追加
#root账⼾的资源软限制和硬限制 (Ubuntu需要添加上下两部分)
root soft core unlimited
root hard core unlimited
root soft nproc 1000000
root hard nproc 1000000
root soft nofile 1000000
root hard nofile 1000000
root soft memlock 32000
root hard memlock 32000
root soft msgqueue 8192000
root hard msgqueue 8192000
#其他账⼾的资源软限制和硬限制 (centos系统只需要下面这一部分即可)
* soft core unlimited
* hard core unlimited
* soft nproc 1000000
* hard nproc 1000000
* soft nofile 1000000
* hard nofile 1000000
* soft memlock 32000
* hard memlock 32000
* soft msgqueue 8192000
* hard msgqueue 8192000
内核参数优化
//配置文件
vim /etc/sysctl.conf
//文件内容覆盖
# Controls source route verification
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1
# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
# TCP kernel paramater
net.ipv4.tcp_mem = 786432 1048576 1572864
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
# socket buffer
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 20480
net.core.optmem_max = 81920
# TCP conn
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_syn_retries = 3
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 15
# tcp conn reuse
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_reuse = 0
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_max_tw_buckets = 20000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syncookies = 1
# keepalive conn
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.ip_local_port_range = 10001 65000
# swap
vm.overcommit_memory = 0
vm.swappiness = 10
#net.ipv4.conf.eth1.rp_filter = 0
#net.ipv4.conf.lo.arp_ignore = 1
#net.ipv4.conf.lo.arp_announce = 2
#net.ipv4.conf.all.arp_ignore = 1
#net.ipv4.conf.all.arp_announce = 2
Ubuntu软件包管理
修改软件仓库:
阿⾥云仓库地址:https://opsx.alibaba.com/
中科⼤:http://mirrors.ustc.edu.cn/help/ubuntu.html
清华⼤学:https://mirror.tuna.tsinghua.edu.cn/help/ubuntu/
华为:https://mirrors.huaweicloud.com/
apt list #apt列出仓库软件包,等于yum list
apt search NAME #搜索安装包
apt show apache2 #查看某个安装包的详细信息
apt install apache2 #在线安装软件包
apt remove apache2 #卸载单个软件包但是保留配置⽂件
apt autoremove apache2 #删除安装包并解决依赖关系
apt update #更新本地软件包列表索引,修改了apt仓库后必须执⾏
apt purge apache2 #卸载单个软件包删除配置⽂件
apt upgrade #升级所有已安装且可升级到新版本的软件包
apt full-upgrade #升级整个系统,必要时可以移除旧软件包。
apt edit-sources #编辑source源⽂件
apt-cache madison nginx #查看仓库中软件包有哪些版本可以安装
apt install nginx=1.14.0-0ubuntu1.6 #安装软件包的时候指定安装具体的版本
dpkg安装包管理
- rpm:RPM(Red Hat Package Manager),是基于Red hat的Linux Distribution的包管理系统,同时也指rpm包本⾝,RPM⽤于rpm包的管理(诸如安装、卸载、升级等)
- "dpkg "是"Debian Packager "的简写,为 "Debian"专⻔开发的套件管理系统,⽅便软件的安装、更新及移除。所有源⾃“Debian”的“Linux ”发⾏版都使⽤ “dpkg”,例如 “Ubuntu”、“Knoppix ”等。
# dpkg -i gitlab-ce_11.9.8-ce.0_amd64.deb #安装某个软件包
# dpkg -r gitlab-ce #删除某个软件包保留配置⽂件
# dpkg -r -P gitlab-ce #删除某个软件包不保留配置⽂件
# dpkg -I gitlab-ce_11.9.8-ce.0_amd64.deb #查看软件包信息
# dpkg -c gitlab-ce_11.9.8-ce.0_amd64.deb #查看软件包内的⽂件及⽬录内容
# dpkg -l #列出本机已经安装的所有软件