Linux小步走(一):从安装系统开始

【引言】想玩转Linux,首先必须得有个Linux的系统,通常我们都是用的windows,好在现在的虚拟化软件做的也非常成熟了,今天第一讲就来谈谈VMware上怎么安装CentOS,实际现在常用的也就是CentOS和Ubuntu两个系统。


VMware Workstation

  作为一个合格的程序猿,安装这个软件想来不是什么难事,所以这里就不做过多的废话了。(PS:现在Oracle的Virtual VM做的也不错,而且还不用到处找激活码的,也可以试试)

VM配置参考

  以下是此次安装完成后的CentOS系统的设置,常规情况下也就是配上内存、CPU、网络(我个人比较习惯用bridge模式)等,如果对磁盘有特殊需求可以在安装前自定义分区。

网络工具初始化

说在前面

  因为默认使用的minimal安装方式,所以此次安装只装上了核心包,缺失了很多的工具包(比如ifconfig),最小安装有利有弊,利在于系统很精简,装什么都可以自己控制,弊在于需要先把网络调起来才能进行下面的一系列的操作。
  因为用到静态IP地址,所以下面有一些针对IP配置的操作,如果不需要做静态地址,其实也可以略过。

ifconfig

  初始状态的系统是没有ifconfig工具的,所以我们通过yum search查一下哪个包里面提供了这个工具,结果发现是net-tools,所以后面的工作就是安装这个tools包的事情了。

1
2
3
4
5
6
7
8
9
[root@aliyun-db ~]# yum search ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: centos.ustc.edu.cn
* updates: centos.ustc.edu.cn
================== Matched: ifconfig ===========================
net-tools.x86_64 : Basic networking tools
[root@aliyun-db ~]#

网络不通怎么办?

  虽然我们找到了net-tools包需要安装,但是实际上当前情况下网络是不通的,所以我们需要将网卡打开,启动一下网络。(应该可以装系统时就设置网卡自动启动的,这样就省去这个步骤了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@aliyun-db ~]#  vi /etc/sysconfig/network-scripts/ifcfg-ens33
...
# 这里设置为yes即可开机启动了
ONBOOT=yes
[root@aliyun-db ~]# service network restart
Restarting network (via systemctl): [ OK ]
[root@aliyun-db ~]#
[root@aliyun-db ~]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.108) 56(84) bytes of data.
64 bytes from 180.97.33.108 (180.97.33.108): icmp_seq=1 ttl=57 time=17.4 ms

64 bytes from 180.97.33.108 (180.97.33.108): icmp_seq=2 ttl=57 time=6.54 ms
^C
--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 6.541/11.998/17.456/5.458 ms
[root@aliyun-db ~]#

安装net-tools

1
2
3
4
[root@aliyun-db ~]# yum install net-tools
...
[root@aliyun-db ~]# service network restart
...

验证ifconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.211 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:feaa:f308 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:aa:f3:08 txqueuelen 1000 (Ethernet)
RX packets 163044 bytes 229513496 (218.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12275 bytes 1831768 (1.7 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 12 bytes 12093 (11.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 12 bytes 12093 (11.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost conf]#

配置静态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
26
27
28
29
30
31
[root@aliyun-db ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
# 关闭DHCp
#BOOTPROTO=dhcp
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=ens33
UUID=e3a92afb-9eda-4dc3-9f47-1e109f768e6f
DEVICE=ens33
# 开机自启动
ONBOOT=yes

# 表示该接口将通过该配置文件进行设置,而不是通过网络管理器进行管理
NM_CONTROLLED=no
# 静态IP
BOOTPROTO=static
# 本机地址
IPADDR=192.168.1.214
# 子网掩码
NETMASK=255.255.255.0
# 默认网关
GATEWAY=192.168.1.1
# 来自ifconfig查看的ether
HWADDR=00:0c:29:da:27:c0

网关配置

1
2
3
4
5
6
7
8
[root@aliyun-db ~]# vi /etc/sysconfig/network
# Created by anaconda
NETWORKING=yes
GATEWAY=192.168.1.1
DNS1=119.29.29.29
DNS2=182.254.116.116
[root@localhost network-scripts]# service network restart
Restarting network (via systemctl):

更改yum源

  之前很流行163的源,现在阿里的源用的也不少,所以这里我们选择阿里的源来使用;随着阿里在技术领域的崛起,现在也提供了很多开源的工具和一些基础环境的支持,相比较而言,BAT里面阿里应该在程序员界的好感度是最高的了吧。

1
2
3
4
5
6
7
8
# 第一步:下载对应的repo文件到源配置目录
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 第二步:生成缓存
yum makecache生成缓存

# CentOS7和之前的版本略有差异,其他版本不可参考此方法

关于防火墙

直接关闭防火墙

1
2
3
4
5
6
7
8
#停止firewall
[root@aliyun-db ~]# systemctl stop firewalld.service

#禁止firewall开机启动
[root@aliyun-db ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@aliyun-db ~]#

配置iptables

  下面以开启mysql的3306端口作为一个实例,来演示一下如何修改防火墙配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装iptables service
[root@aliyun-db ~]# yum -y install iptables-services

[root@aliyun-db ~]# vi /etc/sysconfig/iptables
# 增加规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
:wq

# 重启防火墙使配置生效
[root@aliyun-db ~]# systemctl restart iptables.service

# 设置防火墙开机启动(最后重启系统使设置生效即可)
[root@aliyun-db ~]# systemctl enable iptables.service

hostname修改

  CentOS7的hostname设置和之前的版本是有区别的,所以这里的规则是不适用于其他的CentOS版本的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 # 使用这个命令会立即生效且重启也生效
[root@centos7 ~]$ hostnamectl set-hostname smart01
[root@centos7 ~]$ hostname
smart01

# 编辑下hosts文件, 给127.0.0.1添加hostname
[root@centos7 ~]$ vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 smart01
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

# 这里主要是为了便于部署一些集群时可以直接通过hostname来通信
192.168.1.211 smart01
192.168.1.212 smart02
192.168.1.213 smart03
:wq
[root@centos7 ~]$

安装JDK

  通常有些人会选择使用OpenJDK,但是本人不大习惯使用(可能因为之前遇到过一些坑吧),所以一般情况下我都是自己安装Oracle的JDK。
  首先我们要去Oracle官网下载一个需要的jdk版本包,比如jdk-8u191-linux-x64.tar.gz;然后将该版本包上传到服务器解压,随后配置环境变量,然后就大功告成了。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@aliyun-db ~]# tar -zxvf jdk-8u191-linux-x64.tar.gz
[root@aliyun-db ~]# vi /etc/profile
# 追加补充
export JAVA_HOME=/usr/local/ins/jdk1.8.0_191
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@aliyun-db ~]# source /etc/profile
[root@aliyun-db ~]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
[root@aliyun-db ~]#

------2019 Lin.C ------