分类目录归档:Linux

Linux

How to setup an SFTP server on CentOS

This tutorial explains how to setup and use an SFTP server on CentOS. Before I start, let me explain what actually SFTP represents and what it is used for. Currently, most people know that we can use normal FTP for transferring, downloading or uploading data from a server to client or client to server. But this protocol is getting hacked easily (if TLS is not used) by anonymous intruders as it the ports are wide open to anyone. Therefore, SFTP has been introduced to as another alternative to meet the main purpose to strengthen the security level.

SFTP stands for SSH File Transfer Protocol or Secure File Transfer Protocol. It uses a separate protocol packaged with SSH to provide a secure connection.

1. Preliminary Note

For this tutorial, I am using CentOS 7 in the 64bit version. The same steps will work on CentOS 6 as well. The tutorial result will show how a client can be provided with access to the SFTP server but unable to login to the server itself by SSH.

2. SFTP Installation

Unlike normal FTP, there’s no need to install additional packages in order to use SFTP. We just require the prebuild SSHd package that got already installed during installation on the server. Therefore, just check to confirm if you already have the required SSH package. Below are the steps:

Run:

rpm -qa|grep ssh

The output should be similar to this:

[root@localhost ~]# rpm -qa|grep sshlibssh2-1.4.3-10.el7_2.1.x86_64openssh-7.4p1-13.el7_4.x86_64openssh-server-7.4p1-13.el7_4.x86_64openssh-clients-7.4p1-13.el7_4.x86_64

That’s all, now we’ll go on how to make the SFTP configuration.

3. SFTP Configuration

Once all prerequisites of installation are done, we’ll step over to configuration phase. For best practice, we need to create a group and user so that we can manage all user that shall get SFTP access. But first, let’s create an additional folder called data. Below are the steps:

mkdir -p /data/sftpchmod 701 /data

Basically what I’m trying to do with the above step is to get a separate folder as main directory for the SFTP access. All user directories for the SFTP users will be subdirectories of this data folder.

Let’s create a group for the SFTP user, below are the steps:

groupadd sftpusers

Then create a user ‘howtoforge’ and assign it to the SFTPUSERS group. Below are the steps:

useradd -g sftpusers -d /upload -s /sbin/nologin mysftpuserpasswd mysftpuser
Changing password for user mysftpuser.New password:Retype new password:passwd: all authentication tokens updated successfully.

Below is the explanation of the above commands:

  1. I create a user and include the user into sftpusers group using -g command.
  2. I assign the main directory for the user to be in the /upload directory by setting the -d /upload command. This means that later the /upload folder will be under /data/mysftpuser/upload.
  3. I limit the access to the /sbin/nologin shell to ensure the user is only able to use the SFTP protocol, not SSH.
  4. I name the user “mysftpuser”.
  5. Set password for user “mysftpuser”.


Now let’s create the /upload folder under /data/mysftpuser, then assign appropriate ownership to the folder.

mkdir -p /data/mysftpuser/uploadchown -R root:sftpusers /data/mysftpuserchown -R mysftpuser:sftpusers /data/mysftpuser/upload

Once done, verify that the new folder under the directory /data exists and that we made the configuration correct.

[root@localhost ~]# ls -ld /data/drwx-----x. 5 root root 54 Mar 22 14:29 /data/
[root@localhost ~]# ls -ld /data/mysftpuserdrwxr-xr-x. 3 root sftpusers 20 Mar 22 14:29 /data/mysftpuser
[root@localhost ~]# ls -ld /data/mysftpuser/uploaddrwxr-xr-x. 2 mysftpuser sftpusers 6 Mar 22 14:29 /data/mysftpuser/upload
[root@localhost ~]# cat /etc/passwd|grep mysftpusermysftpuser:x:1001:1001::/upload:/sbin/nologin

Now configure the SSH protocol to create an SFTP process. This can be done by editing the configuration file under /etc/ssh/sshd_config. Below are the steps:

nano /etc/ssh/sshd_config

Add the following lines at the end of the file.

Match Group sftpusersChrootDirectory /data/%uForceCommand internal-sftp

Once done restart the SSH services, below are the steps:

service sshd status
Redirecting to /bin/systemctl status sshd.service? sshd.service - OpenSSH server daemonLoaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)Active: active (running) since Thu 2018-03-22 14:21:38 CET; 16min agoDocs: man:sshd(8)man:sshd_config(5)Main PID: 942 (sshd)CGroup: /system.slice/sshd.service??942 /usr/sbin/sshd -D
Mar 22 14:21:37 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...Mar 22 14:21:38 localhost.localdomain sshd[942]: Server listening on 0.0.0.0 port 22.Mar 22 14:21:38 localhost.localdomain sshd[942]: Server listening on :: port 22.Mar 22 14:21:38 localhost.localdomain systemd[1]: Started OpenSSH server daemon.Mar 22 14:21:49 localhost.localdomain sshd[1375]: Accepted password for root from 192.168.1.9 port 59465 ssh2
service sshd restart
[root@localhost ~]# service sshd restartRedirecting to /bin/systemctl restart sshd.service

4. Testing SFTP

Now everything has been configured, so let’s make a test to ensure the setup meets our purpose.
I’ll access SFTP by using another server called TEST01. First, I’ll verify the Port of the SFTP server . To do that I’ll use the nmap function. If your client server didn’t have it you may download and install it with yum as shown below:

yum list nmap
Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile* base: centos.copahost.com* epel: mirror.imt-systems.com* extras: mirror.imt-systems.com* remi-php72: remi.mirror.wearetriple.com* remi-safe: remi.mirror.wearetriple.com* updates: ftp.rz.uni-frankfurt.deAvailable Packagesnmap.x86_64 2:6.40-7.el7
yum install nmap -y
Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfile* base: centos.copahost.com* epel: mirror.imt-systems.com* extras: mirror.imt-systems.com* remi-php72: remi.mirror.wearetriple.com* remi-safe: remi.mirror.wearetriple.com* updates: ftp.rz.uni-frankfurt.deResolving Dependencies--> Running transaction check---> Package nmap.x86_64 2:6.40-7.el7 will be installed--> Processing Dependency: nmap-ncat = 2:6.40-7.el7 for package: 2:nmap-6.40-7.el7.x86_64--> Running transaction check---> Package nmap-ncat.x86_64 2:6.40-7.el7 will be installed--> Finished Dependency Resolution
Dependencies Resolved
=============================================================================================================================================================================================Package Arch Version Repository Size=============================================================================================================================================================================================Installing:nmap x86_64 2:6.40-7.el7 base 4.0 MInstalling for dependencies:nmap-ncat x86_64 2:6.40-7.el7 base 201 k
Transaction Summary=============================================================================================================================================================================================Install 1 Package (+1 Dependent package)
Total download size: 4.2 MInstalled size: 17 MDownloading packages:(1/2): nmap-ncat-6.40-7.el7.x86_64.rpm | 201 kB 00:00:01(2/2): nmap-6.40-7.el7.x86_64.rpm | 4.0 MB 00:00:14---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total 300 kB/s | 4.2 MB 00:00:14Running transaction checkRunning transaction testTransaction test succeededRunning transactionInstalling : 2:nmap-ncat-6.40-7.el7.x86_64 1/2Installing : 2:nmap-6.40-7.el7.x86_64 2/2Verifying : 2:nmap-ncat-6.40-7.el7.x86_64 1/2Verifying : 2:nmap-6.40-7.el7.x86_64 2/2
Installed:nmap.x86_64 2:6.40-7.el7
Dependency Installed:nmap-ncat.x86_64 2:6.40-7.el7
Complete!
[root@localhost ~]# nmap -n 192.168.1.100
Starting Nmap 6.40 ( http://nmap.org ) at 2018-03-22 14:51 CETNmap scan report for 192.168.1.100Host is up (0.000016s latency).Not shown: 997 closed portsPORT STATE SERVICE22/tcp open ssh
Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds


You’ll notice that currently on our SFTP server, the only open port is SSH 22. Now, let’s try to access the SFTP server (IP: 192.168.1.100 in my case) from TEST01 client. Below are the steps:

[root@TEST01 /]# sftp mysftpuser@192.168.1.100mysftpuser@192.168.1.100's password:Connected to 192.168.1.100.sftp> pwdRemote working directory: /uploadsftp>

Great! Now our SFTP server is accessible from outside. Notice that the default directory is /upload . This means that SFTP will only show the default path as /upload even though our previous configuration made in the SFTP server directory is /data/mysftpuser/upload.
Now let’s try to get a file from the SFTP server directory into our testing client. First, let’s create a test file under /data/mysftpuser/upload. Below are the steps:Advertisements

cd /data/mysftpuser/uploadtouch testing_file.txt

Then go back to our testing site TEST01 and see if we able to get and download the created file.

[root@TEST01 /]# sftp mysftpuser@192.168.1.100mysftpuser@192.168.1.100's password:Connected to 192.168.1.100.sftp> pwdRemote working directory: /uploadsftp> lstesting_file.txtsftp> get testing_file.txtFetching /upload/testing_file.txt to testing_file.txtsftp> quit

Excellent! Our SFTP test has been successful, let’s try to access SSH using the user mysftpuser. As previously, we’ve set configuration as /sbin/nologin, therefore the user won’t be able to use SSH services:

[root@TEST01 ~]# ssh mysftpuser@192.168.1.100mysftpuser@192.168.1.100's password:This service allows sftp connections only.Connection to 192.168.1.100 closed.

Nice! Now we have a secured SFTP server up and running.

How to set DNS in CentOS/RHEL 7 & prevent NetworkManager from overwriting /etc/resolv.conf?

This guide shows you how to set custom DNS entries for CentOS 7 / RedHat 7 and ensure that the settings are persistent even after a reboot.

What you need

  • A CentOS 7 or a Red Hat Enterprise Linux (RHEL) 7 server
  • A couple of minutes

Overview

In CentOS and Red Hat Enterprise Linux (RHEL) 7, any custom DNS entries are stored in the file /etc/resolv.conf. However, if we simply go ahead and add our nameservers to this file, we’ll notice that after a reboot or a restart of the network.service, the file is overwritten by NetworkManager.

In this guide, we will first configure NetworkManager to not overwrite this file. Then, we will go ahead and actually add our custom nameservers to /etc/resolv.conf.

Step 1

The NetworkManager configuration is located here: /etc/NetworkManager/NetworkManager.conf Open this file using vim or your favorite text editor.

Search for the [main] section in this file. It should look something like this:

...
[main]
#plugins=ifcfg-rh,ibft
...

Add dns=none just after the [main] tag like this:

...
[main]
dns=none
#plugins=ifcfg-rh,ibft
...

Go ahead and save the file.

Step 2

Let’s restart the NetworkManager.service service so that it picks up the changes we made to the configuration.

sudo systemctl restart NetworkManager.service

Note that the service name NetworkManager.service is case-sensitive.

Step 3

Now, let’s add our nameservers to /etc/resolv.conf

Open this file in you favorite text editor and specify the name servers as follows:

# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 8.8.4.4

That’s it! You’re done. The nameservers added to /etc/resolv.conf will now persist even after a reboot. NetworkManager will not longer overwrite this file.

Centos curl ssl 替换 NSS 为 OpenSSL

系统版本是 Centos 6/7 64位。

1、先安装常用的开发环境。

yum groupinstall Development tools

2、编译OpenSSL

1.下载 OpenSSL:

wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz

2.解压 OpenSSL:

tar -xzvf openssl-1.0.2l.tar.gz

3.进入 OpenSSL目录:

cd openssl-1.0.2l

4.配置并编译 OpenSSL:

./config –shared
make && make install

3、编译配置curl

1.下载 curl 库:

wget https://curl.haxx.se/download/curl-7.55.1.tar.gz

2.解压 curl 库:

tar -xzvf curl-7.55.1.tar.gz

3.进入 curl 目录:

cd curl-7.55.1

4.设置动态库路径:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ssl/lib

5.配置并编译 curl:

./configure –prefix=/usr/local/curl/ –without-nss –with-ssl=/usr/local/ssl/
make && make install

6.备份默认的 curl 二进制文件

mv /usr/bin/curl /usr/bin/curl.bak

7.做一个新的 curl 软链

ln -s /usr/local/curl/bin/curl /usr/bin/curl

总体的替换到此就完成,可以执行 curl –version 来进行确认。下边是我执行的结果:

curl 7.55.1 (x86_64-pc-linux-gnu) libcurl/7.55.1 OpenSSL/1.0.2l
Release-Date: 2017-08-14
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM NTLM_WB SSL TLS-SRP UnixSockets HTTPS-proxy

CentOs 6 64 Bit系统操作实例:

# curl -V

curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2

Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp

Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz

 # curl https://curl.haxx.se/download/curl-7.69.1.tar.gz

 # tar zxvf curl-7.69.1.tar.gz

 #  cd curl-7.69.1

 #  ./configure –prefix=/usr/local/curl/ –without-nss –with-ssl

 #  make && make install

 # /usr/local/curl/bin/curl -V

curl 7.69.1 (x86_64-pc-linux-gnu) libcurl/7.69.1 OpenSSL/1.0.1e-fips zlib/1.2.3

Release-Date: 2020-03-11

Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp

Features: AsynchDNS HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets

# ldd /usr/local/curl/bin/curl

        linux-vdso.so.1 =>  (0x00007ffd0c5bf000)

        libcurl.so.4 => /usr/local/curl/lib/libcurl.so.4 (0x00007f6513ab3000)

        libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007f6513837000)

        libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007f6513454000)

        libz.so.1 => /lib64/libz.so.1 (0x00007f651323e000)

        librt.so.1 => /lib64/librt.so.1 (0x00007f6513035000)

        libc.so.6 => /lib64/libc.so.6 (0x00007f6512ca1000)

        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007f6512a5d000)

        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007f6512776000)

        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007f6512572000)

        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007f6512346000)

        libdl.so.2 => /lib64/libdl.so.2 (0x00007f6512141000)

        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6511f24000)

        /lib64/ld-linux-x86-64.so.2 (0x0000564ebdb90000)

        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007f6511d19000)

        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007f6511b15000)

        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f65118fb000)

        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f65116db000)

而此时,系统的

# curl -V

curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2

Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp

Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL libz

# ldd /usr/bin/curl          

        linux-vdso.so.1 =>  (0x00007ffde3fec000)

        libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x00007fcbc9064000)

        libidn.so.11 => /lib64/libidn.so.11 (0x00007fcbc8e32000)

        libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x00007fcbc8be1000)

        librt.so.1 => /lib64/librt.so.1 (0x00007fcbc89d9000)

        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fcbc8795000)

        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fcbc84ae000)

        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fcbc8282000)

        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fcbc807e000)

        libssl3.so => /usr/lib64/libssl3.so (0x00007fcbc7e3e000)

        libsmime3.so => /usr/lib64/libsmime3.so (0x00007fcbc7c12000)

        libnss3.so => /usr/lib64/libnss3.so (0x00007fcbc78d3000)

        libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007fcbc76a6000)

        libplds4.so => /lib64/libplds4.so (0x00007fcbc74a2000)

        libplc4.so => /lib64/libplc4.so (0x00007fcbc729d000)

        libnspr4.so => /lib64/libnspr4.so (0x00007fcbc705e000)

        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fcbc6e41000)

        libdl.so.2 => /lib64/libdl.so.2 (0x00007fcbc6c3d000)

        libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x00007fcbc6a14000)

        libz.so.1 => /lib64/libz.so.1 (0x00007fcbc67fe000)

        libc.so.6 => /lib64/libc.so.6 (0x00007fcbc646a000)

        liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x00007fcbc625a000)

        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fcbc6040000)

        libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007fcbc5e26000)

        /lib64/ld-linux-x86-64.so.2 (0x000055a80dede000)

        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fcbc5c1a000)

        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fcbc5a17000)

        libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fcbc57aa000)

        libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fcbc53c7000)

        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fcbc5190000)

        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fcbc4f70000)

        libfreebl3.so => /lib64/libfreebl3.so (0x00007fcbc4cf7000)

# ldd /usr/bin/curl

        linux-vdso.so.1 =>  (0x00007ffd715e1000)

        libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x00007efcd84af000)

        libidn.so.11 => /lib64/libidn.so.11 (0x00007efcd827d000)

        libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x00007efcd802c000)

        librt.so.1 => /lib64/librt.so.1 (0x00007efcd7e24000)

        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007efcd7be0000)

        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007efcd78f9000)

        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007efcd76cd000)

        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007efcd74c9000)

        libssl3.so => /usr/lib64/libssl3.so (0x00007efcd7289000)

        libsmime3.so => /usr/lib64/libsmime3.so (0x00007efcd705d000)

        libnss3.so => /usr/lib64/libnss3.so (0x00007efcd6d1e000)

        libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007efcd6af1000)

        libplds4.so => /lib64/libplds4.so (0x00007efcd68ed000)

        libplc4.so => /lib64/libplc4.so (0x00007efcd66e8000)

        libnspr4.so => /lib64/libnspr4.so (0x00007efcd64a9000)

        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007efcd628c000)

        libdl.so.2 => /lib64/libdl.so.2 (0x00007efcd6088000)

        libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x00007efcd5e5f000)

        libz.so.1 => /lib64/libz.so.1 (0x00007efcd5c49000)

        libc.so.6 => /lib64/libc.so.6 (0x00007efcd58b5000)

        liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x00007efcd56a5000)

        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007efcd548b000)

        libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007efcd5271000)

        /lib64/ld-linux-x86-64.so.2 (0x00005572fd9ce000)

        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007efcd5065000)

        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007efcd4e62000)

        libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007efcd4bf5000)

        libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007efcd4812000)

        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007efcd45db000)

        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007efcd43bb000)

        libfreebl3.so => /lib64/libfreebl3.so (0x00007efcd4142000)

# vi .bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

LD_LIBRARY_PATH=/usr/local/curl/lib

export PATH LD_LIBRARY_PATH

# /usr/bin/curl -V

curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.69.1 OpenSSL/1.0.1e-fips zlib/1.2.3

Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp

Features: AsynchDNS IPv6 Largefile NTLM SSL libz

# ldd /usr/bin/curl

        linux-vdso.so.1 =>  (0x00007fff7c777000)

        libcurl.so.4 => /usr/local/curl/lib/libcurl.so.4 (0x00007fb5ed9b4000)

        libidn.so.11 => /lib64/libidn.so.11 (0x00007fb5ed772000)

        libldap-2.4.so.2 => /lib64/libldap-2.4.so.2 (0x00007fb5ed522000)

        librt.so.1 => /lib64/librt.so.1 (0x00007fb5ed31a000)

        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fb5ed0d5000)

        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fb5ecdef000)

        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fb5ecbc3000)

        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fb5ec9be000)

        libssl3.so => /usr/lib64/libssl3.so (0x00007fb5ec77f000)

        libsmime3.so => /usr/lib64/libsmime3.so (0x00007fb5ec553000)

        libnss3.so => /usr/lib64/libnss3.so (0x00007fb5ec213000)

        libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007fb5ebfe7000)

        libplds4.so => /lib64/libplds4.so (0x00007fb5ebde3000)

        libplc4.so => /lib64/libplc4.so (0x00007fb5ebbdd000)

        libnspr4.so => /lib64/libnspr4.so (0x00007fb5eb99f000)

        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fb5eb782000)

        libdl.so.2 => /lib64/libdl.so.2 (0x00007fb5eb57d000)

        libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x00007fb5eb355000)

        libz.so.1 => /lib64/libz.so.1 (0x00007fb5eb13f000)

        libc.so.6 => /lib64/libc.so.6 (0x00007fb5eadaa000)

        libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fb5eab3e000)

        libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fb5ea75b000)

        liblber-2.4.so.2 => /lib64/liblber-2.4.so.2 (0x00007fb5ea54b000)

        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fb5ea331000)

        libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007fb5ea117000)

        /lib64/ld-linux-x86-64.so.2 (0x0000564abf25b000)

        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fb5e9f0b000)

        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fb5e9d08000)

        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fb5e9ad0000)

        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fb5e98b1000)

        libfreebl3.so => /lib64/libfreebl3.so (0x00007fb5e9637000)

The Mega Guide To Harden and Secure CentOS 7

This tutorial only covers general security tips for CentOS 7 which can be used to harden the system. The checklist tips are intended to be used mostly on various types of bare-metal servers or on machines (physical or virtual) that provides network services.

Security and Hardening of CentOS 7

Security and Hardening of CentOS 7

However, some of tips can be successfully applied on general purpose machines too, such as Desktops, Laptops and card-sized single-board computers (Raspberry Pi).

Requirements

  1. CentOS 7 Minimal Installation

1. Physical Protection

Lock down your server rooms access, use racks locking and video surveillance. Take into consideration that any physical access to server rooms can expose your machine to serious security issues.

BIOS passwords can be changed by resetting jumpers on the motherboard or by disconnecting the CMOS battery. Also, an intruder can steal the hard disks or directly attach new hard disks to the motherboard interfaces (SATA, SCSI etc), boot up with a Linux live distro and clone or copy data without leaving any software trace.

2. Reduce Spying Impact

In case of highly sensitive data you should probably use advanced physical protection such as placing and locking the server into a Faraday Cage or use a military TEMPEST solution in order to minimize the impact of spying the system via radio or electrical leaking emanations.

3. Secure BIOS/UEFI

Start the process of harden your machine by securing BIOS/UEFI settings, especially set a BIOS/UEFI password and disable boot media devices (CD, DVD, disable USB support) in order to prevent an unauthorized users from modifying the system BIOS settings or altering the boot device priority and booting the machine from an alternate medium.

In order to apply this type of changes to your machine you need to consult the motherboard manufacturer manual for specific instructions.

4. Secure Boot Loader

Set a GRUB password in order to prevent malicious users to tamper with kernel boot sequence or runlevels, edit kernel parameters or start the system into single user mode in order to harm your system and reset root password to gain privileged control.

5. Use Separate Disk Partitions

When installing CentOS on systems intended as production servers use dedicated partitions or dedicated hard disks for the following parts of the system:

/(root) 
/boot  
/home  
/tmp 
/var 

6. Use LVM and RAID for Redundancy and File System Growth

The /var partition is the place where log messages are written to disk. This part of the system can exponential grow in size on heavily traffic servers which expose network services such as web servers or file servers.

Thus, use a large partition for /var or consider on setting up this partition using logical volumes (LVM) or combine several physical disks into one larger virtual RAID 0 device to sustain large amounts of data. For data redundancy consider on using LVM layout on top of RAID 1 level.

For setting up LVM or RAID on the disks, follow our useful guides:

  1. Setup Disk Storage with LVM in Linux
  2. Create LVM Disks Using vgcreate, lvcreate and lvextend
  3. Combine Several Disks into One Large Virtual Storage
  4. Create RAID 1 Using Two Disks in Linux

7. Modify fstab Options to Secure Data Partitions

Separate partitions intended for storing data and prevent the execution of programs, device files or setuid bit on these type of partitions by adding the following options to fstab file as illustrated on the below excerpt:

/dev/sda5 	 /nas          ext4    defaults,nosuid,nodev,noexec 1 2

To prevent privilege-escalation and arbitrary script execution create a separate partition for /tmp and mount it as nosuidnodev and noexec.

/dev/sda6  	/tmp         ext4    defaults,nosuid,nodev,noexec 0 0

8. Encrypt the Hard Disks at block level with LUKS

In order to protect sensitive data snooping in case of physical access to machine hard drives. I suggest you to learn how to encrypt disk by reading our article Linux Hard Disk Data Encryption with LUKS.

9. Use PGP and Public-Key Cryptography

In order to encrypt disks, use PGP and Public-Key Cryptography or openssl command to encrypt and decrypt sensitive files with a password as shown in this article Configure Encrypted Linux System Storage.

10. Install Only the Minimum Amount of Packages Required

Avoid installing unimportant or unnecessary programs, applications or services to avoid package vulnerabilities. This can decrease the risk that the compromise of a piece of software may lead to compromise other applications, parts of the system or even file systems, finally resulting in data corruption or data loss.

11. Update the system frequently

Update the system regularly. Keep Linux kernel in sync with the latest security patches and all the installed software up-to-date with the latest versions by issuing the below command:

# yum update

12. Disable Ctrl+Alt+Del

In order to prevent users to reboot the server once they have physical access to keyboard or via a Remote Console Application or a virtualized console (KVM, Virtualizing software interface) you should disable Ctrl+Alt+Del key sequence by executing the below command.

# systemctl mask ctrl-alt-del.target 

13. Remove Unnecessary Software Packages

Install minimal software required for your machine. Never install extra programs or services. Install packages only from trusted or official repositories. Use minimal installation of the system in case the machine is destined to run its entire live as a server.

Verify installed packages using one of the following commands:

# rpm -qa

Make a local list of all installed packages.

# yum list installed >> installed.txt

Consult the list for useless software and delete a package by issuing the below command:

# yum remove package_name

Read AlsoDisable and Remove Unwanted Packages on Minimal Installation of CentOS 7.

14. Restart systemd services after daemon updates

Use the below command example to restart a systemd service in order to apply new updates.

# systemctl restart httpd.service

15. Remove Unneeded Services

Identify the services that are listening on specific ports using the following command.

# ss -tulpn

To list all installed services with their output status issue the below command:

# systemctl list-units -t service

For instance, CentOS 7 default minimal installation comes with Postfix daemon installed by default which runs by the name of master under port 25. Remove Postfix network service in case your machine will not be used as a mail server.

# yum remove postfix

Read AlsoStop and Disable Unwanted Services in CentOS 7.

16. Encrypt Transmitted Data

Do not use unsecure protocols for remote access or file transfer such as TelnetFTP or other plain text high protocols such as SMTP, HTTP, NFS or SMB which, by default, does not encrypt the authentication sessions or sent data.

Use only sftpscp for file transfers and SSH or VNC over SSH tunnels for remote console connections or GUI access.

In order to tunnel a VNC console via SSH use the below example which forwards the VNC port 5901 from the remote machine to your local machine:

# ssh -L 5902:localhost:5901 remote_machine

On local machine run the below command in order to virtual connect to the remote endpoint.

# vncviewer localhost:5902

17. Network Port Scanning

Conduct external port checks using the nmap tool from a remote system over the LAN. This type of scanning can be used to verify network vulnerabilities or test the firewall rules.

# nmap -sT -O 192.168.1.10

Read AlsoLearn How to Use Nmap with these 29 Examples.

18. Packet-filtering Firewall

Use firewalld utility to protect the system ports, open or close specific services ports, especially well-known ports (<1024).

Install, start, enable and list the firewall rules by issuing the below commands:

# yum install firewalld
# systemctl start firewalld.service
# systemctl enable firewalld.service
# firewall-cmd --list-all

19. Inspect Protocol Packets with tcpdump

Use tcpdump utility in order to sniff network packets locally and inspect their content for suspicious traffic (source-destination ports, tcp/ip protocols, layer two traffic, unusual ARP requests).

For a better analysis of the tcpdump captured file use a more advanced program such as Wireshark.

# tcpdump -i eno16777736 -w tcpdump.pcap

Read Also12 tcpdump Command Examples and Analyze Network Using Wireshark Tool.

20. Prevent DNS Attacks

Inspect the contents of your resolver, typically /etc/resolv.conf file, which defines the IP address of the DNS servers it should use to query for domain names, in order to avoid man-in-the-middle attacks, unnecessary traffic for root DNS servers, spoof or create a DOS attack.

This is just the first part. On the next part we’ll discuss other security tips for CentOS 7.

Continuing the previous tutorial on how to secure CentOS 7, in this article we’ll discuss other security tips that will be presented on the below checklist.

Hardening and Securing of CentOS 7 Server

Hardening and Securing of CentOS 7 Server

Requirements

  1. The Mega Guide To Harden and Secure CentOS 7 – Part 1

21. Disable Useless SUID and SGID Commands

If the setuid and setgid bits are set on binary programs, these commands can run tasks with other user or group rights, such as root privileges which can expose seriously security issues.

Often, buffer overrun attacks can exploit such executables binaries to run unauthorized code with the rights of a root power user.

# find /  -path /proc -prune -o -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} \;

To unset the setuid bit execute the below command:

# chmod u-s /path/to/binary_file

To unset the setgid bit run the below command:

# chmod g-s /path/to/binary_file

22. Check for Unowned Files and Directories

Files or directories not owned by a valid account must be deleted or assigned with permissions from a user and group.

Issue the below command to list files or directories with no user and group.

# find / -nouser -o -nogroup -exec ls -l {} \;

23. List World-Writeable Files

Keeping word-writable file on the system can be dangerous due to the fact that anyone can modify them. Execute the below command in order to display word-writeable files, except Symlinks, which are always world-writeable.

# find / -path /proc -prune -o -perm -2 ! -type l –ls

24. Create Strong Passwords

Create a password of minimum of eight characters. The password must contain digits, special characters and uppercase letters. Use pwmake to generate a password of 128 bits from /dev/urandom file.

# pwmake 128

25. Apply Strong Password Policy

Force the system to use strong passwords by adding the below line in /etc/pam.d/passwd file.

password required pam_pwquality.so retry=3

Adding the above line, the password entered cannot contain more than 3 characters in a monotonic sequence, such as abcd, and more than 3 identical consecutive characters, such as 1111.

To force users to use a password with a minimum length of 8 characters, including all classes of characters, strength-check for character sequences and consecutive characters add the following lines to the /etc/security/pwquality.conf file.

minlen = 8
minclass = 4
maxsequence = 3
maxrepeat = 3

Sharing is Caring…

26. Use Password Aging

The chage command can be used for user password aging. To set a user’s password to expire in 45 days, use the following command:

# chage -M 45 username

To disable password expiration time use the command:

# chage -M -1 username

Force immediate password expiration (user must change password on next login) by running the following command:

# chage -d 0 username

27. Lock Accounts

User accounts can be locked by executing the passwd or usermod command:

# passwd -l username
# usermod -L username

To unlock accounts use the -u option for passwd command and -U option for usermod.

28. Prevent Accounts Shell Access

To prevent a system account (ordinary account or service account) to gain access to bash shell, change root shell to /usr/sbin/nologin or /bin/false in the /etc/passwd file by issuing the command below:

# usermod -s /bin/false username

To change the shell when creating a new user issue the following command:

# useradd -s /usr/sbin/nologin username

Read AlsoLearn 15 Examples of “useradd” Command in Linux

29. Lock Virtual User Console with vlock

vlock is a program used for locking one multiple sessions on Linux console. Install the program and start locking your terminal session by running the below commands:

# yum install vlock
# vlock

30. Use a Centralized System to Manage Accounts and Authentication

Using a centralized authentication system can greatly simplify account management and control. Services that can offer this type of account management are: IPA Server, LDAP, Kerberos, Microsoft Active Directory, Nis, Samba ADS or Winbind.

Some of these services are by default highly secured with cryptographic protocols and symmetric-key cryptography, such as Kerberos.

Read AlsoSetup NFS Server with Kerberos-based User Authentication in Linux

31. Force Read-Only Mounting of USB Media

Using blockdev utility you can force all removable media to be mounted as read-only. For instance, create a new udev configuration file named 80-readonly-usb.rules in the /etc/udev/rules.d/ directory with the following content:

SUBSYSTEM=="block",ATTRS{removable}=="1",RUN{program}="/sbin/blockdev --setro %N"

Then, apply the rule with the below command:

# udevadm control -reload

32. Disabling Root Access via TTY

To prevent the root account from performing system log-in via all console devices (tty), erase the contents of securetty file by typing the following command terminal prompt as root.

# cp /etc/securetty /etc/securetty.bak
# cat /dev/null > /etc/securetty

Remember that this rule does not apply to SSH login sessions
To prevent root login via SSH edit the file /etc/ssh/sshd_config and add the below line:

PermitRootLogin no

Read AlsoEnable or Disable SSH Root Login and Limit SSH Access
5 Best Practices to Secure and Protect SSH Server

Sharing is Caring…

33. Use POSIX ACLs to Expand System Permissions

Access Control Lists can define access rights for more than just a single user or group and can specify rights for programs, processes, files, and directories. If you set ACL on a directory, its descendants will inherit the same rights automatically.

For example,

# setfacl -m u:user:rw file
# getfacl file

Read AlsoSetup ACL and Disk Quotas for Users/Groups in Linux

34. Setup SELinux in Enforce Mode

The SELinux enhancement to the Linux kernel implements the Mandatory Access Control (MAC) policy, allowing users to define a security policy that provides granular permissions for all users, programs, processes, files, and devices.

The kernel’s access control decisions are based on all the security relevant context and not on the authenticated user identity.

To get Selinux status and enforce policy run the below commands:

# getenforce
# setenforce 1
# sestatus

Read AlsoSetup Mandatory Access Control Policy with SELinux

35. Install SELinux Additional Utilities

Install policycoreutils-python package which provides additional Python utilities for operating SELinuxaudit2allowaudit2whychcat, and semanage.

To display all boolean values together with a short description, use the following command:

# semanage boolean -l

For instance, to display and set the value of httpd_enable_ftp_server, run the below command:

# getsebool httpd_enable_ftp_server

To make the value of a boolean persist across reboots, specify the -P option to setsebool, as illustrated on the following example:

# setsebool -P httpd_enable_ftp_server on

36. Use Centralized Log Server

Configure rsyslog daemon to send sensitive utilities log messages to a centralized log server. Also, monitor log files with the help of logwatch utility.

Sending log messages to a remote server assures that once the system has been compromised, the malicious users cannot completely hide their activity, always leaving traces on remote log files.

Read Also4 Best Linux Log Monitoring and Management Tools

37. Enable Process Accounting

Enable process accounting by installing psacct utility.

Read AlsoMonitor Linux User Activity with psacct or acct Tools

Use lastcomm command to displays information about previously executed commands as recorded in the system accounting file and sa to summarize information about previously executed commands as recorded in the system accounting file.

38. Hardening /etc/sysctl.conf

Use the following kernel parameters rules to protect the system:

Disabling Source Routing

net.ipv4.conf.all.accept_source_route=0

Disable IPv4 forwarding

ipv4.conf.all.forwarding=0

Disable IPv6

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Disable the acceptance and sending of ICMP redirected packets unless specifically required.

net.ipv4.conf.all.accept_redirects=0
net.ipv4.conf.all.secure_redirects=0
net.ipv4.conf.all.send_redirects=0

Disable Reverse Path Forwarding

net.ipv4.conf.all.rp_filter=2

Ignore all ICMP echo requests (set to 1 to enable)

net.ipv4.icmp_echo_ignore_all = 0

Read AlsoSet Kernel Runtime Parameters in a Persistent and Non-Persistent Way

39. Use VPN Services to Access your Premises over Unprotected Public Networks

Always use VPN services for carriers to remotely access LAN premises over Internet. Such type of services can be configured using a free open source solution, such as OpenVPN, or using a proprietary solution, such as Cisco VPN (install vpnc command-line utility provided by Epel Repositories).

Read AlsoInstall OpenVPN Server with Windows Clients in CentOS 7

40. Perform External System Scan

Evaluate your system security for vulnerabilities by scanning the system from remote points over your LAN using specific tools such as:

  1. Nmap – network scanner 29 Examples of Nmap Command
  2. Nessus – security scanner
  3. OpenVAS – used to scan for vulnerabilities and for comprehensive vulnerability management.
  4. Nikto – an excellent common gateway interface (CGI) script scanner Scan Web Vulnerability in Linux

41. Protect System Internally

Use internal system protection against viruses, rootkits, malware and, as a good practice, install intrusion detection systems that can detect unauthorized activity (DDOS attacks, port scans), such as:

  1. AIDE – Advanced Intrusion Detection Environment – http://aide.sourceforge.net/
  2. ClamAV – Antivirus Scanner https://www.clamav.net
  3. Rkhunter – Rootkit Dcanner
  4. Lynis – Security Auditing and Scanning Tool for Linux
  5. Tripwire – Security and Data Integrity http://www.tripwire.com/
  6. Fail2Ban – Intrusion Network Prevention
  7. OSSEC – (HIDS) Host-based Intrusion Detection System http://ossec.github.io/
  8. Mod_Security – Protect Brute Force or DDoS Attacks

42. Modify User Environment Variables

Append date and time format to store commands execution by issuing the below command:

# echo 'HISTTIMEFORMAT="%d/%m/%y  %T  "' >> .bashrc'

Force to instantly record HISTFILE every time a command is typed (instead of logout):

# echo ‘PROMPT_COMMAND="history -a"’ >> .bashrc

Limit timeout login session. Automatically tear-down the shell when no activity is performed during idle time period. Very useful to automatically disconnect SSH sessions.

# echo ‘TMOUT=120’ >> .bashrc

Apply all the rules by executing:

# source .bashrc

Read Also Set User Environment Variables in Linux

43. Backup Data

Use backup utilities, such as tarcatrsyncscpLVM snapshots, etc in order to store a copy of your system, preferably offsite, in case of a system failure.

If the system gets compromised you can perform data restore from previous backups.

Finally, don’t forget that no matter how many security measures and contra-measures you take in order to keep your system safe, you will never be 100% completely secure as long as your machine is plugged-in and powered-on.

Enforce Password Complexity Policy On CentOS 7/RHEL Derivatives

In this guide, we are going to learn how to enforce password complexity policy on CentOS 7/RHEL based derivatives. Our previous guide covered the enforcement of password complexity on Ubuntu 18.04. You can check the same by following the link below;

Enforce Password Complexity Policy On CentOS 7

Similar to our previous guide, we are going to use PAM pwquality modules to enforce password complexity policy on CentOS 7/RHEL based derivatives.

In Ubuntu or Debian based derivatives, we modified the, /etc/pam.d/common-password configuration file. For CentOS 7 or similar derivatives, the /etc/security/pwquality.conf or /etc/pam.d/system-auth configuration file is used.

As our normalcy, make a backup of the configuration file before making changes just in case things go south.

cp /etc/security/pwquality.conf /etc/security/pwquality.conf.original
cp /etc/pam.d/system-auth /etc/pam.d/system-auth.original

Open the configuration file for editing.

vim /etc/pam.d/system-auth

Locate the line containing the pam_pwquality.so modules;

password    requisite     pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type=

Comment the line and replace by the line below;

password    requisite     pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= minlen=8 lcredit=-1 ucredit=-1 dcredit=-1 ocredit=-1 enforce_for_root

Where:

  • minlen=8– sets the minimum password length to 8 characters.
  • lcredit=-1 -Sets the minimum number of lower case letters that the password should contain to at least one
  • ucredit=-1-Sets the minimum number of upper case letters on a password to at least one.
  • dcredit=-1 – Sets the minimum number of digits to be contained in a password to at least one
  • ocredit=-1 – Set the minimum number of other symbols such as @, #, ! $ % etc on a password to at least one
  • enforce_for_root – Ensures that even if it is the root user that is setting the password, the complexity policies should be enforced.

You can also achieve the same by using the authconfig command line utility as shown below;

authconfig --enablereqlower --enablerequpper --enablereqdigit --enablereqother --passminlen=8 --passmaxrepeat=3 --update

The above command basically ensures that password should have at least (in the respective order);

  • one lower case letter
  • one upper case letter
  • one digit
  • an alphanumeric character.
  • 8 characters in length
  • no more than 3 characters similar to the previous password.

The changes will updated on /etc/security/pwquality.conf.

tail /etc/security/pwquality.conf
# Path to the cracklib dictionaries. Default is to use the cracklib default.
# dictpath =
minlen = 8
minclass = 1
maxrepeat = 3
maxclassrepeat = 0
lcredit = -1
ucredit = -1
dcredit = -1
ocredit = -1

Note that root or any user with sudo rights can always set any password irrespective of the enforced policies. However, to ensure that the password complexity policies applies to both root and user with sudo, you must append the enforce_for_root option to the line below on /etc/pam.d/system-auth.

password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= enforce_for_root

Testing Password Enforcement Policy

As a root user, try to change the password of a user with a password that doesn’t meet the set credentials.

[root@Cent7 ~]# passwd amos
Changing password for user amos.
New password: @moskifaru1
BAD PASSWORD: The password contains less than 1 uppercase letters
New password: @mosKifaru
BAD PASSWORD: The password contains less than 1 digits
New password: mosKifaru1
BAD PASSWORD: The password contains less than 1 non-alphanumeric characters
passwd: Have exhausted maximum number of retries for service

Test using a more complex password; @mosKifaru1

[root@Cent7 ~]# passwd amos
Changing password for user amos.
New password: @mosKifaru1
Retype new password: @mosKifaru1
passwd: all authentication tokens updated successfully.

That is all about how to enforce password complexity policy on CentOS 7. Enjoy.

Linux下进程/程序网络带宽占用情况查看工具 — NetHogs

nethogs命令

有很多适用于Linux系统的开源网络监视工具。比如说,你可以用命令iftop来检查带宽使用情况。netstat用来查看接口统计报告,还有top监控系统当前运行进程。但是如果你想要找一个能够按进程实时统计网络带宽利用率的工具,那么NetHogs值得一看。

NetHogs是一个开源的命令行工具(类似于Linux的top命令),用来按进程或程序实时统计网络带宽使用率。

来自NetHogs项目网站:

NetHogs是一个小型的net top工具,不像大多数工具那样拖慢每个协议或者是每个子网的速度而是按照进程进行带宽分组。NetHogs不需要依赖载入某个特殊的内核模块。如果发生了网络阻塞你可以启动NetHogs立即看到哪个PID造成的这种状况。这样就很容易找出哪个程序跑飞了然后突然占用你的带宽。

本文为你介绍如何在Unix/Linux操作系统下如何安装和使用NetHogs按进程监控网络带宽使用率。

在RHEL、CentOS和Fedora下安装NetHogs

要安装NetHogs,你必须要启用你所用Linux下的EPEL源。然后运行下面的yum命令下载安装NetHogs包。
# yum install nethogs

在Ubuntu、Linux mint和Debian下安装NetHogs

键入apt-get命令安装NetHogs包:
$ sudo apt-get install nethogs

NetHogs用法

在基于RedHat系统下键入如下命令启动NetHogs工具。
# nethogs

在Debian/Ubuntu/Linux Mint下要执行NetHogs你必须拥有root权限:
$ sudo nethogs

nethogs

Ubuntu 12.10 下的NetHogs预览

正如上图所示,send列和received列显示的是按照每个进程的流量统计。总的收发数据带宽在最下方,而且可以用交互命令控制排序,下面将要讨论这些交互命令。

NetHogs 命令行参数

以下就是NetHogs命令行的参数,用-d来添加刷新频率参数,device name 用来检测给定的某个或者某些设备的带宽(默认是eth0)。例如:设置5秒钟的刷新频率,键入如下命令即可:
# nethogs -d 5

$ sudo nethogs -d 5

如果只用来监视设备(eth0)的网络带宽可以使用如下命令:
# nethogs eth0

$ sudo nethogs eth0

如果要同时监视eth0和eth1接口,使用以下命令即可:
# nethogs eth0 eth1

$ sudo nethogs eth0 eth1

其他参数和用法
-d : 刷新间隔
-h : 帮助
-p : promiscious 模式
-t : trace模式
-V : 版本

交互命令

以下是NetHogs的一些交互命令(键盘快捷键)
• m : 修改单位
• r : 按流量排序
• s : 按发送流量排序
• q : 退出命令提示符

关于NetHogs命令行工具的完整参数列表,可以参考NetHogs的手册,使用方法是在终端里输入man nethogs或者sudo man nethogs,更多信息请参考NetHogs项目主页

之前VPS侦探曾经介绍过流量带宽相关的工具如:iftop、vnstat,这几个都是统计和监控网卡流量的。但是当我们的服务器或 VPS的带宽被大量占用或占满,却没找不到称心的工具或程序来查看到底是哪个程序或进程占有率多少带宽。虽然在Windows上查看进程占用带宽情况的软件很多,像某3**、某Q家的电脑管家、IP雷达等。但是Linux下这一类软件很少,今天我们介绍的就是Linux的一款查看进程带宽网络占用的软件:NetHogs。

安装
Debian/Ubuntu下安装很简单,执行:apt-get install nethogs 就可以安装。

CentOS/RHEL下建议先安装上EPEL,再执行:yum install libpcap nethogs 进行安装。

具体使用参数说明:

[root@vpser ~]# nethogs -h
usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [device [device [device …]]] //nethogs可以使用的参数
-V : prints version.//打印版本信息
-d : delay for update refresh rate in seconds. default is 1. //延迟刷新时间,单位秒,默认1秒
-t : tracemode. //跟踪模式
-b : bughunt mode – implies tracemode. //bughunt模式
-p : sniff in promiscious mode (not recommended). //混合模式下嗅探,不推荐
device : device(s) to monitor. default is eth0 //监听的设备,默认是eth0,也就是网卡设备名称,如果是openvz的vps一般都是venet0,具体可以ifconfig进行查看,lo为本地回环,用不到。多个网卡可以一块写上,空格隔开。

When nethogs is running, press: //nethogs运行是可以使用以下按键进行操作
q: quit //运行时,按 q 键退出
m: switch between total and kb/s mode //按 m 键,切换单位或显示进程占用速度或已统计使用的流量。切换顺序是KB/sec->KB->B->MB
r : Sort by received. //按received进行排序
s : Sort by sent. //按send进行排序

使用例子:nethogs venet0

如上图,PID一列就是进程的PID,PROGRAM就是显示进程或连接双方的端口号,前面红框是服务器的IP:端口,后面红框是客户端的IP:端口,如图根据端口可以判断,目前有7700端口,如果不知道端口对应的进程可以通过lsof来进行查看。

lsof -i:被占用的端口号 如: lsof -i:80

ps aux|grep PID

DEV列显示设备名,SEND是服务器发送的流量,RECEIVED服务器接收的流量。 // 按 m 可以切换统计方式 : 流量速度KB/sec 或 总数{KB /B /m}

How to Install and Configure ‘Cache Only DNS Server’ with ‘Unbound’ in RHEL/CentOS 7

Version:1.0 StartHTML:000000222 EndHTML:000042418 StartFragment:000012983 EndFragment:000042312 StartSelection:000012983 EndSelection:000042308 SourceURL:https://www.tecmint.com/setup-dns-cache-server-in-centos-7/ How to Install and Configure ‘Cache Only DNS Server’ with ‘Unbound’ in RHEL/CentOS 7

Caching name servers using ‘Unbound‘ ( is a validating, recursive, and caching DNS server software ), back in RHEL/CentOS 6.x (where x is version number), we used bind software to configure DNS servers.

Here in this article, we are going to use ‘unbound‘ caching software to install and configure a DNS Server in RHEL/CentOS 7 systems.

Setup Cahing DNS Server in RHEL/CentOS 7

DNS cache servers are used to resolve any DNS query they receive. If the server caches the query and in future the same queries requested by any clients the request will be delivered from DNS ‘unbound‘ cache, this can be done in milliseconds than the first time it resolved.

Caching will only act as a agent to resolve the query of client from any one of the forwarders. Using caching server, will reduce the loading time of webpages by keeping the cache database in unbound server.

My Server and Client Setup

For demonstration purpose, I will be using two systems. The first system will act as a Master (Primary) DNS server and the second system will act as a local DNS client.

Master DNS Server
Operating System   :    CentOS Linux release 7.0.1406 (Core)
IP Address	   :	192.168.0.50
Host-name	   :	ns.tecmintlocal.com
Client Machine
Operating System   :	CentOS 6
IP Address	   :	192.168.0.100
Host-name	   :	client.tecmintlocal.com

Step 1: Check System Hostname and IP

1. Before setting up a caching DNS server, make sure that you’ve added correct hostname and configured correct static IP address for your system, if not set the system static IP address.

2. After, setting correct hostname and static IP address, you can verify them with the help of following commands.

# hostnamectl
# ip addr show | grep inet

Check IP Address

Step 2: Installing and Configuring Unbound

3. Before installing ‘Unbound’ package, we must update the our system to latest version, after that we can install the unbound package.

# yum update -y
# yum install unbound -y

4. After package has been installed, make a copy of the unbound configuration file before making any changes to original file.

# cp /etc/unbound/unbound.conf /etc/unbound/unbound.conf.original

5. Next, use any of your favorite text editor to open and edit ‘unbound.conf‘ configuration file.

# vim /etc/unbound/unbound.conf
Copy Unbound DNS Configuration

Once the file is opened for editing, make the following changes:

Interfaces

Search for Interface and enable the interface which we going to use or if our server have multiple interfaces we have to enable the interface 0.0.0.0.

Here Our server IP was 192.168.0.50, So, i’am going to use unbound in this interface.

Interface 192.168.0.50
Enable IPv4 and Protocol Supports

Search for the following string and make it ‘Yes‘.

do-ip4: yes
do-udp: yes
do-tcp: yes
Enable the logging

To enable the log, add the variable as below, it will log every unbound activities.

logfile: /var/log/unbound
Hide Identity and Version

Enable following parameter to hide id.server and hostname.bind queries.

hide-identity: yes

Enable following parameter to hide version.server and version.bind queries.

hide-version: yes
Access Control

Then search for access-control to allow. This is to allow which clients are allowed to query this unbound server.

Here I have used 0.0.0.0, that means anyone send query to this server. If we need to refuse query for some range of network we can define which network need to be refuse from unbound queries.

access-control: 0.0.0.0/0 allow

Note: Instead of allow, we can replace it with allow_snoop this will enable some additional parameters like dig and it support both recursive and non recursive.

Domain Insecure

Then search for domain-insecure. If our domain is works with DNS sec keys, we need to define our server available for domain-insecure. Here our domain will be treated as insecure.

domain-insecure: "tecmintlocal.com
Forward Zones

Then change the forwarders for our requested query not fulfilled by this server it will forward to root domain (. ) and resolve the query.

forward-zone:
        name: "."
        forward-addr: 8.8.8.8
        forward-addr: 8.8.4.4

Finally, save and quit the configuration file using wq!.

6. After making the above configuration, now verify the unbound.conf file for any errors using the following command.

# unbound-checkconf /etc/unbound/unbound.conf

Check Unbound DNS Configuration

7. After file verification over without any errors, you can safely restart the ‘unbound’ service and enable it at system startup.

# systemctl start unbound.service
# sudo systemctl enable unbound.service

Start Unbound DNS Service

Step 3: Test DNS Cache Locally

8. Now it’s time to check our DNS cache, by doing a ‘drill’ (query) one ‘india.com‘ domain. At first the ‘drill‘ command results for ‘india.com‘ domain will take some milliseconds, and then do a second drill and have a note on Query time it takes for both drills.

drill india.com @192.168.0.50

Check DNS Cache Locally

Did you see in the above output, the first query taken almost 262 msec to resolve and the second query takes 0 msec to resolve domain (india.com).

That means, the first query gets cached in our DNS Cache, so when we run ‘drill’ second time the query served from our local DNS cache, this way we can improve loading speed of websites.

Step 4: Flush Iptables and Add Firewalld Rules

9. We can’t use both iptables and firewalld at same time on same machine, if we do both will conflict with each other, thus removing ipables rules will be a good idea. To remove or flush the iptables, use the following command.

# iptables -F

10. After removing iptables rules permanently, now add the DNS service to firewalld list permanently.

# firewall-cmd --add-service=dns
# firewall-cmd --add-service=dns --permanent

11. After adding DNS service rules, list the rules and confirm.

# firewall-cmd --list-all

Add DNS to Firewalld

Step 5: Managing and Troubleshooting Unbound

12. To get the current server status, use the following command.

# unbound-control status

Check Unbound DNS Status

Dumping DNS Cache

13. If in-case you would like to have a dump of a DNS cache information in a text file, you can redirect it to some file using below command for future use.

 # unbound-control dump_cache > /tmp/DNS_cache.txt

Backup DNS Cache

14. To restore or import the cache from the dumped file, you can use following command.

# unbound-control dump_cache < /tmp/DNS_cache.txt

Restore DNS Cache

Flushing DNS Records

15. To check whether the specific address was resolved by our forwarders in unbound cache Server, use the below command.

# unbound-control lookup google.com

Check DNS Lookup

16. Some times if our DNS cache server will not reply our query, in mean time we can use to flush the cache to remove information such as A, AAA, NS, SO, CNAME, MX, PTR etc.. records from DNS cache. We can remove all information using flush_zone this will remove all informations.

# unbound-control flush www.digitalocean.com
# unbound-control flush_zone tecmintlocal.com

17. To check which forwards are currently used to resolve.

# unbound-control list_forwards

Check Current DNS Forwards

Step 6: Client Side DNS Configuration

18. Here I’ve used a CentOS 6 server as my client machine, IP for this machine is 192.168.0.100 and I’m going to use my unbound DNS server IP (i.e Primary DNS) in it’s interface configuration.

Log-into the Client machine and set the Primary DNS server IP to our unbound server’s IP.

Run the setup command and choose network configuration from TUI network manager.

Then choose DNS configuration, insert the unbound DNS server’s IP as Primary DNS, but here i have used both in Primary and Secondary because I don’t have any other DNS server.

Primary DNS	: 192.168.0.50
Secondary DNS	: 192.168.0.50

Select Network Configuration

Select DNS Configuration

Enter DNS IP Address

Click OK –> Save&Quit –> Quit.

19. After adding Primary and Secondary DNS IP addresses, now it’s time to restart the network using following command.

# /etc/init.d/network restart

Restart Network

20. Now time to access any one of the website from client machine and check for the cache in unbound DNS server.

# elinks aol.com
# dig aol.com

Check Website

Query Website

Conclusion

Earlier we were used to setup DNS cache server using bind package in RHEL and CentOS systems. Now, we have seen how to setup a DNS cache server using unbound package. Hope this will resolve your query request quicker than the bind pacakge.

Linux 磁盘IO性能测试工具:FIO

1、FIO安装

CentOS下:

$ yum install fio

2、FIO简介

FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证。
项目主页:https://github.com/axboe/fio
FIO输出结果详细解释可参见:https://tobert.github.io/post/2014-04-17-fio-output-explained.html
注:fio用于测试磁盘性能,不是文件系统,测试之前需要先把要测试的磁盘卸载掉,测试完后需格式化一遍再挂载上去。相关命令如下:

# 卸载磁盘
umount /mnt/datadir1
# 格式化磁盘
mkfs.ext4 /dev/sdd
# 挂载磁盘
mount -t ext4 /dev/sdd /mnt/datadir1

# 自动挂载,在里面添加该盘
vim /etc/fstab

3、FIO参数

例如:

$ fio -filename=/dev/sda -direct=1 -iodepth 1 -thread -rw=read -ioengine=psync -bs=16k -size=200G -numjobs=30 -runtime=1000 -group_reporting -name=mytest1

说明:
filename=/dev/sdd 测试文件名称,通常选择需要测试的盘的data目录。
direct=1 测试过程绕过机器自带的buffer。使测试结果更真实。
rw=randwrite 测试模式
=read 顺序读
=write 顺序写
=randwrite 随机写
=randread 随机读
=rw,readwrite 顺序混合读写
=randrw 随机混合读写

bs=4k 单次io的块文件大小为4k
bsrange=512-2048 同上,提定数据块的大小范围
size=5G 本次的测试文件大小为5g,以每次4k的io进行测试。
numjobs=30 本次的测试线程为30个
runtime=1000 测试时间为1000秒,如果不写则一直将5g文件分4k每次写完为止
iodepth 队列深度,只有使用libaio时才有意义。这是一个可以影响IOPS的参数。
ioengine=psync io引擎使用psync方式
=libaio Linux专有的异步IO

rwmixwrite=30 在混合读写的模式下,写占30%
group_reporting 关于显示结果的,汇总每个进程的信息。
lockmem=1G 只使用1g内存进行测试。
zero_buffers 用0初始化系统buffer。
nrfiles=8 每个进程生成文件的数量。

点击查看更多详细参数说明

4、FIO测试示例

fio工具使用特别注意:

  1. 请不要在系统盘上进行 fio 测试,避免损坏系统重要文件
  2. fio测试建议在空闲的、未保存重要数据的硬盘上进行,并在测试完后重新制作文件系统。请不要在业务数据硬盘上测试,避免底层文件系统元数据损坏导致数据损坏
  3. 测试硬盘性能时,推荐直接测试裸盘(如 vdb);测试文件系统性能时,推荐指定具体文件测试(如 /data/file)

4.1 顺序读测试

$ fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=read -filename=/dev/sdd -name=”BS 4KB read test” -iodepth=16 -runtime=601

4.2 顺序写测试

$ fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=write -filename=/dev/sdd -name=”BS 4KB write test” -iodepth=16 -runtime=601

4.3 随机读测试

$ fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randread -filename=/dev/sdd -name=”BS 4KB randread test” -iodepth=16 -runtime=601

4.4 随机写测试

$ fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randwrite -filename=/dev/sdd -name=”BS 4KB randwrite test” -iodepth=16 -runtime=601

4.5 随机读写混合测试

$ fio -ioengine=libaio -bs=4k -direct=1 -thread -rw=randrw -rwmixread=70 -filename=/dev/sdd -name=”BS 4KB randrw 70 test” -iodepth=16 -runtime=601

5、FIO输出结果

例如:

BS 4KB randread test: (g=0): rw=randread, bs=4K-4K/4K-4K/4K-4K, ioengine=libaio, iodepth=1
fio-2.1.10
Starting 1 thread

BS 4KB randread test: (groupid=0, jobs=1): err= 0: pid=26408: Mon Feb 27 16:43:07 2017
read : io=19908KB, bw=339689B/s, iops=82, runt= 60013msec
# fio做了19908KB的IO,bandwidth速率为339689B/s,总IOPS为82,运行时间为60013毫秒

slat (usec): min=18, max=97, avg=23.60, stdev= 2.79
# slat (submission latency):代表 盘需要多久将IO提交到kernel做处理
# usec 微秒

clat (msec): min=1, max=23, avg=12.03, stdev= 3.79
# clat (completion latency):命令提交到kernel到IO做完之间的时间,不包括submission latency

 lat (msec): min=1, max=23, avg=12.05, stdev= 3.79
# 从IO结构体创建时刻开始,直到紧接着clat完成的时间

clat percentiles (usec):
 |  1.00th=[ 4080],  5.00th=[ 5984], 10.00th=[ 7136], 20.00th=[ 8640],
 | 30.00th=[ 9920], 40.00th=[10944], 50.00th=[11840], 60.00th=[12992],
 | 70.00th=[14016], 80.00th=[15296], 90.00th=[17024], 95.00th=[18560],
 | 99.00th=[20864], 99.50th=[21632], 99.90th=[22400], 99.95th=[22912],
 | 99.99th=[23168]
# Completion latency百分数

bw (KB  /s): min=  276, max=  373, per=100.00%, avg=331.54, stdev=18.83
# 带宽(bandwidth)

lat (msec) : 2=0.02%, 4=0.82%, 10=30.32%, 20=66.75%, 50=2.09%
# 30.32%的request延迟在4~10毫秒,66.75%的request延迟在10~20毫秒,以此类推

cpu : usr=0.10%, sys=0.25%, ctx=5057, majf=0, minf=8
# 用户/系统CPU占用率,进程上下文切换(context switch)次数,主要和次要(major and minor)页面错误数量(page faults)。由于测试是配置成使用直接IO,page faults数量应该极少。

IO depths : 1=100.0%, 2=0.0%, 4=0.0%, 8=0.0%, 16=0.0%, 32=0.0%, >=64=0.0%
# iodepth设置,用来控制同一时刻发送给OS多少个IO。这完全是纯应用层面的行为,和盘的IO queue不是一回事

 submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
 complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
 #submit和complete代表同一时间段内fio发送上去和已完成的IO数量

 issued    : total=r=4977/w=0/d=0, short=r=0/w=0/d=0
 # 发送的IO数量

 latency   : target=0, window=0, percentile=100.00%, depth=1
 # Fio可以配置一个延迟目标值,这个值可以调节吞吐量直到达到预设的延迟目标

Run status group 0 (all jobs):

Fio支持把不同的测试聚合

READ: io=19908KB, aggrb=331KB/s, minb=331KB/s, maxb=331KB/s, mint=60013msec, maxt=60013msec
# 汇总输出吞吐量和时间。
# io=表示总共完成的IO数量。在基于时间的测试中这是一个变量,在基于容量的测试中,这个值能匹配size参数。
# aggrb是所有进程/设备的汇总带宽。
# minb/maxb表示测量到的最小/最大带宽。
# mint/maxt表示测试的最短和最长耗时。和io=参数类似,时间值对于基于时间的测试应该能匹配runtime参数,对于基于容量的测试是一个变量。

Disk stats (read/write):
sdd: ios=4969/0, merge=0/0, ticks=59703/0, in_queue=59702, util=99.51%

Lynis:面向Linux系统的安全审查和扫描工具

 

Lynis是一款功能非常强大的开源审查工具,面向类似Unix/Linux的操作系统。它可以扫描系统,查找安全信息、一般的系统信息、已安装软件及可用软件信息、配置错误、安全问题、没有设密码的用户帐户、错误的文件许可权限以及防火墙审查等。

Lynis是一款功能非常强大的开源审查工具,面向类似Unix/Linux的操作系统。它可以扫描系统,查找安全信息、一般的系统信息、已安装软件及可用软件信息、配置错误、安全问题、没有设密码的用户帐户、错误的文件许可权限以及防火墙审查等。

Lynis 2.2.0 :面向Linux系统的安全审查和扫描工具

Lynis是最可靠的自动化审查工具之一,可用于基于Unix/Linux的系统中的软件补丁管理、恶意软件扫描和安全漏洞检测。这款工具适用于审查人员、网络及系统管理员、安全专家和渗透测试人员。

经过几个月开发后,现在发布了一个新的主要升级版:Lynis 2.2.0,它随带一些新的功能和测试以及许多小的改进之处。我鼓励所有Linux用户测试并升级到Lynis的这个最新版本。

我们在本文中将介绍在Linux系统中如何使用tarball源文件安装Lynis 2.2.0(Linux审查工具)。 继续阅读

Nginx配置WebService、MySQL、SQL Server、ORACLE等代理

nginx配置webservice

#user  nobody;
worker_processes  4;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    upstream esbServer {   
        server 127.0.0.1:8083 weight=1 max_fails=2 fail_timeout=30s;   
    }

    #gzip  on;

    server {
        listen       8081;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /ladder_web {
            proxy_set_header X-real-ip $remote_addr;
            proxy_pass http://esbServer;
        }

       
    }

}

nginx 配置mysql代理 — 基于nginx1.9以上 stream module 继续阅读