使用者工具

網站工具


os:linux:centos7_-_技巧

CentOS7 - 技巧

修改預設開機的模式

使用指令切換文字模式或圖形模式

systemctl isolate multi-user.target //切換到模式3,該命令對下次啟動無影響,等價於telinit 3 
systemctl isolate runlevel3.target //切換到模式3,該命令對下次啟動無影響,等價於telinit 3 
systemctl isolate graphical.target //切換到模式5,該命令對下次啟動無影響,等價於telinit 5 
systemctl isolate runlevel5.target //切換到模式5,該命令對下次啟動無影響,等價於telinit 5 

執行systemctl命令,設置啟動時預設進入文字模式或圖形模式。

# systemctl -f enable multi-user.target //文字模式 
# systemctl -f enable graphical.target //圖形模式 

執行以下命令,設置啟動時預設進入文字模式或圖形模式

開機啟動之預設檔案為default.target,預設連結到graphical.target(大致相當於原來的運行級別5)。

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target //文字模式 
ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target //圖形模式 

安裝 ifconfig 命令

ifconfig命令是設置或顯示網絡接口的程序,可以顯示出我們機器的網卡信息,可是有些時候最小化安裝CentOS等Linux發行版的時候會默認不安裝ifconfig等命令,這時候你進入終端,運行ifconfig命令就會出錯

yum安裝ifconfig這個命令包

[root@CentOS7 ~]# yum search ifconfig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tc.edu.tw
 * extras: ftp.tc.edu.tw
 * updates: ftp.tc.edu.tw
============================== Matched: ifconfig ===============================
net-tools.x86_64 : Basic networking tools
[root@CentOS7 ~]#

通過yum search 這個命令我們發現ifconfig這個命令是在net-tools.x86_64這個包裡,接下來我們安裝這個包就行了

yum install net-tools

修改網卡名為eth0

從CentOS/RHEL7起,可預見的命名規則變成了默認。這一規則,接口名稱被自動基於固件,拓撲結構和位置信息來確定。現在,即使添加或移除網絡設備,接口名稱仍然保持固定,而無需重新枚舉,和壞掉的硬件可以無縫替換。

在 RHEL 及 CentOS 7 開始, 網卡的預設名稱由以往的 Ethx 改成 Enox, 以下文章會介紹將 CentOS 7 網卡名稱修改, 用回 Eth0 的方法。

但是這樣一來,雖然有好處,但也影響了不少用戶的操作,因為新的命名方案接口名稱比傳統的eth0相比有點難以閱讀。出於習慣等一些原因,你想修改網卡名稱,下面為大家介紹如何在CentOS7或RHEL7命名網絡接口。

無序列表項目系統安裝完畢,可能你的網卡命名是這樣的!

[root@CentOS7 ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.133  netmask 255.255.255.0  broadcast 192.168.200.255
        inet6 fe80::20c:29ff:fe83:6b22  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:6b:22  txqueuelen 1000  (Ethernet)
        RX packets 148  bytes 17435 (17.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 11507 (11.2 KiB)
        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 0  (Local Loopback)
        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

[root@CentOS7 ~]#

首先,先編輯網卡的配置文件 vi /etc/sysconfig/network-scripts/ifcfg-eno16777736 將裡面的NAME項修改為eth0

HWADDR="00:0C:29:83:6B:22"
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
NAME="eno16777736"
UUID="ec6adc96-025d-42ca-ae30-ae2b513c8ff3"
ONBOOT="yes"

全句是這樣:

NAME="eth0"

繼續,重命名該配置文件

[root@CentOS7 ~]# cd /etc/sysconfig/network-scripts/
[root@CentOS7 network-scripts]# mv ifcfg-eno16777736 ifcfg-eth0

然後,禁用該可預測命名規則。對於這一點,你可以在啟動時傳遞「net.ifnames=0 biosdevname=0 」的內核參數。這是通過編輯/etc/default/grub並加入「net.ifnames=0 biosdevname=0 」到GRUBCMDLINELINUX變量來實現的。

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto  rd.lvm.lv=centos_centos7/swap rd.lvm.lv=centos_centos7/root vconsole.font=latarcyrheb-sun16 vconsole.keymap=us rhgb quiet"
GRUB_DISABLE_RECOVERY="true"

裡面其中一行是 GRUB_CMDLINE_LINUX, 在這行結尾的雙引號前加上這句:

net.ifnames=0 biosdevname=0

要留意, 加上以上一行資料前, 要先加上一個空格, 整行修改後是這樣:

GRUB_CMDLINE_LINUX="crashkernel=auto  rd.lvm.lv=centos_centos7/swap rd.lvm.lv=centos_centos7/root vconsole.font=latarcyrheb-sun16 vconsole.keymap=us rhgb quiet net.ifnames=0 biosdevname=0"

然後用 grub2-mkconfig 指令重新建立 GRUB 的設定檔:

運行命令grub2-mkconfig -o /boot/grub2/grub.cfg 來重新生成GRUB配置並更新內核參數。

[root@CentOS7 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
[root@CentOS7 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Warning: Please don't use old title `CentOS Linux, with Linux 3.10.0-123.el7.x86_64' for GRUB_DEFAULT, use `Advanced options for CentOS Linux>CentOS Linux, with Linux 3.10.0-123.el7.x86_64' (for versions before 2.00) or `gnulinux-advanced-73d3a1f2-7f58-4459-9bcd-382a346c6e51>gnulinux-3.10.0-123.el7.x86_64-advanced-73d3a1f2-7f58-4459-9bcd-382a346c6e51' (for 2.00 or later)
Found linux image: /boot/vmlinuz-0-rescue-def0630cbe164c37894b6e5a40772064
Found initrd image: /boot/initramfs-0-rescue-def0630cbe164c37894b6e5a40772064.img
done
[root@CentOS7 ~]#

最後重新啟動系統便會生效, 要確認修改成功, 可以用 ifconfig 指令確認。

[root@CentOS7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.133  netmask 255.255.255.0  broadcast 192.168.200.255
        inet6 fe80::20c:29ff:fe83:6b22  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:6b:22  txqueuelen 1000  (Ethernet)
        RX packets 107  bytes 13408 (13.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69  bytes 11859 (11.5 KiB)
        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 0  (Local Loopback)
        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

[root@CentOS7 ~]#

創建規則

重啟在ifconfig查看應該就已經生效了,根據Centos 官方WIKI的FAQ中得知,如果你有多個接口,並且想要控制其設備名,而不是讓內核以它自己的方式命名,創建,/etc/udev/rules.d/XXX-net.rules規則是必要的!那麼這裡我們也創建好規則吧。

注意:以前系統的net規則名稱是70-persistent-net.rules,這裡我也按照這個名字定義規則!

[root@centos7 ~]# cat /etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1022:0x2000 (pcnet32) ---> (可能網卡 VID:PID 吧?)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:83:6b:22", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
[root@centos7 ~]#

解決 CentOS 7 開機不啟動 rc.local

以前在 Redhat 及 CentOS 要設定開機自動執行的 Shell Script, 一般都會在檔案 /etc/rc.d/rc.local 加入需要執行的 Shell Script 或指令, 但在 CentOS 7 開始, /etc/rc.d/rc.local 預設權限改為 644, 即沒有執行權限, 為甚麼會這樣, 可以開啟 CentOS 7 的 /etc/rc.d/rc.local 看看, 裡面有 Redhat 的說明:

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In constrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

可以看到這個檔案在 RHEL 及 CentOS 7 只為了解決兼容性, Redhat 建議還是自行建立 systemd 服務或者 udev rules 較好, 如果真的需要使用 rc.local, 只要執行 chmod 給予 rc.local 可執行權限即可:

# chmod +x /etc/rc.d/rc.local

執行以上指令後, 下次開機便會自動執行 rc.local 內的指令或 Shell Script.

CentOS 7 Service 操作

CentOS 7.0 開始,服務的管理使用變成用 systemctl,例如 httpd 的使用可用下面幾種方式

# systemctl status|start|stop|restart|reload httpd

OR

# service httpd status|start|stop|restart|reload

OR

# apachectl configtest| graceful

service httpd 會自動重導指令到 systemctl 命令,問題不大。

CentOS 7 安裝設定 Samba

CentOS 7.0 x64 的 iptables 與 firewall-cmd 防火牆

RHEL 7 / CentOS 7 : 關閉 Firewalld 及使用 iptables

CentOS 7 安裝 Apache, MariaDB, PHP

https://www.phpini.com/linux/redhat-centos-7-setup-apache-mariadb-php

安裝 Apache

# yum install httpd

跟著回答 “y” 後便會完成安裝, 然後輸入以下指令啟動及設定 Apache 開機自動執行:

# systemctl start httpd
# systemctl enable httpd

這時 Apache 已經啟動了, 可以在瀏覽器輸入伺服器的位置試試, 例如 http://localhost

安裝 MariaDB

執行以下指令安裝 MariaDB:

# yum install mariadb-server mariadb

啟動及設定開機自動執行 MariaDB:

# systemctl start mariadb
# systemctl enable mariadb

執行以下指令設定 MariaDB 的 root 密碼, 預設是空密碼, 所以建議盡快修改:

# /usr/bin/mysql_secure_installation

完成後可以用測試一下 MariaDB 是否已經啟動:

# mysql -u root -p 

安裝 PHP

以下指令除了會安裝 PHP 外, 還會安裝一些常用的 PHP 套件:

# yum install php php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

安裝好 PHP 後需要重新啟動 Apache 才會生效:

# systemctl restart httpd.service

要測試 PHP, 可以放一個簡單的 PHP Script 會 Apache DocumentRoot 目錄, 這裡:

# vi /var/www/html/test.php

然後輸入以下幾行程式碼:

<?php
phpinfo();
?>

然後在瀏覽器看看 http://localhost/test.php, 如果看到 PHP 的設定資料便表示安裝完成了。

CentOS 7 upgrade php 5.6

How to Upgrade PHP version 5.4 to 5.6 on CentOS/RHEL 6/7

# php -v

To upgrade php, first you must include the Webtatic EL yum repository data relating to your CentOS/RHEL variant to yum:

For CentOS/RHEL 7:

# rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

For CentOS/RHEL 6:

# rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

After installing Webtatic yum repository, you have to remove the old version of php-common package. It will also delete it dependencies packages.

# yum remove php-common

Now we install PHP 5.5 using following command:

# yum install -y php56w php56w-opcache php56w-xml php56w-mcrypt php56w-gd php56w-devel php56w-mysql php56w-intl php56w-mbstring
os/linux/centos7_-_技巧.txt · 上一次變更: 2019/11/16 08:12 (外部編輯)