运维部落

香港vps促销1G99元 点击购买 美国vps促销512M59元 点击购买 日本vps促销1G100元 点击购买

如何使用命令方式检测mx记录是否生效

2017年3月3日 没有评论

主要命令就只有以下2个
nslookup
set querytype=mx

第一步输入:nslookup
第二步输入:set querytype=mx
第三步输入:你的域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:\Users\Administrator>nslookup
默认服务器:  dnspai-public-dns.dnspai.com
Address:  101.226.4.6
 
> set querytype=mx
> qq.com
服务器:  dnspai-public-dns.dnspai.com
Address:  101.226.4.6
 
非权威应答:
qq.com  MX preference = 20, mail exchanger = mx2.qq.com
qq.com  MX preference = 30, mail exchanger = mx1.qq.com
qq.com  MX preference = 10, mail exchanger = mx3.qq.com
>
分类: 解决方案 标签: , ,

Windows环境下安装Redis(转)

2017年2月18日 没有评论

1:首先下载redis。
从下面地址下:https://github.com/MSOpenTech/redis/releases
2:创建redis.conf文件:
这是一个配置文件,指定了redis的监听端口,timeout等。如下面有:port 6379。

配置:

遇到问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[18892] 05 Jan 16:02:28.584 #
The Windows version of Redis allocates a memory mapped heap for sharing with
the forked process used for persistence operations. In order to share this
memory, Windows allocates from the system paging file a portion equal to the
size of the Redis heap. At this time there is insufficient contiguous free
space available in the system paging file for this operation (Windows error
0x5AF). To work around this you may either increase the size of the system
paging file, or decrease the size of the Redis heap with the --maxheap flag.
Sometimes a reboot will defragment the system paging file sufficiently for
this operation to complete successfully.
Please see the documentation included with the binary distributions for more
details on the --maxheap flag.
Redis can not continue. Exiting.

处理方法:

1
2
3
4
windows硬盘需要配置虚拟内存,如果还有问题,清理磁盘碎片
redis.windows.conf
maxheap 1024000000
daemonize no

更改redis的配置需要修改redis.conf文件,以下是它一些主要的配置注释:

#是否作为守护进程运行
daemonize no
#Redis 默认监听端口
port 6379
#客户端闲置多少秒后,断开连接
timeout 300
#日志显示级别
loglevel verbose
#指定日志输出的文件名,也可指定到标准输出端口
logfile redis.log
#设置数据库的数量,默认最大是16,默认连接的数据库是0,可以通过select N 来连接不同的数据库
databases 32
#Dump持久化策略
#当有一条Keys 数据被改变是,900 秒刷新到disk 一次
#save 900 1
#当有10 条Keys 数据被改变时,300 秒刷新到disk 一次
save 300 100
#当有1w 条keys 数据被改变时,60 秒刷新到disk 一次
save 6000 10000
#当dump     .rdb 数据库的时候是否压缩数据对象
rdbcompression yes
#dump 持久化数据保存的文件名
dbfilename dump.rdb
###########    Replication #####################
#Redis的主从配置,配置slaveof则实例作为从服务器
#slaveof 192.168.0.105 6379
#主服务器连接密码
# masterauth <master-password>
############## 安全性 ###########
#设置连接密码
#requirepass <password>
############### LIMITS ##############
#最大客户端连接数
# maxclients 128
#最大内存使用率
# maxmemory <bytes>
########## APPEND ONLY MODE #########
#是否开启日志功能
appendonly no
# AOF持久化策略
#appendfsync always
#appendfsync everysec
#appendfsync no
################ VIRTUAL MEMORY ###########
#是否开启VM 功能
#vm-enabled no
# vm-enabled yes
#vm-swap-file logs/redis.swap
#vm-max-memory 0
#vm-page-size 32
#vm-pages 134217728
#vm-max-threads 4

主从复制

在从服务器配置文件中配置slaveof ,填写服务器IP及端口即可,如果主服务器设置了连接密码,在masterauth后指定密码就行了。

持久化

  • redis提供了两种持久化文案,Dump持久化和AOF日志文件持久化。
  • Dump持久化是把内存中的数据完整写入到数据文件,由配置策略触发写入,如果在数据更改后又未达到触发条件而发生故障会造成部分数据丢失。
  • AOF持久化是日志存储的,是增量的形式,记录每一个数据操作动作,数据恢复时就根据这些日志来生成。

 

3.命令行操作

使用CMD命令提示符,打开redis-cli连接redis服务器 ,也可以使用telnet客户端

# redis-cli -h 服务器 –p 端口 –a 密码

redis-cli.exe -h 127.0.0.1 -p 6379

连接成功后,就可对redis数据增删改查了,如字符串操作:

以下是一些服务器管理常用命令:

info   #查看服务器信息
select <dbsize> #选择数据库索引  select 1
flushall #清空全部数据
flushdb  #清空当前索引的数据库
slaveof <服务器> <端口>  #设置为从服务器
slaveof no one #设置为主服务器
shutdown  #关闭服务

 

附加几个 bat 批处理脚本,请根据需要灵活配置

1
2
3
4
5
6
7
8
service-install.bat
redis-server.exe --service-install redis.windows.conf --loglevel verbose 
uninstall-service.bat
redis-server --service-uninstall
 
startup.bat
redis-server.exe redis.windows.conf 
分类: Windows, 解决方案 标签:

linux下使用stat获取文件及目录对应权限的数字

2017年2月11日 没有评论

经常在想了解比如文件属性为-rw-r–r– 对应权限为644,如何使用命令获取权限对应的数字??

举例如下:

[linuxidc@localhost ~]$ ll -l
-rw-r–r– 1 linuxidc wheel 38 Oct 12 16:29 1.txt

使用stat命令可以查看
[linuxidc@localhost ~]$ stat 1.txt
File: `1.txt’
Size: 38 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 390954 Links: 1
Access: (0644/-rw-r–r–) Uid: ( 503/ linuxidc) Gid: ( 10/ wheel)
Access: 2015-10-12 16:29:34.674990005 +0800
Modify: 2015-10-12 16:29:32.248990536 +0800
Change: 2015-10-12 16:29:32.248990536 +0800

取出对应的数字则需要使用正则sed awk 或cut ,head,tail命令;

方法1:使用正则或命令取
head,tail,cut

[linuxidc@localhost ~]$ stat 1.txt |head -n4|tail -n1|cut -d “/” -f1|cut -d “(” -f2
0644

sed,cut

[linuxidc@localhost ~]$ stat 1.txt |sed -n ’4p’|cut -d “/” -f1|cut -d “(” -f2
0644

sed,awk

[linuxidc@localhost ~]$ stat 1.txt |sed -n ’4p’|awk -F”/” ‘{print $1}’|awk -F”(” ‘{print $2}’
0644

方法2:stat -c 命令

[linuxidc@localhost ~]$ stat -c %a 1.txt
644

注意:如何想到法二的思考过程,比答题更重要。当命令结果包含我们需要的内容的时候,我们要想到是否有具体的参数能够一步达到我们需要的结果。

man stat 查看帮助
-c –format=FORMAT
use the specified FORMAT instead of the default; output a new line after each use of FORMAT
使用特殊格式代替默认输出;
常用的参数有如下:
%a Access rights in octal 8进制显示访问权限,0644
%A Access rights in human readable form 以人类可读的形式输出,
%F File type 文件的类型
%g Group ID of owner 所属组gid的号码
%G Group name of owner 所属组的名称
%h Number of hard links 硬连接的数量
%i Inode number inode的值
%n File name 文件名
%o I/O block size IO块大小
%s Total size, in bytes 文件的总大小,字节显示;
%u User ID of owner 所属主的uid号码
%U User name of owner 所属主的名称
%x Time of last access 最后访问的时间
%X Time of last access as seconds since Epoch 最后访问时间的时间戳
%y Time of last modification 最后修改的时间
%Y Time of last modification as seconds since Epoch 最后修改时间的时间戳
%z Time of last change 最后更改的时间
%Z Time of last change as seconds since Epoch 最后更改的时间的时间戳

使用参数结果如下:
[linuxidc@localhost ~]$ ls -l 1.txt
-rw-r–r– 1 linuxidc wheel 38 Oct 12 16:29 1.txt
[linuxidc@localhost ~]$ stat -c %a 1.txt
644
[linuxidc@localhost ~]$ stat -c %A 1.txt
-rw-r–r–
[linuxidc@localhost ~]$ stat -c %b 1.txt
8
[linuxidc@localhost ~]$ stat -c %B 1.txt
512
[linuxidc@localhost ~]$ stat -c %d 1.txt
64768
[linuxidc@localhost ~]$ stat -c %F 1.txt
regular file
[linuxidc@localhost ~]$ stat -c %g 1.txt
10
[linuxidc@localhost ~]$ stat -c %G 1.txt
wheel
[linuxidc@localhost ~]$ stat -c %u 1.txt
503
[linuxidc@localhost ~]$ stat -c %U 1.txt
linuxidc
[baby@localhost ~]$ stat -c %h 1.txt
1
[linuxidc@localhost ~]$ stat -c %i 1.txt
390954
[linuxidc@localhost ~]$ stat -c %n 1.txt
1.txt
[linuxidc@localhost ~]$ stat -c %o 1.txt
4096
[linuxidc@localhost ~]$ stat -c %s 1.txt
38

分类: Linux, 解决方案 标签:

Linux下脚本上传文件到dropbox

2017年2月3日 没有评论

这里介绍一个可以上传文件到dropbox的脚本。不用安装,直接运行即可把文件上传到dropbox。
脚本地址:https://github.com/andreafabrizi/Dropbox-Uploader
也可以在本站直接下载:dropbox_uploader.sh
脚本使用方法:
语法:./dropbox_uploader.sh [OPTIONS]…
选项:-u [USERNAME] dropbox用户
-p [PASSWORD] dropbox密码
-f [FILE/FOLDER] 待上传的文件
-d [REMOTE_FOLDER] dropbox的目录,默认是 “/”
-v 返回详细进程模式
例子:
./dropbox_uploader.sh -u andrea.fabrizi@gmail.com -f /etc/passwd -v
./dropbox_uploader.sh -u andrea.fabrizi@gmail.com -f /var/backup/ -v
也可以在dropbox_uploader.sh文件填写好用户和密码,之后运行脚本时就不用再定义用户和密码。

ps:更新一下,最新的版本已经改变为需要设置一个密钥已经不需要用户密码了更为安全:

第一次先运行先进行下载程序:

curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
chmod 777 dropbox_uploader.sh
./dropbox_uploader.sh
会要求你输入
# Access token:
这个你需要登陆https://www.dropbox.com/developers/apps进行创建一个app的api支持,名字随便起。
在找到点击Generated access token后会有一大串的密钥复制到# Access token:后再运行以下示例命令就可以上传文件了。
注意:如果这地方输入错误的话,需要把这个脚本文件目录下.dropbox_uploader的文件删除,然后再运行文件输入正确的密钥才可以不然上传肯定是失败的。

上传命令示例:

Examples:

    ./dropbox_uploader.sh upload /etc/passwd /myfiles/passwd.old
    ./dropbox_uploader.sh upload *.zip /
    ./dropbox_uploader.sh download /backup.zip
    ./dropbox_uploader.sh delete /backup.zip
    ./dropbox_uploader.sh mkdir /myDir/
    ./dropbox_uploader.sh upload "My File.txt" "My File 2.txt"
    ./dropbox_uploader.sh share "My File.txt"
    ./dropbox_uploader.sh list

 

linux之sed用法

2017年1月4日 没有评论

sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换、删除、新增、选取等特定工作,下面先了解一下sed的用法
sed命令行格式为:
sed [-nefri] ‘command’ 输入文本

常用选项:
-n∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
-e∶直接在指令列模式上进行 sed 的动作编辑;
-f∶直接将 sed 的动作写在一个档案内, -f filename 则可以执行 filename 内的sed 动作;
-r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
-i∶直接修改读取的档案内容,而不是由萤幕输出。

常用命令:
a ∶新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~
s ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

举例:(假设我们有一文件名为ab)
删除某行
[root@localhost ruby] # sed ’1d’ ab #删除第一行
[root@localhost ruby] # sed ‘$d’ ab #删除最后一行
[root@localhost ruby] # sed ’1,2d’ ab #删除第一行到第二行
[root@localhost ruby] # sed ’2,$d’ ab #删除第二行到最后一行

  显示某行
. [root@localhost ruby] # sed -n ’1p’ ab #显示第一行
[root@localhost ruby] # sed -n ‘$p’ ab #显示最后一行
[root@localhost ruby] # sed -n ’1,2p’ ab #显示第一行到第二行
[root@localhost ruby] # sed -n ’2,$p’ ab #显示第二行到最后一行

  使用模式进行查询
[root@localhost ruby] # sed -n ‘/ruby/p’ ab #查询包括关键字ruby所在所有行
[root@localhost ruby] # sed -n ‘/\$/p’ ab #查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义

  增加一行或多行字符串
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ’1a drink tea’ ab #第一行后增加字符串”drink tea”
Hello!
drink tea
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ’1,3a drink tea’ ab #第一行到第三行后增加字符串”drink tea”
Hello!
drink tea
ruby is me,welcome to my blog.
drink tea
end
drink tea
[root@localhost ruby] # sed ’1a drink tea\nor coffee’ ab #第一行后增加多行,使用换行符\n
Hello!
drink tea
or coffee
ruby is me,welcome to my blog.
end

  代替一行或多行
[root@localhost ruby] # sed ’1c Hi’ ab #第一行代替为Hi
Hi
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed ’1,2c Hi’ ab #第一行到第二行代替为Hi
Hi
end

  替换一行中的某部分
  格式:sed ‘s/要替换的字符串/新的字符串/g’ (要替换的字符串可以用正则表达式)
[root@localhost ruby] # sed -n ‘/ruby/p’ ab | sed ‘s/ruby/bird/g’ #替换ruby为bird
  [root@localhost ruby] # sed -n ‘/ruby/p’ ab | sed ‘s/ruby//g’ #删除ruby

插入
[root@localhost ruby] # sed -i ‘$a bye’ ab #在文件ab中最后一行直接输入”bye”
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
bye

删除匹配行

sed -i ‘/匹配字符串/d’ filename (注:若匹配字符串是变量,则需要“”,而不是‘’。记得好像是)

替换匹配行中的某个字符串

sed -i ‘/匹配字符串/s/替换源字符串/替换目标字符串/g’ filename

CentOS openssh升级到openssh-7.4版本

2017年1月2日 没有评论

最近又暴出最新的漏洞。太可怕了。只能升级了。查了下大神们的升级操作。发出来给大家共享一下吧。

环境:
cat /etc/issue
CentOS release 6.5 (Final)

ssh -V
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013

openssl version -a
OpenSSL 1.0.1e-fips 11 Feb 2013

一、准备
备份ssh目录(重要)
cp -rf /etc/ssh /etc/ssh.bak

【 可以现场处理的,不用设置
安装telnet,避免ssh升级出现问题,导致无法远程管理(此步其实可以不用,测试过没必要因为ssh不会中断)
yum install telnet-server

vi /etc/xinetd.d/telnet
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
}

默认不允许root登录

vi /etc/securetty
增加
pts/0
pts/1
pts/2
如果登录用户较多,需要更多的pts/*

/etc/init.d/xinetd restart
这样root可以telnet登录了

ssh升级后建议再修改回还原设置

二、安装
升级需要几个组件
yum install -y gcc openssl-devel pam-devel rpm-build

现在新版本,目前是openssh-7.4最新
wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-7.4p1.tar.gz

解压升级包,并安装
tar -zxvf openssh-7.4p1.tar.gz
cd openssh-7.4p1
./configure –prefix=/usr –sysconfdir=/etc/ssh –with-pam –with-zlib –with-md5-passwords –with-tcp-wrappers
make && make install

安装后提示:
/etc/ssh/ssh_config already exists, install will not overwrite
/etc/ssh/sshd_config already exists, install will not overwrite
/etc/ssh/moduli already exists, install will not overwrite
ssh-keygen: generating new host keys: ECDSA ED25519
/usr/sbin/sshd -t -f /etc/ssh/sshd_config
/etc/ssh/sshd_config line 81: Unsupported option GSSAPIAuthentication
/etc/ssh/sshd_config line 83: Unsupported option GSSAPICleanupCredentials

修改配置文件,允许root登录

vi /etc/ssh/sshd_config
#PermitRootLogin yes
修改为
PermitRootLogin yes

命令:
sed -i ‘/^#PermitRootLogin/s/#PermitRootLogin yes/PermitRootLogin yes/’ /etc/ssh/sshd_config

重启openSSH
service sshd restart

升级后版本
ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.1e-fips 11 Feb 2013

如果之前你将原ssh目录修改名字
mv /etc/ssh /etc/ssh_bak

需要修改下配置:
修改配置文件,禁止root登录
sed -i ‘/^#PermitRootLogin/s/#PermitRootLogin yes/PermitRootLogin no/’ /etc/ssh/sshd_config

可以不操作,禁止dns解析
sed -i ‘/^#UseDNS yes/s/#UseDNS yes/UseDNS no/’ /etc/ssh/sshd_config

可以不操作默认是22,修改ssh端口至6022
echo “Port 6022″ >> /etc/ssh/sshd_config

注:在升级SSH时你的SSH是不会因为升级或重启服务而断掉的.

问题1:
[root@testserver2 tmp]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd: /etc/ssh/sshd_config line 81: Unsupported option GSSAPIAuthentication
/etc/ssh/sshd_config line 83: Unsupported option GSSAPICleanupCredentials [ OK ]

解决:
将/etc/ssh/sshd_config文件中以上行数内容注释下即可
sed -i ‘/^GSSAPICleanupCredentials/s/GSSAPICleanupCredentials yes/#GSSAPICleanupCredentials yes/’ /etc/ssh/sshd_config
sed -i ‘/^GSSAPIAuthentication/s/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/’ /etc/ssh/sshd_config
sed -i ‘/^GSSAPIAuthentication/s/GSSAPIAuthentication no/#GSSAPIAuthentication no/’ /etc/ssh/sshd_config

问题2:
更新后ssh有如下提示,但不影响使用:
[root@testserver2 tmp]# ssh 10.111.32.51
/etc/ssh/ssh_config line 50: Unsupported option “gssapiauthentication”

解决:
可以注释/etc/ssh/ssh_config的gssapiauthentication内容

——————————————————————————————
CentOS7升级openssh参考这里的内容
本次使用源码安装(系统需要gcc),各软件版本如下(例):

zlib-1.2.8
openssl-1.0.2h
openssh-7.3p1

安装步骤如下:

1、安装zlib
[root@CentOS7test ~]# cd zlib-1.2.8/
[root@CentOS7test zlib-1.2.8]# ./configure
[root@CentOS7test zlib-1.2.8]# make
[root@CentOS7test zlib-1.2.8]# make install

2、安装openssl
[root@CentOS7test ~]# cd openssl-1.0.2h/
[root@CentOS7test openssl-1.0.2h]# ./config –prefix=/usr/ –shared
[root@CentOS7test openssl-1.0.2h]# make
[root@CentOS7test openssl-1.0.2h]# make install

3、安装openssh
[root@CentOS7test ~]# cd openssh-7.3p1/
[root@CentOS7test openssh-7.3p1]# ./configure –prefix=/usr/local –sysconfdir=/etc/ssh –with-pam –with-zlib –with-md5-passwords –with-tcp-wrappers
[root@CentOS7test openssh-7.3p1]# make
[root@CentOS7test openssh-7.3p1]# make install

4、查看版本是否已更新
[root@CentOS7test openssh-7.3p1]# ssh -V
OpenSSH_7.3p1, OpenSSL 1.0.2h 3 May 2016

5、新介质替换原有内容
[root@CentOS7test openssh-7.3p1]# mv /usr/bin/ssh /usr/bin/ssh_bak
[root@CentOS7test openssh-7.3p1]# cp /usr/local/bin/ssh /usr/bin/ssh
[root@CentOS7test openssh-7.3p1]# mv /usr/sbin/sshd /usr/sbin/sshd_bak
[root@CentOS7test openssh-7.3p1]# cp /usr/local/sbin/sshd /usr/sbin/sshd

6-加载ssh配置重启ssh服务
[root@CentOS7test ~]# systemctl daemon-reload
[root@CentOS7test ~]# systemctl restart sshd.service

7、遇到的问题解决

问题1:
安装完成后,telnet 22端口不通,通过systemctl status sshd.service查看发现有警告信息
部分信息如Permissions 0640 for ‘/etc/ssh/ssh_host_ecdsa_key’ are too open

修正:
修改相关提示文件的权限为600,并重启sshd服务(systemctl restart sshd.service)
查看服务状态(systemctl status sshd.service)
例:chmod 600 /etc/ssh/ssh_host_ecdsa_key

问题2:
安装完成后,如需root直接登录

修正:
修改/etc/ssh/sshd_config文件,将文件中#PermitRootLogin yes改为PermitRootLogin yes
并重启sshd服务
升级后验证

问题3:
如果你使用了jenkins进行部署,升级后会影响jenkins部署,测试连接web端会报错 Algorithm negotiation fail
修正:
在web端修改sshd_config文件最后一行增加以下内容
KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
参考:http://stackoverflow.com/questions/32627998/algorithm-negotiation-fail-in-jenkins
————————————————————–

【临时修改版本号,运行很久的线上环境升级存在风险,如果可以的话只修改版本号吧(后期经过验证,这种修改版本号的方法无效,ssh -v IP可以查看版本)
查询
ssh -V
sshd -V
备份
cp /usr/bin/ssh /usr/bin/ssh.bak.version_edit
cp /usr/sbin/sshd /usr/sbin/sshd.bak.version_edit
修改
sed -i ‘s#OpenSSH_5.3p1#OpenSSH_7.2p1#g’ /usr/bin/ssh
sed -i ‘s#OpenSSH_5.3p1#OpenSSH_7.2p1#g’ /usr/sbin/sshd

补充汇总下:
centos7.X主机升级ssh
cp /usr/bin/ssh /usr/bin/ssh.bak.20161124
cp /usr/sbin/sshd /usr/bin/sshd.bak.20161124
mv /etc/ssh /etc/ssh.bak
—下载包、安装gcc 、编译等中间步骤参上边内容—
make && make install
/usr/sbin/sshd -t -f /etc/ssh/sshd_config
echo ‘PermitRootLogin yes’ >> /etc/ssh/sshd_config
cp /etc/ssh.bak/sshd_config /etc/ssh/sshd_config 将原来的文件覆盖下这个新生成的内容
/bin/systemctl restart sshd.service

centos6.X升级ssh
cp /usr/bin/ssh /usr/bin/ssh.bak.20161124
cp /usr/sbin/sshd /usr/bin/sshd.bak.20161124
cp -rf /etc/ssh /etc/ssh.bak
—下载包、安装gcc 、编译等中间步骤参上边内容—
make && make install
sed -i ‘/^#PermitRootLogin/s/#PermitRootLogin yes/PermitRootLogin yes/’ /etc/ssh/sshd_config
sed -i ‘/^GSSAPICleanupCredentials/s/GSSAPICleanupCredentials yes/#GSSAPICleanupCredentials yes/’ /etc/ssh/sshd_config
sed -i ‘/^UsePAM/s/UsePAM yes/#UsePAM yes/’ /etc/ssh/sshd_config
sed -i ‘/^GSSAPIAuthentication/s/GSSAPIAuthentication yes/#GSSAPIAuthentication yes/’ /etc/ssh/sshd_config
sed -i ‘/^GSSAPIAuthentication/s/GSSAPIAuthentication no/#GSSAPIAuthentication no/’ /etc/ssh/sshd_config
service sshd restart

附录:
CentOS7 sshd_config配置内容
[python] view plain copy 在CODE上查看代码片派生到我的代码片
# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don’t trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don’t read the user’s ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to ‘yes’ to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of “PermitRootLogin without-password”.
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to ‘no’.
# WARNING: ‘UsePAM no’ is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server

CentOS6 sshd_config配置内容
[python] view plain copy 在CODE上查看代码片派生到我的代码片
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don’t trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don’t read the user’s ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
#GSSAPICleanupCredentials yes
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

# Set this to ‘yes’ to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of “PermitRootLogin without-password”.
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to ‘no’.
#UsePAM no
UsePAM yes

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#PidFile /var/run/sshd.pid
#MaxStartups 10
#PermitTunnel no
#ChrootDirectory none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# ForceCommand cvs server
UseDNS no
#GSSAPIAuthentication no
#GSSAPIAuthentication yes

20161205补充:
实际使用中发现ansible和jenkins使用时有些问题,网上查询了下,需要在/etc/ssh/sshd_config文件中最后增加两行:
[python] view plain copy 在CODE上查看代码片派生到我的代码片
Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3des-cbc,arcfour128,arcfour256,arcfour,blowfish-cbc,cast128-cbc

KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
因为升级了openssh太新导致通信时加密算法出现问题,加上后重启就可以了。

CentOS SSH密钥登陆及密码密钥双重验证

2017年1月1日 没有评论

一、首先登陆centos,切换用户,切换到你要免密码登陆的用户,进入到家目录,以下我以root为例,命令:
su root
cd ~

二、创建钥匙,命令:ssh-keygen -t rsa,一路按回车就搞定了(请注意提示输入密码一定不要输入不然会不成功)

三、按照流程走完后会在 ~/.ssh目录下(用户所在家目录下的.ssh目录)看到id_rsa, id_rsa.pub文件 第一个是私有密钥 第二个是公共密钥

四、修改SSH配置文件,命令:vi /etc/ssh/sshd_config
#禁用root账户登录,如果是用root用户登录请开启
PermitRootLogin yes
# 是否让 sshd 去检查用户家目录或相关档案的权限数据,
# 这是为了担心使用者将某些重要档案的权限设错,可能会导致一些问题所致。
# 例如使用者的 ~.ssh/ 权限设错时,某些特殊情况下会不许用户登入
StrictModes no
# 是否允许用户自行使用成对的密钥系统进行登入行为,仅针对 version 2。
# 至于自制的公钥数据就放置于用户家目录下的 .ssh/authorized_keys 内
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# 有了证书登录了,就禁用密码登录吧,安全要紧
PasswordAuthentication no

五、因为在第四步指定了AuthorizedKeysFile的放置位置为.ssh/authorized_keys,所以还需把公钥数据id_rsa.pub附加到 authorized_keys 这个档案内才行,命令:
cd ~/.ssh
cat id_ras.pub >> authorized_keys
重启SSH服务,命令:systemctl restart sshd.service

六、下载私钥,这里我使用了rz/sz工具(你也可以使用其他方式),系统默认没有安装,先安装,命令:yum -y install lrzsz
SecureCRT配置:选项→会话选项→X/Y/Zmodem,修改上传和下载的目录。
现在开始下载,命令:
cd ~/.ssh
sz id_ras
然后到你之前配置的下载目录去找,把私钥导入到SecurtCRT,方法:
选项→会话选项→SSH2,在鉴权一栏中点击公钥(注意因为前面已经禁用了密码登陆,我们还得把密码这一栏的勾去掉,否则会无法登陆),点属性,点击使用会话公钥设置,然后在下方的使用身份或证书文件中,选择你刚才下载来的私钥文件,点确定即可。

七、以上所有配置完成,看网上别的教程还说要注意各文件权限问题,我做实验的过程没有遇到,也可能我使用的是root用户的原因,如果你们在过程中有权限报错,建议权限设置:
~/.ssh/ 700
.ssh/authorized_keys 644
.ssh/id_rsa 600 且属于你当前要添加的用户

八、让服务器更安全,开启密码和证书双重验证,先修改SSH配置文件:
vi /etc/ssh/sshd_config
PasswordAuthentication yes
AuthenticationMethods publickey,password #新加入这条
重启SSH服务:systemctl restart sshd.service
SecureCRT配置:因为之前在第六步中把密码去掉了,还得把它再勾起来,选项→会话选项→SSH2,把密码这一栏勾起来即可。

PS:在配置完成后不要关闭当前SecurtCRT的连接窗口,你可以使用新建连接尝试登陆,以免配置出错,造成服务器无法登陆。

RHEL7/CentOS7系统配置和管理变化汇总(转)

2016年12月26日 没有评论

RHEL 7带来了很多系统配置和管理上的变化,虽然有些不习惯,但个人认为大家应该去适应它们,而不是因循守旧,因为变化总是有原因的,它们大致都代表了未来的趋势,你不可能一直停留在原地踏步。

1、GRUB升级至2.0,bootloader的配置文件也调整至/boot/grub2/grub.cfg
查看启动项:
# cat /boot/grub2/grub.cfg |grep menuentry
menuentry ‘CentOS Linux (4.2.5-1.el7.elrepo.x86_64) 7 (Core)’ –class centos …
menuentry ‘CentOS Linux (3.10.0-229.20.1.el7.x86_64) 7 (Core)’ –class centos …
查看默认启动项:
# grub2-editenv list
saved_entry=CentOS Linux (4.2.5-1.el7.elrepo.x86_64) 7 (Core)

修改默认启动内核为4.2.5:
# grub2-set-default ‘CentOS Linux (4.2.5-1.el7.elrepo.x86_64) 7 (Core)’
2、/etc/inittab 不再使用(when using systemd)

cat /etc/inittab 看下就知道了:

# systemd uses ‘targets’ instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target
3、主机名保存到了/etc/hostname 文件下
安装系统时配置的主机名,保存到了 /etc/hostname 。
4、几乎全面由systemd管理服务,SysV已经退居二线了,想配置服务得用systemctl

systemctl enable/disable sshd.service
其实启用服务就是在当前“runlevel”的配置文件目录(/etc/systemd/system/multi-user.target.wants/)里, 建立/usr/lib/systemd/system 里面对应服务配置文件的软链接;禁用服务就是删除此软链接。
有兴趣就自己看看 /usr/lib/systemd/system 里的文件,语法跟旧版/etc/init.d/ 里的服务脚本完全不同,也不能再用 /etc/init.d/sshd restart 之类的指令启动服务器了。

常用指令:
# systemctl list-unit-files -t service
# systemctl status firewalld.service
# systemctl enable/disable firewalld.service
# systemctl is-enabled firewalld.service
查看系统启动以来的message信息
# journalctl -b (类似 dmesg )

另,/etc/rc.local 文件默认没有执行权限:

# cat /etc/rc.local

系统不再建议使用此文件,而是建议创建自己的 systemd services:

# It is highly advisable to create own systemd services or udev rules

# to run scripts during boot instead of using this file.

#

# Please note that you must run ‘chmod +x /etc/rc.d/rc.local’ to ensure

# that this script will be executed during boot.

如果希望使用该文件(在开机时运行某命令),注意记得给该文件添加执行权限。
5、/etc/sysct.conf 中的默认配置移到了/usr/lib/sysctl.d/00-system.conf

# cat /etc/sysctl.conf
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
# To override those settings, enter new settings here, or in an /etc/sysctl.d/.conf file
# 在这里设置配置会覆盖 00-system.conf 下的默认配置,或者
# 在 /etc/sysctl.d/ 下创建一个 .conf 的文件也可以

6、是否关闭IPv6:不建议完全关闭ipv6模块,否则一些组件会有问题

from Centos 7 FAQ:
Upstream employee Daniel Walsh recommends not disabling the ipv6 module, as that can cause issues with SELinux and other components, but adding the following to /etc/sysctl.conf:

# net.ipv6.conf.all.disable_ipv6 = 1 //但开启这个会导致postfix无法启动
net.ipv6.conf.default.disable_ipv6 = 1 //设置这一项还是可以的

注:总感觉没啥必要关闭ipv6了,如果有域名解析的问题,那也应该是DNS服务器去适配。

7、新的网卡命名规则(默认不再是 eth0/1 )

旧的 eth0/1 命名规则,是不确定的,机器增减网卡后,原有的网卡名字可能会变化。RHEL7支持几种新的命名规则,其中默认的基于硬件信息的规则可以保证网卡名字自动生成并且是固定的,但是会比较难以“阅读”(read),比如 enp2s0,enp3s0。

引用自Redhat官方文档:

8.3. UNDERSTANDING THE PREDICTABLE NETWORK INTERFACE DEVICE NAMES
The names have two character prefixes based on the type of interface:

en for Ethernet,

wl for wireless LAN (WLAN),

ww for wireless wide area network (WWAN).

The names have the following types:

Table 8.1. Device Name Types

Format

Description

o on-board device index number
s[f][d] hotplug slot index number
x MAC address
ps[f][d] PCI geographical location
ps[f][u ][..][c][i] USB port number chain

更多信息参考:
新的网卡命名规则,参见:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/ch-Consistent_Network_Device_Naming.html#sec-Understanding_the_Predictable_Network_Interface_Device_Names
还有Centos 7 FAQ:https://wiki.centos.org/FAQ/CentOS7
8、ll /etc/udev/rules.d/,默认不存在 70-persistent-net.rules 了

网卡MAC只在网卡配置文件里有,一旦变了我只需要改网卡配置文件?(拷贝虚拟机的情况的常见问题)
在图形界面下修改MAC后网卡不认了,测试结果是只需要改网卡配置文件的MAC,而且相关的多个配置文件都要改才行。

(注:这个我没实际测试)
9、ip / ss 指令替代 ifconfig route arp / netstat
用 ip neighbour 代替 arp -n
其他 ip 指令 大家自己看帮助吧。
10、旧的 network 脚本(service)和 ifcfg 文件

Centos7 开始,网络由 NetworkManager 服务负责管理,相对于旧的 /etc/init.d/network 脚本,NetworkManager 是动态的、事件驱动的网络管理服务。旧的 /etc/init.d/network 以及 ifup,ifdown 等依然存在,但是处于备用状态,即:NetworkManager 运行时,多数情况下这些脚本会调用 NetworkManager 去完成网络配置任务;NetworkManager没有运行时,这些脚本就按照老传统管理网络。
需要注意的是:
1)不建议 systemctl disable NetworkManager.service
2)因为旧的 network 脚本不兼容 ifcfg-* 文件里的新的配置项名称 IPADDR0/PREFIX0/GATEWAY0
3)除非把后面那个 0 去掉,否则开机是无法启动网卡的
11、网络配置
(大部分引用自网上文章)
1)nmtui 配置基本网络连接

nmtui 属于curses-based text user interface(文本用户界面), 类似 Centos6 的 setup 工具,但只能编辑连接、启用/禁用连接、更改主机名。系统初装之后可以第一时间用nmtui配置网络,挺方便。
2)nmcli 和其他网络设置
nmcli 是命令行形式的工具,功能要强大、复杂的多。
# nmcli help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OBJECT和COMMAND可以用全称也可以用简称,最少可以只用一个字母,建议用头三个字母。OBJECT里面我们平时用的最多的就是connection和device,这里需要简单区分一下connection和device。
device 叫网络接口,是物理设备
connection 是连接,偏重于逻辑设置

多个connection可以应用到同一个device,但同一时间只能启用其中一个connection。
这样的好处是针对一个网络接口,我们可以设置多个网络连接,比如静态IP和动态IP,再根据需要up相应的connection。
我们可以用nmtui把两个连接改成我们熟悉的名字(nmcli也能,但比较麻烦哦),需要注意的是,enp0s3设备对应的连接名改为eth0,但对应的ifcfg文件并没有改名。
连接的配置文件 ifcfg-*,可以用 DEVICE 指定设备名,也可以用HWADDR指定设备的MAC地址,最终结果都一样的指向某个设备(网络接口)。
所以,对一个网络接口设置不同的连接,可以快速的切换不同的网络配置,这个真的满厉害的。
如果希望系统重启后仅up某个特定连接,那么可以把其他连接配置文件的ONBOOT=no,ONBOOT=no的连接也可以随时up。
3)编辑连接
用nmtui编辑连接后,ifcfg文件也会有相应的改动;手工修改ifcfg后,nmtui中也能看到。
但是,不论用nmtui还是直接修改ifcfg文件,想让新的配置生效,我们需要load连接配置文件并重新up连接。

举例,我们编辑ifcfg-enp2s0-1,把IP改为10.0.3.111,然后执行nmcli con reload 或 nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp2s0-1
nmtui 手工为网卡添加多个IP(secondary IP)
新IP已经被加入到ifcfg-enp2s0中,原始地址的关键字是 IPADDR0、PREFIX0,新地址的关键字是 IPADDR1、PREFIX1 。
nmtui 添加IP后,需要执行命令来生效:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp2s0
nmcli dev connect enp2s0

3.1)手工添加IP到ifcfg-eth0

手工添加IP到ifcfg-enp2s0后,需要执行命令来生效:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp2s0 或 nmcli con reload
nmcli dev connect enp2s0

3.2)用ip addr add 指令添加/删除IP:即刻生效,重启不保留。
3.3)使用子连接配置文件 ifcfg-*:X(Centos5/6时代):不支持了。
但 ifconfig eth0:1 192.168.1.22 up 这种指令还是支持的!
3.4)配置DNS nameserver(不直接修改/etc/resolv.conf 了)
也是在 /etc/sysconfig/network-scripts/ifcfg-enp2s0 文件中配置,在其中添加
DNS1=”10.36.109.251″
DNS2=”10.36.112.251″
与手工添加IP到ifcfg-enp2s0一样,需要执行命令来生效:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp2s0 或 nmcli con reload
nmcli dev connect enp2s0
这样,添加的DNS1/DNS2会同步到/etc/resolv.conf文件中。
(注:直接修改/etc/resolv.conf文件,重启系统或运行上述命令后会丢失)
4)网络配置总结:
ip addr show 中显示的IP才是有效的。

ip addr add 能在线添加IP,立即生效,但重启即丢。

ip addr del 能在线删除IP,立即生效,但重启即丢。

子连接配置文件(ifcfg-*:X)已经无效了。

配置DNS nameserver(不直接修改/etc/resolv.conf 了),同在 ifcfg-enp2s0 这类文件中。

nmtui 和 编辑ifcfg-enp2s0 文件效果基本相同,都不能立即生效,必须 load 连接再重新connect网络设备,即:

nmcli con load /etc/sysconfig/network-scripts/ifcfg-enp2s0 或 nmcli reload
nmcli dev connect enp2s0

如何在CentOS 7 / RHEL 7 运行单用户模式进行root的密码重置

2016年12月26日 没有评论

步骤一,开机时随便按下键盘,进入以下菜单

步骤二: 选择第一项,按e键进行修改

步骤三,定位到 ro(
linux 16 or linuxefi
)

步骤四:把ro改成 “rw init=/sysroot/bin/sh”. 完成之后按 “Ctrl+x”

现在你可以进入单用户模式了

依次输入以下命令进行root密码修改,修改完成之后强制重启即可。

chroot /sysroot/
passwd root
touch /.autorelabel

然后强行重启就可以了。

在CentOS 7上给一个网卡分配多个IP地址

2016年12月25日 没有评论

我们现在可以在CentOS/RHEL 7中给一个网卡分配多个ip地址。

如你所知,网卡的配置文件存储在 /etc/sysconfig/network-scripts/ 目录下。每个网卡的详细内容将会以不同的名字存储,比如ifcfg-enp0s3。

让我们看下ifcfg-enp0s3的细节。

cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
示例输出:

TYPE=”Ethernet”
BOOTPROTO=”none”
DEFROUTE=”yes”
IPV4_FAILURE_FATAL=”no”
IPV6INIT=”yes”
IPV6_AUTOCONF=”yes”
IPV6_DEFROUTE=”yes”
IPV6_FAILURE_FATAL=”no”
NAME=”enp0s3″
UUID=”e9f9caef-cb9e-4a19-aace-767c6ee6f849″
ONBOOT=”yes”
HWADDR=”08:00:27:80:63:19″
IPADDR0=”192.168.1.150″
PREFIX0=”24″
GATEWAY0=”192.168.1.1″
DNS1=”192.168.1.1″
IPV6_PEERDNS=”yes”
IPV6_PEERROUTES=”yes”
好的,现在我们将在相同的子网中分配多个地址了。

编辑文件 /etc/sysconfig/network-scripts/ifcfg-enp0s3:

vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
像下面那样加入额外的IP地址。

TYPE=”Ethernet”
BOOTPROTO=”none”
DEFROUTE=”yes”
IPV4_FAILURE_FATAL=”no”
IPV6INIT=”yes”
IPV6_AUTOCONF=”yes”
IPV6_DEFROUTE=”yes”
IPV6_FAILURE_FATAL=”no”
NAME=”enp0s3″
UUID=”933cdc9b-b383-4ddd-b219-5a72c69c9cf0″
ONBOOT=”yes”
HWADDR=”08:00:27:3F:AB:68″
IPADDR0=”192.168.1.150″
IPADDR1=”192.168.1.151″
IPADDR2=”192.168.1.152″
PREFIX0=”24″
GATEWAY0=”192.168.1.1″
DNS1=”192.168.1.1″
IPV6_PEERDNS=”yes”
IPV6_PEERROUTES=”yes”
如你所见,我已经加了两个IP地址:IPADDR1=”192.168.1.151″ & IPADDR2=”192.168.1.152″

类似地,你可以加入更多的ip地址。

最后,保存并退出文件。重启网络服务来使更改生效。

systemctl restart network

现在,让我们检查是否已经加入了ip地址。

ip addr

分类: Linux, 解决方案 标签: ,