第1章 环境安装配置
1.4 CentOS 基本配置
1、修改hosts
vi /etc/hosts
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.131 node1
192.168.80.132 node2
192.168.80.133 node3
或者
[root@localhost ~]# echo '192.168.80.101 node1' >> /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.80.101 node1
2、修改主机名
永久修改主机名(需要重启)
vi /etc/hostname
[root@localhost ~]# vi /etc/hostname
首先删除主机名默认值localhost.localdomain,然后修改为node1
[root@localhost ~]# cat /etc/hostname
node1
修改临时主机名(避免重启)
[root@localhost ~]# hostname node1
这是只要重新打开一个Shell即可看到修改的临时主机名
XShell重新链接node1
即可发现主机名已经和永久主机名相同。
2017-06-29更新
可以通过命令hostnamectl set-hostname
永久修改主机名,无需编辑文件
[root@abc ~]# hostnamectl set-hostname node1
[root@abc ~]# hostname
node1
[root@abc ~]# cat /etc/hostname
node1
3、更换镜像源
- 1)先安装wget
执行命令yum install -y wget
[root@node1 ~]# yum install -y wget
-
- 查询国内镜像源
打开阿里镜像http://mirrors.aliyun.com/
- 3)下载CentOS 7的repo文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2017-05-07 09:21:28-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 115.28.122.210, 112.124.140.210
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|115.28.122.210|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2573 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[==============================================================================>] 2,573 --.-K/s in 0s
2017-05-07 09:21:28 (279 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2573/2573]
2018-08-11更新
如果没有安装wget,可以直接使用curl命令
[root@node1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
102 2572 102 2572 0 0 499 0 0:00:05 0:00:05 --:--:-- 49461
[root@node1 ~]#
-
4)更新镜像源
-
yum clean all:清除缓存
-
yum makecache:生成缓存
[root@node1 ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@node1 ~]# yum makecache
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/12): base/7/x86_64/group_gz | 155 kB 00:00:00
(2/12): extras/7/x86_64/filelists_db | 770 kB 00:00:02
(3/12): extras/7/x86_64/primary_db | 151 kB 00:00:00
(4/12): extras/7/x86_64/other_db | 640 kB 00:00:07
(5/12): updates/7/x86_64/filelists_db
.......省略
4、关闭防护墙和selinux
执行命令:
systemctl stop firewalld
systemctl disable firewalld
[root@node1 ~]# systemctl stop firewalld
[root@node1 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
补充:CentOS 6 关闭防火墙
[root@node1 ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@node1 ~]# chkconfig iptables off
执行命令:
setenforce 0
vi /etc/selinux/config
[root@node1 ~]# setenforce 0
[root@node1 ~]# vi /etc/selinux/config
修改:SELINUX=disabled
或者使用sed命令直接修改
[root@node1 ~]# sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
5、安装vim
[root@node1 ~]# yum install -y vim
[root@node1 ~]# vi .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@node1 ~]# source ~/.bashrc
再次编辑bashrc,会发现vi实际上使用的vim
6、安装SSH
yum install -y openssh-clients
[root@node1 ~]# yum install -y openssh-clients
7、安装JDK
-
1)首先在宿主机下载好JDK
-
2)通过Xftp上传JDK到虚拟机
[root@node1 ~]# ls
anaconda-ks.cfg jdk-8u112-linux-x64.tar.gz
将软件包解压缩到/opt目录
tar -zxvf jdk-8u112-linux-x64.tar.gz -C /opt
[root@node1 ~]# tar -zxvf jdk-8u112-linux-x64.tar.gz -C /opt
[root@node1 ~]# ls /opt/jdk1.8.0_112/
bin COPYRIGHT db include javafx-src.zip jre lib LICENSE man README.html release src.zip THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt
复制jdk的根目录/opt/jdk1.8.0_112,下面配置环境变量将使用到。
编辑环境变量
vi /etc/profile.d/custom.sh
[root@node1 ~]# vi /etc/profile.d/custom.sh
[root@node1 ~]# cat /etc/profile.d/custom.sh
#!/bin/bash
#java path
export JAVA_HOME=/opt/jdk1.8.0_112
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib
使生效
source /etc/profile.d/custom.sh
[root@node1 ~]# source /etc/profile.d/custom.sh
查看JDK版本
[root@node1 ~]# java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
2023-09-24遇到问题
[root@node1 opt]# java -version
-bash: /opt/jdk1.8.0_381/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
原因,下载了32位的JDK
[root@node1 opt]# ll
drwxr-xr-x. 8 root root 4096 9月 24 10:30 jdk1.8.0_381
-rw-r--r--. 1 root root 142711034 9月 24 10:29 jdk-8u381-linux-i586.tar.gz
[root@node1 opt]#
解决办法,重新下载64位版本的JDK:jdk-8u381-linux-x64.tar.gz