Linux运维笔记-日常操作命令总结【centos7添加主机名、网卡操作、SELINUX】

一、centos7添加主机名

1)要查看主机名相关的设置:
[root@kevin-test ~]# hostname
kevin-test.kevin.com
  
[root@kevin-test ~]# hostnamectl
   Static hostname: kevin-test.kevin.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 45461f76679f48ee96e95da6cc798cc8
           Boot ID: 1e7ed2e4777240529451d611c03465c6
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-327.13.1.el7.x86_64
      Architecture: x86-64
  
2)手动更新/etc/hosts(这个只是临时生效,重启服务器后,主机名还是会恢复默认)
[root@kevin-test ~]# cat /etc/hosts
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 kevin-test.kevin.com
 
===============================================================================
除此之外,还有一种永久修改主机名的方法:修改配置文件 /etc/hostname
[root@kevin-test ~]# vim /etc/hostname
kevin-test.kevin.com
 
然后重启服务器即可永久生效
[root@kevin-test ~]# reboot

二、网卡文件
1)修改网卡名

安装好centos7版本的系统后,发现默认的网卡名字有点怪,为了便于管理,可以手动修改。下面对centos7版本下网卡重命名操作做一记录:
1)编辑网卡信息
[root@linux-node2~]# cd /etc/sysconfig/network-scripts/                             #进入网卡目录
[root@linux-node2network-scripts]# mv ifcfg-eno16777728 ifcfg-eth0          #将默认的网卡名eno16777728改为eth0
[root@linux-node2network-scripts]# cat ifcfg-eth0                        #编辑网卡信息
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eth0                                    #name修改为eth0
ONBOOT=yes
IPADDR=192.168.56.12
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
DNS1=192.168.56.2

2)修改grub
[root@linux-node2~]# cat /etc/sysconfig/grub                  
#编辑内核信息,添加字段net.ifnames=0 biosdevname=0
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=autorhgb net.ifnames=0 biosdevname=0 quiet"
GRUB_DISABLE_RECOVERY="true"

[root@linux-node2~]# grub2-mkconfig -o /boot/grub2/grub.cfg       #生成启动菜单
Generatinggrub configuration file ...
Foundlinux image: /boot/vmlinuz-3.10.0-229.el7.x86_64
Foundinitrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img
Foundlinux image: /boot/vmlinuz-0-rescue-1100f7e6c97d4afaad2e396403ba7f61
Foundinitrd image: /boot/initramfs-0-rescue-1100f7e6c97d4afaad2e396403ba7f61.img
Done

3)验证是否修改成功
[root@linux-node2~]# reboot                           #必须重启系统生效
[root@linux-node2~]# yum install net-tools                   #默认centos7不支持ifconfig 需要看装net-tools包
[root@linux-node2~]# ifconfig eth0                 #在次查看网卡信息
eth0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

2)网卡配置文件内容

[root@znix ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0    <==网卡的名字
HWADDR=00:0c:29:a8:e4:14   <==MAC地址 硬件地址
TYPE=Ethernet   <==以太网
NAME=eth0
UUID=a3b2265e-9dac-4a29-aff6-d2e88eb28cfc   <==系统里识别
ONBOOT=yes   <==开机时自动启动
NM_CONTROLLED=yes<== 是否受network程序管理
BOOTPROTO=none   <==通过指定方式的办法来获得地址,dhcp或者static
#dhcp 自动获取ip地址
#none 固定的ip地址
#static 固定的ip地址
IPADDR=10.0.0.201   <==服务器的ip地址
NETMASK=255.255.255.0   <==子网掩码
GATEWAY=10.0.0.2   <==网关
DNS2=119.29.29.29   <== dns
DNS1=114.114.114.114   <==dns

3)centos7启动网卡服务报错“RTNETLINK answers: File exists”的解决方法

[root@localhost network-scripts]# service NetworkManager stop
Starting network (via systemctl):  Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
 
[root@localhost network-scripts]# systemctl status network.service
?.network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network)
   Active: failed (Result: exit-code) since Mon 2017-03-20 18:41:03 CST; 3min 49s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18880 ExecStart=/etc/rc.d/init.d/network start (code=exited, status=1/FAILURE)
 
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain network[18880]: RTNETLINK answers: File exists
Mar 20 18:41:03 localhost.localdomain systemd[1]: network.service: control process exited, code=exited status=1
Mar 20 18:41:03 localhost.localdomain systemd[1]: Failed to start LSB: Bring up/down networking.
Mar 20 18:41:03 localhost.localdomain systemd[1]: Unit network.service entered failed state.
Mar 20 18:41:03 localhost.localdomain systemd[1]: network.service failed.
 
原因分析和解决:
在centos下出现该故障的原因是启动网络的两个服务/etc/init.d/network 和 /etc/init.d/NetworkManager有冲突。
从根本上说是NetworkMaganager(NM)的带来的冲突,停用NetworkManager即可解决。重启即可。
[root@localhost network-scripts]# service NetworkManager stop
Redirecting to /bin/systemctl stop  NetworkManager.service
[root@localhost network-scripts]# systemctl start network.service

三、SELINUX

linux环境下执行某些程序时,偶尔会遇到来一个关于selinux的强制模式不可执行的情况,
这种情况下需要关闭selinux或者将enforcing改为permissive模式后才能进行执行。
selinux是Linux内核中提供的强制访问控制(MAC)系统,下面就对selinux的几种模式及其转换进行总结:

selinux的启动、关闭与查看
1)目前selinux支持三种模式,分别如下:
•enforcing:强制模式,代表selinux运作中,且已经正确的开始限制 domain/type 了;
•permissive:宽容模式:代表selinux运作中,不过仅会有警告讯息并不会实际限制 domain/type 的存取。这种模式可以运来作为 selinux 的 debug 之用;
•disabled:关闭,selinux 并没有实际运作。
  
2)查看selinux的模式
[root@localhost ~]# getenforce
Enforcing       //显示出目前的模式为 Enforcing
  
3)查看 selinux的政策 (Policy)[root@localhost ~]# sestatus
selinux status: enabled      //是否启动 selinux
selinuxfs mount: /selinux     //selinux 的相关文件资料挂载点
Current mode: enforcing           //目前的模式
Mode from config file: enforcing     //设定档指定的模式
Policy version: 21
Policy from config file: targeted     //目前的政策为何?
  
4)通过配置文件调整selinux的参数
[root@localhost ~]# vim /etc/selinux/config
selinux=enforcing           //调整 enforcing|disabled|permissive
selinuxTYPE=targeted        //目前仅有 targeted 与 strict
  
5)selinux的启动与关闭
上面是预设的政策与启动的模式!需要注意的是,如果改变了政策则需要重新开机;如果由enforcing或permissive改成disabled,或由disabled改成其他两个,那也必须要重新开机。这是因为 selinux 是整合到核心里面去的, 你只可以在 selinux 运作下切换成为强制 (enforcing) 或宽容 (permissive) 模式,不能够直接关闭 selinux 的!
同时,由selinux关闭 (disable) 的状态到开启的状态也需要重新开机!
  
查看selinux状态:
a)/usr/sbin/sestatus -v    //如果selinux status参数为enabled即为开启状态
selinux status: enabled
b)getenforce       //也可以用这个命令检查
  
  
关闭selinux:
a)临时关闭(不用重启机器):
setenforce 0      //设置selinux 成为permissive宽容模式
setenforce 1      //设置selinux 成为enforcing强制模式
  
b)修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将selinux=enforcing改为selinux=disabled
重启机器即可
  
如果你要启动selinux的话,请将上述的selinux=enforcing设定妥当,并且指定selinuxTYPE=targeted 这一个设定,并且到/boot/grub/menu.lst这个文件去,看看核心有无关闭selinux。
[root@localhost ~]# vi /boot/grub/menu.lst
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-92.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-92.el5 ro root=LABEL=/1 rhgb quiet selinux=0
initrd /initrd-2.6.18-92.el5.img
  
如果要启动selinux ,则不可以出现 selinux=0的字样在kernel后面!
 
=======================================================
温馨提示一个坑点:
一般而言,要关闭服务器的selinux功能,常用下面两种方法:
1)临时关闭
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
 
2)永久关闭
[root@localhost ~]# cat /etc/sysconfig/selinux
SELINUX=disabled
[root@localhost ~]# reboot
 
之前碰到一个坑,如上设置SELINUX为disabled,并reboot重启服务器后,selinux并没有关闭!
 
最后发现原因是:
/etc/selinux/config文件里面的SELINUX值没有改过来,所以重启不生效!
一般而言,服务器系统安装后,/etc/sysconfig/selinux会默认为/etc/selinux/config的软链接,所以只需修改/etc/sysconfig/selinux文件中的SELINUX为disabled即可!
因为系统启动后默认会以/etc/selinux/config为主。
 
[root@localhost ~]# ll /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 17 Apr 27 02:41 /etc/sysconfig/selinux -> ../selinux/config
 
但之前碰到过的现象是: /etc/sysconfig/selinux 并不是/etc/selinux/config的软链接,所以这个细节要留意,如果不是软链接关系,就最好两个文件都修改下!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值