systemctl


使用systemctl命令,要记住start,stop,restart,status,enable,disable,is-enabled

systemctl可以看作是service和chkconfig的组合,CentOS的Services使用了systemd来代替sysvinit管理

job、chkconfig、service systemctl

服务开机自启动 chkconfig --level 3 httpd on systemctl enable httpd

服务不开机自启动 chkconfig --level 3 httpd off systemctl disable httpd

 

服务状态

service httpd status

systemctl status httpd(服务详细信息)

systemctl is-active httpd(只显示是否active)

 

所有服务的启动状态 chkconfig --list systemctl list-units --type=service

启动服务 service httpd start systemctl start httpd

停止服务 service httpd stop systemctl stop httpd

重启服务 service httpd restart systemctl restart httpd


httpd服务

systemctl enable httpd

ystemctl   stop   httpd.service

systemctl   start   httpd.service

systemctl   restart   httpd.service

systemctl   status  httpd.service





运行级别


如何查看当前运行级别?

旧的runlevel命令在systemd下仍然可以使用。可以继续使用它,尽管systemd使用 ‘target’ 概念(多个的 ‘target’ 可以同时激活)替换了之前系统的runlevel。等价的systemd命令是systemctl list-units –type=target


ls -ltr /lib/systemd/system/runlevel*.target


systemctl set-default multi-user.target


systemctl set-default graphical.target



修改系统运行级别:

1、systemd使用比sysvinit的运行级更为自由的target替代。第3运行级用multi-user.target替代。第5运行级用graphical.target替代。runlevel3.target和runlevel5.target分别是指向 multi-user.target和graphical.target的符号链接。

可以使用下面的命令切换到“运行级别3 ”:

systemctl isolate multi-user.target或systemctl isolate runlevel3.target

可以使用下面的命令切换到“运行级别5 ”:

systemctl isolate graphical.target或systemctl isolate runlevel5.target

2、如何改变默认运行级别?

systemd使用链接来指向默认的运行级别。在创建新的链接前,可以通过下面命令删除存在的链接: rm /etc/systemd/system/default.target

默认启动运行级别3 :

ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target

默认启动运行级别5 :

ln -sf /lib/systemd/system/graphical.target/etc/systemd/system/default.target

systemd不使用/etc/inittab文件。

3、如何查看当前运行级别?

旧的runlevel命令在systemd下仍然可以使用。可以继续使用它,尽管systemd使用 ‘target’ 概念(多个的 ‘target’ 可以同时激活)替换了之前系统的runlevel。

等价的systemd命令是systemctl list-units –type=target



防火墙

service firewalld stop

 

systemctl   disabled   firewalld.service


systemctl mask firewalld


systemctl stop firewalld


firewall-cmd --zone=public --add-port=5901-5910/tcp --permanent 关闭防火墙

firewall-cmd --reload 重启





回归iptables

 yum -y install iptables-services

 systemctl enable iptables

 systemctl enable ip6tables

 systemctl start iptables

 systemctl start ip6tables

iptables -I INPUT -p tcp --dport 5901 -j ACCEPT    手动开启端口号


centos7 关闭firewall安装iptables并配置


配置防火墙,开启80端口、3306端口

CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

 

2、安装iptables防火墙

yum install iptables-services #安装

vi /etc/sysconfig/iptables #编辑防火墙配置文件

# Firewall configuration written by system-config-firewall 

# Manual customization of this file is not recommended.

*filter

:INPUT ACCEPT [0:0]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [0:0]

-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

-A INPUT -p icmp -j ACCEPT

-A INPUT -i lo -j ACCEPT IT网,http://www.it.net.cn

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited

-A FORWARD -j REJECT --reject-with icmp-host-prohibited

COMMIT

:wq! #保存退出

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动


 

关闭SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加 

:wq! #保存退出

setenforce 0 #使配置立即生效

l



selinux


我想到的:sed  -i   “/selinux/enforcing/disabled/” /etc/selinux/selinux.conf


实际上:sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config  


在安装Cobbler和Puppet时需要关闭selinux,但是通常情况下载安装完CentOS7后,默认情况下SElinux是启用状态,

如下所示:

[csharp] view plaincopy

 

[root@rdo ~]# sestatus  

SELinux status:                 enabled  

SELinuxfs mount:                /sys/fs/selinux  

SELinux root directory:         /etc/selinux  

Loaded policy name:             targeted  

Current mode:                   enforcing  

Mode from config file:          enforcing  

Policy MLS status:              enabled  

Policy deny_unknown status:     allowed  

Max kernel policy version:      28  

1、如果要临时关闭,可以执行

[cpp] view plaincopy

 

setenforce 0  

此时的状态如下

[html] view plaincopy

 

[root@rdo ~]# sestatus  

SELinux status:                 enabled  

SELinuxfs mount:                /sys/fs/selinux  

SELinux root directory:         /etc/selinux  

Loaded policy name:             targeted  

Current mode:                   permissive  

Mode from config file:          enforcing  

Policy MLS status:              enabled  

Policy deny_unknown status:     allowed  

Max kernel policy version:      28  

2、如果要永久关闭,可以修改配置文件/etc/selinux/config,将SELINU置为disabled。

[html] view plaincopy

 

[root@rdo ~]# cat /etc/selinux/config   

  

# This file controls the state of SELinux on the system.  

# SELINUX= can take one of these three values:  

#     enforcing - SELinux security policy is enforced.  

#     permissive - SELinux prints warnings instead of enforcing.  

#     disabled - No SELinux policy is loaded.  

#SELINUX=enforcing  

SELINUX=disabled  

# SELINUXTYPE= can take one of three two values:  

#     targeted - Targeted processes are protected,  

#     minimum - Modification of targeted policy. Only selected processes are protected.   

#     mls - Multi Level Security protection.  

SELINUXTYPE=targeted   


修改该配置文件也可以执行下面的命令来完成

[html] view plaincopy

 

sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config  

修改完成后,保存重启,重启后状态如下:

[html] view plaincopy

 

[root@rdo ~]# sestatus  

SELinux status:                 disabled  

 



主机名

         在CentOS或RHEL中,有三种定义的主机名:a、静态的(static),b、瞬态的(transient),以及 c、灵活的(pretty)。“静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名。“瞬态”主机名是在系统运行时临时分配的主机名,例如,通过DHCP或mDNS服务器分配。静态主机名和瞬态主机名都遵从作为互联网域名同样的字符限制规则。而另一方面,“灵活”主机名则允许使用自由形式(包括特殊/空白字符)的主机名,以展示给终端用户(如Dan's Computer)。


在CentOS/RHEL 7中,有个叫hostnamectl的命令行工具,它允许你查看或修改与主机名相关的配置。

要查看主机名相关的设置:

$ hostnamectl status


只查看静态、瞬态或灵活主机名,分别使用“--static”,“--transient”或“--pretty”选项。

$ hostnamectl status [--static|--transient|--pretty]

要同时修改所有三个主机名:静态、瞬态和灵活主机名:

$ sudo hostnamectl set-hostname <host-name>


就像上面展示的那样,在修改静态/瞬态主机名时,任何特殊字符或空白字符会被移除,而提供的参数中的任何大写字母会自动转化为小写。一旦修改了静态主机名,/etc/hostname 将被自动更新。然而,/etc/hosts 不会更新以保存所做的修改,所以你需要手动更新/etc/hosts。

如果你只想修改特定的主机名(静态,瞬态或灵活),你可以使用“--static”,“--transient”或“--pretty”选项。

例如,要永久修改主机名,你可以修改静态主机名:

$ sudo hostnamectl --staticset-hostname <host-name>

注意,你不必重启机器以激活永久主机名修改。上面的命令会立即修改内核主机名。注销并重新登入后在命令行提示来观察新的静态主机名。


日志


To get the content of the Systemd journal, type:

# journalctl

To get all the events related to the crond process in the journal, type:

# journalctl /sbin/crond

Note: You can replace /sbin/crond by `which crond`.

To get all the events since the last boot, type:

# journalctl -b

To get all the events that appeared today in the journal, type:

# journalctl --since=today

To get all the events with a syslog priority of err, type:

# journalctl -p err

To get the 10 last events and wait for any new one (like “tail -f /var/Log/messages”), type:

# journalctl -f

Note: You will find additional information on this point in the Lennart Poettering’s blog or Lennart Poettering’s video (44min: the first ten minutes are very interesting concerning security issues).


网络

       Red Hat Enterprise Linux 7 与 CentOS 7 中默认的网络服务由 NetworkManager 提供,这是动态控制及配置网络的守护进程,它用于保持当前网络设备及连接处于工作状态,同时也支持传统的 ifcfg 类型的配置文件。

NetworkManager 可以用于以下类型的连接:Ethernet,VLANS,Bridges,Bonds,Teams,Wi-Fi,mobile boradband(如移动3G)以及 IP-over-InfiniBand。针对与这些网络类型,NetworkManager 可以配置他们的网络别名,IP 地址,静态路由,DNS,×××连接以及很多其它的特殊参数。

可以用命令行工具 nmcli 来控制 NetworkManager。


nmcli 用法

# nmcli [ OPTIONS ] OBJECT { COMMAND | help }

我们可以通过 TAB 键补全命令,当你忘记这个命令的语法时只需要按下 TAB 就可以看到选项列表。


nmcli tab

使用 nmcli 的一些例子:

# nmcli general status

这条命令将 NetworkManager 的所有状态都打印出来。

# nmcli connection show

显示所有连接。

# nmcli connection show -a

仅显示当前活动的连接。

# nmcli device status

列出 NetworkManager 识别出的设备列表及他们的状态。


nmcli general

启动/停止 网络接口


使用 nmcli 工具启动或停止网络接口,与 ifconfig 的 up/down 是一样的。

使用下列命令停止某个接口:

# nmcli device disconnect eno16777736

下列命令用来启动接口:

# nmcli device connect eno16777736

添加静态IP的以太网连接


以下命令可以添加一个静态IP地址的以太网连接:

# nmcli connection add type ethernet con-name NAME_OF_CONNECTION ifname interface-name ip4 IP_ADDRESS gw4 GW_ADDRESS

根据你需要的配置更改 NAME_OF_CONNECTION,IP_ADDRESS, GW_ADDRESS 参数(如果不需要网关的话可以省略最后一部分)。

# nmcli connection add type ethernet con-name NEW ifname eno16777736 ip4 192.168.1.141 gw4 192.168.1.1

使用下列命令设置DNS服务器:

# nmcli connection modify NEW ipv4.dns "8.8.8.8 8.8.4.4"

下列命令启动新的 Ethernet 连接:

# nmcli connection up NEW ifname eno16777736

查看新连接的配置信息:

# nmcli -p connection show NEW


nmcli add static

增加一个使用 DHCP 的新连接


增加新的连接,使用DHCP自动分配IP地址,网关,DNS等,你要做的就是将命令行后 ip/gw 地址部分去掉就行了,DHCP会自动分配这些参数。

例,在 eno 16777736 设备上配置一个 名为 NEW_DHCP 的 DHCP 连接

# nmcli connection add type ethernet con-name NEW_DHCP ifname eno16777736


首先发现ifconfig、netstat、route、arp都没有了,在哪儿呢?

[root@centos7 ~]# yum search ifconfig

......

======================== Matched: ifconfig =========================

net-tools.x86_64 : Basic networking tools

[root@centos7 ~]# 

 哦,最小安装默认没有包含这些老工具,如果非用不可,就 yum install net-tools 吧,但我就是要看看不用它们我们怎么来管理网络。

我们将要用到的是ip指令,ss指令和NetworkManager的两个工具 nmtui,nmcli。老实说,这几个工具更加强大了,但还真不太容易习惯呢。

setup和ntsysv工具还是保留了