错误原因
可能由于强制结束了yum操作而导致rpm数据库被损坏了!不一定是你手动结束,也可能是因为网络原因
解决方案
删除已损坏的 __db 文件
进入rpm目录命令:
cd /var/lib/rpm
#删除 __db 文件命令:两种方式任选一种
#方式一:
rm /var/lib/rpm/__**
#方式二:
rm -i __db.*
重建rpm数据库
#重新构建rpm数据库命令:
rpm –rebuilddb
错误原因
可能由于强制结束了yum操作而导致rpm数据库被损坏了!不一定是你手动结束,也可能是因为网络原因
解决方案
删除已损坏的 __db 文件
进入rpm目录命令:
cd /var/lib/rpm
#删除 __db 文件命令:两种方式任选一种
#方式一:
rm /var/lib/rpm/__**
#方式二:
rm -i __db.*
重建rpm数据库
#重新构建rpm数据库命令:
rpm –rebuilddb
yum -y install wget
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
yum -y install nmon
nmon
link:https://blog.csdn.net/weixin_42612178/article/details/115801519
警告:setlocale: LC_CTYPE: 无法改变区域选项 (UTF-8)
在/etc/environment添加:
LC_ALL=zh_CN.UTF-8
LANG=zh_CN.UTF-8
内核参数overcommit_memory
它是 内存分配策略
可选值:0、1、2。
0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
什么是Overcommit和OOM
Linux对大部分申请内存的请求都回复”yes”,以便能跑更多更大的程序。因为申请内存后,并不会马上使用内存。这种技术叫做 Overcommit。当linux发现内存不足时,会发生OOM killer(OOM=out-of-memory)。它会选择杀死一些进程(用户态进程,不是内核线程),以便释放内存。
当oom-killer发生时,linux会选择杀死哪些进程?选择进程的函数是oom_badness函数(在mm/oom_kill.c中),该 函数会计算每个进程的点数(0~1000)。点数越高,这个进程越有可能被杀死。每个进程的点数跟oom_score_adj有关,而且 oom_score_adj可以被设置(-1000最低,1000最高)。
解决方法:
很简单,按提示的操作(将vm.overcommit_memory 设为1)即可:
有三种方式修改内核参数,但要有root权限:
(1)编辑/etc/sysctl.conf ,改vm.overcommit_memory=1,然后sysctl -p使配置文件生效
(2)sysctl vm.overcommit_memory=1
(3)echo 1 > /proc/sys/vm/overcommit_memory
systemctl status firewalld
firewall-cmd --state
查看linux哪些程序正在使用互联网
firewall-cmd --permanent --list-services ssh dhcpv6-client
开启
service firewalld start
重启
service firewalld restart
关闭
service firewalld stop
firewall-cmd --list-all
查询端口是否开放
firewall-cmd --query-port=8080/tcp
开放80端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=8080-8085/tcp
移除端口
firewall-cmd --permanent --remove-port=8080/tcp
查看防火墙的开放的端口 firewall-cmd --permanent --list-ports
重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;
Link: https://blog.csdn.net/qq_41153478/article/details/83033688
tcpdump是一个用于截取网络分组,并输出分组内容的工具。tcpdump凭借强大的功能和灵活的截取策略,使其成为类UNIX系统下用于网络分析和问题排查的首选工具。
tcpdump提供了源代码,公开了接口,因此具备很强的可扩展性,对于网络维护和入侵者都是非常有用的工具。tcpdump存在于基本的Linux系统中,由于它需要将网络界面设置为混杂模式,普通用户不能正常执行,但具备root权限的用户可以直接执行它来获取网络上的信息。因此系统中存在网络分析工具主要不是对本机安全的威胁,而是对网络上的其他计算机的安全存在威胁。
自 17.05 版本开始,Docker 支持多步骤镜像创建( Multi-stage build)特性,可以精简最终生成的镜像大小 。
对于需要编译的应用(如 C、Go或Java语言等)来说,通常情况下至少需要准备两个 环境的 Docker镜像:
使用多步骤创建,可以在保证最终生成的运行环境镜像保持精筒的情况下,使用单一的Dockerfile,降低维护复杂度 。
以 Go语言应用为例。创建干净目录,进入到目录中,创建main.go文件,内容为:
// main.go will output "Hello, Docker"
package main
import ("fmt")
func main() {
fmt.println("Hello, Docker")
}
创建 Dockerfile,使用 golang:1.9镜像编译应用二进制文件为 app,使用精简的镜像 alpine:latest 作为运行环境 。Dockerfile 完整内容为:
FROM golang:1.9 as builder # define stage name as builder
RUN mkdir -p /go/src/test
WORKDIR /go/src/test
COPY main.go
RUN CGO_ENABLED=O GOOS=linux go build -o app
FROM alpine:latest
RUN apk –no-cache add ca-certificates
WORKDIR /root/
COPY –from=builder /go/src/test/app . # copy file from the builder stage
CMD ["./app"]
执行如下命令创建镜像,并运行应用:
$ docker build -t yeasy/test multistage:latest .
Sending build context to Docker daemon 3.072kB
Step 1/10 : FROM golang:1.9
Successfully built 5fd0cb93dda0
Successfully tagged yeasy/test-multistage:latest
$ docker run --rm yeasy/test-m ultistage:latest
Hello, Docker
查看生成的最终镜像,大小只有 6.55 MB:
$docker images | grep test-multistage
yeasy/test-multistage latest 5fd0cb93dda0 1 minutes ago 6.55MB
文章来源:
《Docker技术入门与实践》第三版 杨保华、戴王剑、曹亚仑 编著
dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。
注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512;c=1; k=1024;w=2
参数注释:
1. 将本地的/dev/hdb整盘备份到/dev/hdd
$ dd if=/dev/hdb of=/dev/hdd
2. 将/dev/hdb全盘数据备份到指定路径的image文件
$ dd if=/dev/hdb of=/root/image
3. 将备份文件恢复到指定盘
$ dd if=/root/image of=/dev/hdb
4. 备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径
$ dd if=/dev/hdb | gzip > /root/image.gz
5. 将压缩的备份文件恢复到指定盘
$ gzip -dc /root/image.gz | dd of=/dev/hdb
6. 备份与恢复MBR
备份磁盘开始的512个字节大小的MBR信息到指定文件:
$ dd if=/dev/hda of=/root/image count=1 bs=512
count=1指仅拷贝一个块;bs=512指块大小为512个字节。
恢复:
$ dd if=/root/image of=/dev/had
将备份的MBR信息写到磁盘开始部分
7. 备份软盘
$ dd if=/dev/fd0 of=disk.img count=1 bs=1440k (即块大小为1.44M)
8. 拷贝内存内容到硬盘
$ dd if=/dev/mem of=/root/mem.bin bs=1024 (指定块大小为1k)
9. 拷贝光盘内容到指定文件夹,并保存为cd.iso文件
$ dd if=/dev/cdrom(hdc) of=/root/cd.iso
10. 增加swap分区文件大小
第一步:创建一个大小为256M的文件:
$ dd if=/dev/zero of=/swapfile bs=1024 count=262144
第二步:把这个文件变成swap文件:
$ mkswap /swapfile
第三步:启用这个swap文件:
$ swapon /swapfile
第四步:编辑/etc/fstab文件,使在每次开机时自动加载swap文件:
$ /swapfile swap swap default 0 0
11. 销毁磁盘数据
$ dd if=/dev/urandom of=/dev/hda1
注意:利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据。
12. 测试硬盘的写速度
$ dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
通过以上命令输出的命令执行时间,可以计算出硬盘的写速度。
13. 测试硬盘的读速度
$ dd if=/root/1Gb.file bs=64k | dd of=/dev/null
通过以上命令输出的命令执行时间,可以计算出硬盘的读速度。
14. 确定硬盘的最佳块大小
$ dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
$ dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file
$ dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file
$ dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file
通过比较以上命令输出中所显示的命令执行时间,即可确定系统最佳的块大小。
15. 修复硬盘
$ dd if=/dev/sda of=/dev/sda
或者
$ dd if=/dev/hda of=/dev/hda
当硬盘较长时间(一年以上)放置不使用后,磁盘上会产生magnetic flux point,当磁头读到这些区域时会遇到困难,并可能导致I/O错误。当这种情况影响到硬盘的第一个扇区时,可能导致硬盘报废。上边的命令有可能使这些数 据起死回生。并且这个过程是安全、高效的。
16. 利用netcat远程备份
$ dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234
在源主机上执行此命令备份/dev/hda
netcat -l -p 1234 | dd of=/dev/hdc bs=16065b
在目的主机上执行此命令来接收数据并写入/dev/hdc
$ netcat -l -p 1234 | bzip2 > partition.img
$ netcat -l -p 1234 | gzip > partition.img
以上两条指令是目的主机指令的变化分别采用bzip2、gzip对数据进行压缩,并将备份文件保存在当前目录。
17. 将一个很大的视频文件中的第i个字节的值改成0x41(也就是大写字母A的ASCII值)
$ echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc
A virtual private network, or VPN, allows you to securely encrypt traffic as it travels through untrusted networks, such as those at the coffee shop, a conference, or an airport.
IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunneling between the server and client. In IKEv2 VPN implementations, IPSec provides encryption for the network traffic. IKEv2 is natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.
In this tutorial, you’ll set up an IKEv2 VPN server using StrongSwan on an Ubuntu 18.04 server and connect to it from Windows, macOS, Ubuntu, iOS, and Android clients.
To complete this tutorial, you will need:
One Ubuntu 18.04 server configured by following the Ubuntu 18.04 initial server setup guide, including a sudo non-root user and a firewall.
First, we’ll install StrongSwan, an open-source IPSec daemon which we’ll configure as our VPN server. We’ll also install the public key infrastructure component so that we can create a certificate authority to provide credentials for our infrastructure.
Update the local package cache and install the software by typing:
$ sudo apt update
$ sudo apt install strongswan strongswan-pki ufw
Now that everything’s installed, let’s move on to creating our certificates.
An IKEv2 server requires a certificate to identify itself to clients. To help us create the certificate required, the strongswan-pki package comes with a utility to generate a certificate authority and server certificates. To begin, let’s create a few directories to store all the assets we’ll be working on. The directory structure matches some of the directories in /etc/ipsec.d, where we will eventually move all of the items we create. We’ll lock down the permissions so that our private files can’t be seen by other users:
$ mkdir -p ~/pki/{cacerts,certs,private}
$ chmod 700 ~/pki
Now that we have a directory structure to store everything, we can generate a root key. This will be a 4096-bit RSA key that will be used to sign our root certificate authority.
Execute these commands to generate the key:
ipsec pki --gen --type rsa --size 4096 --outform pem \
> ~/pki/private/ca-key.pem
Now that we have a key, we can move on to creating our root certificate authority, using the key to sign the root certificate:
$ ipsec pki --self --ca --lifetime 3650 --in ~/pki/private/ca-key.pem \
--type rsa --dn "CN=VPN root CA" --outform pem > ~/pki/cacerts/ca-cert.pem
You can change the distinguished name (DN) values to something else to if you would like. The common name here is just the indicator, so it doesn’t have to match anything in your infrastructure.
Now that we’ve got our root certificate authority up and running, we can create a certificate that the VPN server will use.
We’ll now create a certificate and key for the VPN server. This certificate will allow the client to verify the server’s authenticity using the CA certificate we just generated.
First, create a private key for the VPN server with the following command:
$ ipsec pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/server-key.pem
Now, create and sign the VPN server certificate with the certificate authority’s key you created in the previous step. Execute the following command, but change the Common Name (CN) and the Subject Alternate Name (SAN) field to your VPN server’s DNS name or IP address:
$ ipsec pki --pub --in ~/pki/private/server-key.pem --type rsa \
| ipsec pki --issue --lifetime 1825 \
--cacert ~/pki/cacerts/ca-cert.pem \
--cakey ~/pki/private/ca-key.pem \
--dn "CN=server_domain_or_IP" --san "server_domain_or_IP" \
--flag serverAuth --flag ikeIntermediate --outform pem \
> ~/pki/certs/server-cert.pem
Now that we’ve generated all of the TLS/SSL files StrongSwan needs, we can move the files into place in the /etc/ipsec.d
directory by typing:
$ sudo cp -r ~/pki/* /etc/ipsec.d/
In this step, we’ve created a certificate pair that would be used to secure communications between the client and the server. We’ve also signed the certificates with the CA key, so the client will be able to verify the authenticity of the VPN server using the CA certificate. Now that have all of the certificates ready, we’ll move on to configuring the software.
StrongSwan has a default configuration file with some examples, but we will have to do most of the configuration ourselves. Let’s back up the file for reference before starting from scratch:
$ sudo mv /etc/ipsec.conf{,.original}
Create and open a new blank configuration file by typing:
sudo vim /etc/ipsec.conf
First, we’ll tell StrongSwan to log daemon statuses for debugging and allow duplicate connections. Add these lines to the file:
config setup
charondebug="ike 1, knl 1, cfg 0"
uniqueids=no
conn ikev2-vpn
auto=add
compress=no
type=tunnel
keyexchange=ikev2
fragmentation=yes
forceencaps=yes
dpdaction=clear
dpddelay=300s
rekey=no
left=%any
leftid=@server_domain_or_IP
leftcert=server-cert.pem
leftsendcert=always
leftsubnet=0.0.0.0/0
right=%any
rightid=%any
rightauth=eap-mschapv2
rightsourceip=10.10.10.0/24
rightdns=8.8.8.8,8.8.4.4
rightsendcert=never
eap_identity=%identity
Save and close the file once you’ve verified that you’ve configured things as shown.
Now that we’ve configured the VPN parameters, let’s move on to creating an account so our users can connect to the server.
Our VPN server is now configured to accept client connections, but we don’t have any credentials configured yet. We’ll need to configure a couple things in a special configuration file called ipsec.secrets
:
Let’s open the secrets file for editing:
$ sudo vim /etc/ipsec.secrets
First, we’ll tell StrongSwan where to find our private key,Then, we’ll define the user credentials. You can make up any username or password combination that you like:
: RSA "server-key.pem"
your_username : EAP "your_password"
Save and close the file. Now that we’ve finished working with the VPN parameters, we’ll restart the VPN service so that our configuration is applied:
$ sudo systemctl restart strongswan
Now that the VPN server has been fully configured with both server options and user credentials, it’s time to move on to configuring the most important part: the firewall.
With the StrongSwan configuration complete, we need to configure the firewall to forward and allow VPN traffic through.
If you followed the prerequisite tutorial, you should have a very basic UFW firewall enabled. If you don’t yet have UFW configured, you can create a baseline configuration and enable it by typing:
$ sudo ufw allow OpenSSH
$ sudo ufw enable
Now, add a rule to allow UDP traffic to the standard IPSec ports, 500 and 4500:
$ sudo ufw allow 500,4500/udp
Next, we will open up one of UFW’s configuration files to add a few low-level policies for routing and forwarding IPSec packets. Before we do, we need to find which network interface on our server is used for internet access. We can find that by querying for the interface associated with the default route:
$ ip route | grep default
Your public interface should follow the word “dev”. For example, this result shows the interface named eth0, which is highlighted below:
Output
default via 203.0.113.7 dev eth0 proto static
When you have your public network interface, open the /etc/ufw/before.rules
file in your text editor:
$ sudo nano /etc/ufw/before.rules
Near the top of the file (before the*filter
line), add the following configuration block:
*nat
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -m policy --pol ipsec --dir out -j ACCEPT
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
COMMIT
*mangle
-A FORWARD --match policy --pol ipsec --dir in -s 10.10.10.0/24 -o eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
COMMIT
*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]
. . .
Change each instance of eth0 in the above configuration to match the interface name you found with ip route. The *nat lines create rules so that the firewall can correctly route and manipulate traffic between the VPN clients and the internet. The *mangle line adjusts the maximum packet segment size to prevent potential issues with certain VPN clients.
Next, after the *filter
and chain definition lines, add one more block of configuration:
. . .
*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]
-A ufw-before-forward --match policy --pol ipsec --dir in --proto esp -s 10.10.10.0/24 -j ACCEPT
-A ufw-before-forward --match policy --pol ipsec --dir out --proto esp -d 10.10.10.0/24 -j ACCEPT
These lines tell the firewall to forward ESP (Encapsulating Security Payload) traffic so the VPN clients will be able to connect. ESP provides additional security for our VPN packets as they’re traversing untrusted networks.
When you’re finished, save and close the file.
Before we restart the firewall, we’ll change some network kernel parameters to allow routing from one interface to another. Open UFW’s kernel parameters configuration file:
$ sudo nano /etc/ufw/sysctl.conf
We’ll need to configure a few things here:
The changes you need to make to the file are highlighted in the following code:
. . .
# Enable forwarding
# Uncomment the following line
net/ipv4/ip_forward=1
. . .
# Do not accept ICMP redirects (prevent MITM attacks)
# Ensure the following line is set
net/ipv4/conf/all/accept_redirects=0
# Do not send ICMP redirects (we are not a router)
# Add the following lines
net/ipv4/conf/all/send_redirects=0
net/ipv4/ip_no_pmtu_disc=1
Save the file when you are finished. UFW will apply these changes the next time it starts.
Now, we can enable all of our changes by disabling and re-enabling the firewall:
$ sudo ufw disable
$ sudo ufw enable
You’ll be prompted to confirm the process. Type Y to enable UFW again with the new settings.
Reboot server to make it active.
Now that you have everything set up, it’s time to try it out. First, you’ll need to copy the CA certificate you created and install it on your client device(s) that will connect to the VPN. The easiest way to do this is to log into your server and output the contents of the certificate file:
$ cat /etc/ipsec.d/cacerts/ca-cert.pem
You’ll see output similar to this:
-----BEGIN CERTIFICATE-----
MIIFQjCCAyqgAwIBAgIIFkQGvkH4ej0wDQYJKoZIhvcNAQEMBQAwPzELMAkGA1UE
. . .
EwbVLOXcNduWK2TPbk/+82GRMtjftran6hKbpKGghBVDPVFGFT6Z0OfubpkQ9RsQ
BayqOb/Q
-----END CERTIFICATE-----
Copy this output to your computer, including the —–BEGIN CERTIFICATE—– and —–END CERTIFICATE—– lines, and save it to a file with a recognizable name, such as ca-cert.pem. Ensure the file you create has the .pem extension.
Alternatively, use SFTP to transfer the file to your computer.
Once you have the ca-cert.pem file downloaded to your computer, you can set up the connection to the VPN.
To configure the VPN connection on an iOS device, follow these steps:
Follow these steps to import the certificate:
Now that the certificate is important and trusted, configure the VPN connection with these steps:
Finally, click on Connect to connect to the VPN. You should now be connected to the VPN.
搭建一套redis集群用于测试,搭建过程如下
安装redis的ruby插件,用于构建redis集群。
gem install redis
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz
cd redis-4.0.9
make
make PREFIX=/opt/redis install
cp src/redis-trib.rb /opt/redis/bin/
mkdir -p /opt/redis/cluster/{10001,10002,10003,10004,10005,10006}
echo "port {port}" > ./redis-cluster.conf
echo "dir /opt/redis/cluster/{port}" >> ./redis-cluster.conf
echo "cluster-enabled yes" >> ./redis-cluster.conf
echo "daemonize yes" >> ./redis-cluster.conf
echo "bind 127.0.0.1" >> ./redis-cluster.conf
sed 's/{port}/10001/' ./redis-cluster.conf > /opt/redis/redis-cluster-10001.conf
sed 's/{port}/10002/' ./redis-cluster.conf > /opt/redis/redis-cluster-10002.conf
sed 's/{port}/10003/' ./redis-cluster.conf > /opt/redis/redis-cluster-10003.conf
sed 's/{port}/10004/' ./redis-cluster.conf > /opt/redis/redis-cluster-10004.conf
sed 's/{port}/10005/' ./redis-cluster.conf > /opt/redis/redis-cluster-10005.conf
sed 's/{port}/10006/' ./redis-cluster.conf > /opt/redis/redis-cluster-10006.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10001.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10002.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10003.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10004.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10005.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10006.conf
echo "yes" | /opt/redis/bin/redis-trib.rb create --replicas 1 127.0.0.1:10001 127.0.0.1:10002 127.0.0.1:10003 127.0.0.1:10004 127.0.0.1:10005 127.0.0.1:10006
PIDS=`ps -ef | grep '/opt/redis/bin/redis-server' | grep '127.0.0.1:1000' | awk '{print $2}'`
for i in $PIDS; do
echo "kill pid $i"
kill $i
done
sleep 3
rm -rf /opt/redis/cluster
mkdir -p /opt/redis/cluster/{10001,10002,10003,10004,10005,10006}
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10001.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10002.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10003.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10004.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10005.conf
/opt/redis/bin/redis-server /opt/redis/redis-cluster-10006.conf
echo "yes" | /opt/redis/bin/redis-trib.rb create --replicas 1 127.0.0.1:10001 127.0.0.1:10002 127.0.0.1:10003 127.0.0.1:10004 127.0.0.1:10005 127.0.0.1:10006