Linux网络管理和crontab定时任务配置

主机名配置

1.查看主机名

[root@centos6-1 ~]# hostname 
centos6-1
[root@centos6-1 ~]# 

2.修改主机名

[root@centos6-1 ~]# hostname haha
[root@centos6-1 ~]# hostname 
haha
[root@centos6-1 ~]# 

以上只是临时修改,永久修改需要修改/ect/sysconfig/network文件

IP地址配置

1.setup
用 root 输入 setup 命令,进入交互式修改界面,Minimal 安装的系统没有安装 setuptool 软件,可 yum 安装后使用。
2.修改配置文件(永久生效)

[root@centos6-1 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
[root@centos6-1 ~]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Determining if ip address 192.168.214.150 is already in use for device eth0...
                                                           [  OK  ]
[root@centos6-1 ~]#

3.ifconfig 命令(重启后无效)

[root@centos6-1 ~]# ifconfig eth0 192.168.214.222

主机名映射

etc/hosts 文件用于在通过主机名进行访问时做 ip 地址解析之用。 所以,你想访问一个什么样的主机名,就需要把这个主机名和它对应的 ip 地址 配置在/etc/hosts 文件中。

网络端口监听

netstat 是一款命令行工具,用于列出系统上所有的网络 socket 连接情况, 包括 tcp, udp 以及 unix socket,另外它还能列出处于监听状态(即等待接入 请求)的socket。如想确认Web 服务有没有起来,可查看80端口有没有打开。
常见参数:

参数 解释
-a (all)显示所有选项,默认不显示 LISTEN 相关
-t (tcp)仅显示 tcp 相关选项
-u (udp)仅显示 udp 相关选项
-n 禁用域名反向解析功能,只显示 ip
-l 仅列出有在 Listen (监听) 的服务状态
-p 显示建立相关链接的进程信息
-ep 可以同时查看进程名和用户名
[root@centos6-1 ~]# netstat -lnp|grep 80
unix  2      [ ACC ]     STREAM     LISTENING     14080  1594/cupsd          /var/run/cups/cups.sock
[root@centos6-1 ~]# 

crontab

1.简介

crontab 是 Unix 和 Linux 用于设置周期性被执行的指令。通过 crontab 命 令,可以在固定间隔时间执行指定的系统指令或 shell 脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合,最小只能是分。

2.命令格式

usage:  crontab [-u user] file
        crontab [-u user] [ -e | -l | -r ]
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)
        -s      (selinux context)

参数说明:

参数 解释
-u user 用来设定某个用户的 crontab 服务
file file 是命令文件的名字,表示将 file 做为 crontab 的任务列表文件 并载入 crontab
-e 编辑某个用户的 crontab 文件内容。如果不指定用户,则表示编辑当前用户的 crontab 文件。
-l 显示某个用户的 crontab 文件内容。如果不指定用户,则表示显示当前用户的 crontab 文件内容。
-r 删除定时任务配置,从/var/spool/cron 目录中删除某个用户的 crontab文件,如果不指定用户,则默认删除当前用户的 crontab 文件。
-i 在删除用户的 crontab 文件时给确认提示。
crontab file [-u user] ## 用指定的文件替代目前的 crontab
crontab -l [-u user] ## 列出用户目前的 crontab
crontab -e [-u user] ## 编辑用户目前的 crontab

3.crontab任务配置

* * * * * command
分 时 日 月 周 命令
第 1 列表示分钟 1~59 每分钟用或者 /1 表示 第 2 列表示小时 0~23(0 表示 0 点)
第 3 列表示日期 1~31
第 4 列表示月份 1~12
第 5 列标识号星期 0~6(0 表示星期天)
第 6 列要运行的命令

配置实例如下

扫描二维码关注公众号,回复: 2644131 查看本文章
*/1 * * * * date >> /root/date.txt  //每分钟执行一次 date 命令
30 21 * * * /usr/local/etc/rc.d/httpd restart  //每晚的 21:30 重启 apache。
45 4 1,10,22 * * /usr/local/etc/rc.d/httpd restart  //每月 1、10、22 日的 4 : 45 重启 apache。
10 1 * * 6,0 /usr/local/etc/rc.d/httpd restart  //每周六、周日的 1 : 10 重启 apache。
0,30 18-23 * * * /usr/local/etc/rc.d/httpd restart  //每天18 : 00至23 : 00之间每隔30分钟重启apache。 
* 23-7/1 * * * /usr/local/etc/rc.d/httpd restart  //晚上 11 点到早上 7 点之间,每隔一小时重启 apache

猜你喜欢

转载自blog.csdn.net/weixin_37490221/article/details/80849163