环境

CentOS Linux release 7.5.1804

Q

新安装CentOS后,为了方便远程ssh,想设置静态IP,但是在/etc/sysconfig/network-scripts/下面只有ifcfg-lo这张Local Loopback网卡的配置文件,并没有我们想要的enp0s3(不同机器的网卡名不一样,也可能是ens33或其它名字)网卡配置文件,想要配置静态IP根本无从下手。

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-lo     ifdown-post      ifup          ifup-plip    ifup-TeamPort
ifdown       ifdown-ppp       ifup-aliases  ifup-plusb   ifup-tunnel
ifdown-bnep  ifdown-routes    ifup-bnep     ifup-post    ifup-wireless
ifdown-eth   ifdown-sit       ifup-eth      ifup-ppp     init.ipv6-global
ifdown-ippp  ifdown-Team      ifup-ippp     ifup-routes  network-functions
ifdown-ipv6  ifdown-TeamPort  ifup-ipv6     ifup-sit     network-functions-ipv6
ifdown-isdn  ifdown-tunnel    ifup-isdn     ifup-Team

A

  • 使用nmcli con show查看网卡的UUID
[root@localhost network-scripts]# nmcli con show
NAME        UUID                                  TYPE      DEVICE  
enp0s3  ad633ea8-e358-255e-a167-6c14742b347d  ethernet  enp0s3 
  • /etc/sysconfig/network-scripts/目录下新建ifcfg-enp0s3(网卡名改成自己机器上的网卡名)
vim ifcfg-enp0s3  # 文件名改为自己的网卡名
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3  # 改为自己的网卡名
UUID=ad633ea8-e358-255e-a167-6c14742b347d  # 改为自己的UUID
DEVICE=enp0s3  # 改为自己的网卡名
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.222  # 改为自己想要的IP,前三段保持和原来的IP一致
NETMASK=255.255.255.0
GATEWAY=192.168.0.1  # 一般为x.x.x.1,具体可在物理机上使用ipconfig命令查看
DNS1=114.114.114.114
DNS2=114.114.115.115
  • 配置文件保存后重启网络服务
systemctl restart network
  • 查看网络信息
[root@localhost network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.222  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 de12::f1a4:3694:6189:d250 prefixlen 64  scopeid 0x20<link>
        ether 05:01:24:0a:54:15  txqueuelen 1000  (Ethernet)
        RX packets 221235  bytes 325157863 (310.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 129166  bytes 10537641 (10.0 MiB)
        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 40  bytes 3528 (3.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 40  bytes 3528 (3.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 惯例,调戏下百度
[root@localhost network-scripts]# ping www.baidu.com
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112 (115.239.211.112): icmp_seq=1 ttl=54 time=12.0 ms
64 bytes from 115.239.211.112 (115.239.211.112): icmp_seq=2 ttl=54 time=11.9 ms
  • 搞定

博客更新地址

环境

CentOS Linux release 7.5.1804

脚本完成操作

  • 切换CentOS软件镜像源为中科大软件源
  • 设置防火墙,允许全部端口通过
  • 安装git
  • 安装Pyenv包管理工具,Pyenv使用详见:Python版本管理工具 Pyenv的安装与使用
  • 安装Python 3.6.4
  • 切换pip源为豆瓣
  • 安装Docker
  • 安装MySQL
  • 初始化MySQL(root@localhost的密码为123456,song@%的密码为123456)
  • 设置sshd开机启动
  • 安装ntp时间自动更新工具

使用

  • Github下载部署脚本代码
  • 百度云下载包含MySQL rpm包的required_rpms文件夹(MySQL官网由于国内特殊的网络环境原因,下载很慢,经常中断导致无法安装,因而将安装所需的rpm包单独down了下来,又因为所有MySQL包加起来有200+M,不方便传到Github上,只能传到百度云上单独下载)
  • 将百度云上下载的required_rpms文件夹加入到代码主目录,最终目录如下:
.
├── 0_start.sh
├── 1_shell_init.sh
├── 2_deploy_firewall.sh
├── 3_install_git.sh
├── 4_install_pip.sh
├── 5_install_docker.sh
├── 6_install_mysql.sh
├── 7_enable_sshd.sh
├── 99_shell_finalize.sh
├── deploy_mysql_hook
│   ├── 0_deploy_mysql.sh
│   └── init_mysql.exp
├── post_hook
│   └── 1_test.sh
├── pre_hook
│   └── 1_test.sh
├── README.md
└── required_rpms
    ├── mysql
    │   ├── mariadb-5.5.56-2.el7.x86_64.rpm
    │   ├── perl-5.16.3-292.el7.x86_64.rpm
    │   ├── perl-Carp-1.26-244.el7.noarch.rpm
    │   ├── perl-constant-1.27-2.el7.noarch.rpm
    │   ├── perl-Encode-2.51-7.el7.x86_64.rpm
    │   ├── perl-Exporter-5.68-3.el7.noarch.rpm
    │   ├── perl-File-Path-2.09-2.el7.noarch.rpm
    │   ├── perl-File-Temp-0.23.01-3.el7.noarch.rpm
    │   ├── perl-Filter-1.49-3.el7.x86_64.rpm
    │   ├── perl-Getopt-Long-2.40-2.el7.noarch.rpm
    │   ├── perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
    │   ├── perl-libs-5.16.3-292.el7.x86_64.rpm
    │   ├── perl-macros-5.16.3-292.el7.x86_64.rpm
    │   ├── perl-parent-0.225-244.el7.noarch.rpm
    │   ├── perl-PathTools-3.40-5.el7.x86_64.rpm
    │   ├── perl-Pod-Escapes-1.04-292.el7.noarch.rpm
    │   ├── perl-podlators-2.5.1-3.el7.noarch.rpm
    │   ├── perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
    │   ├── perl-Pod-Simple-3.28-4.el7.noarch.rpm
    │   ├── perl-Pod-Usage-1.63-3.el7.noarch.rpm
    │   ├── perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
    │   ├── perl-Socket-2.010-4.el7.x86_64.rpm
    │   ├── perl-Storable-2.45-3.el7.x86_64.rpm
    │   ├── perl-Text-ParseWords-3.29-4.el7.noarch.rpm
    │   ├── perl-threads-1.87-4.el7.x86_64.rpm
    │   ├── perl-threads-shared-1.43-6.el7.x86_64.rpm
    │   ├── perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
    │   └── perl-Time-Local-1.2300-2.el7.noarch.rpm
    ├── mysql57-community-release-el7-11.noarch.rpm
    ├── mysql-community-server
    │   ├── mysql-community-client-5.7.20-1.el7.x86_64.rpm
    │   ├── mysql-community-common-5.7.20-1.el7.x86_64.rpm
    │   ├── mysql-community-devel-5.7.20-1.el7.x86_64.rpm
    │   ├── mysql-community-libs-5.7.20-1.el7.x86_64.rpm
    │   ├── mysql-community-libs-compat-5.7.20-1.el7.x86_64.rpm
    │   └── mysql-community-server-5.7.20-1.el7.x86_64.rpm
    └── mysql-devel
        ├── keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm
        ├── krb5-devel-1.15.1-8.el7.x86_64.rpm
        ├── libcom_err-devel-1.42.9-10.el7.x86_64.rpm
        ├── libkadm5-1.15.1-8.el7.x86_64.rpm
        ├── libselinux-devel-2.5-11.el7.x86_64.rpm
        ├── libsepol-devel-2.5-6.el7.x86_64.rpm
        ├── libverto-devel-0.2.5-4.el7.x86_64.rpm
        ├── mariadb-devel-5.5.56-2.el7.x86_64.rpm
        ├── openssl-devel-1.0.2k-8.el7.x86_64.rpm
        ├── pcre-devel-8.32-17.el7.x86_64.rpm
        └── zlib-devel-1.2.7-17.el7.x86_64.rpm
  • 在主目录运行source 0_start.sh,等待执行完成即可,执行结果如下:
==================================================
shell exec time
1_shell_init.sh      exec time:      0 m  25 s
pre_hook     exec time:      0 m  0 s
2_deploy_firewall.sh     exec time:      0 m  2 s
3_install_git.sh     exec time:      0 m  7 s
4_install_pip.sh     exec time:      4 m  3 s
5_install_docker.sh      exec time:      1 m  2 s
6_install_mysql.sh   exec time:      1 m  6 s
deploy_mysql_hook    exec time:      0 m  3 s
7_enable_sshd.sh     exec time:      0 m  0 s
post_hook    exec time:      0 m  0 s

all_shell        exec time:      6 m  48 s
==================================================

博客更新地址