[root@centos8 ~]# openssl version OpenSSL 1.1.1 FIPS 11 Sep 2018
[root@centos8 ~]# openssl help Standard commands asn1parse ca ciphers cms crl crl2pkcs7 dgst dhparam dsa dsaparam ec ecparam enc engine errstr gendsa genpkey genrsa help list nseq ocsp passwd pkcs12 pkcs7 pkcs8 pkey pkeyparam
[root@centos8 ~]# openssl OpenSSL> help Standard commands asn1parse ca ciphers cms crl crl2pkcs7 dgst dhparam ...... OpenSSL> ca --help Usage: ca [options] Valid options are: -help Display this summary -verbose Verbose output during processing -config val A config file ...... OpenSSL>q
[root@centos8 ~]#openssl passwd --help Usage: passwd [options] Valid options are: -help Display this summary -in infile Read passwords from file -noverify Never verify when reading password from terminal -quiet No warnings -table Format output as table -reverse Switch table columns -salt val Use provided salt -stdin Read passwords from stdin -6 SHA512-based password algorithm -5 SHA256-based password algorithm -apr1 MD5-based password algorithm, Apache variant -1 MD5-based password algorithm -aixmd5 AIX MD5-based password algorithm -crypt Standard Unix password algorithm (default) -rand val Load the file(s) into the random number generator -writerand outfile Write random data to the specified file
[root@centos7 ~]# openssl passwd --help Usage: passwd [options] [passwords] where options are -crypt standard Unix password algorithm (default) -1 MD5-based password algorithm -apr1 MD5-based password algorithm, Apache variant -salt string use provided salt -in file read passwords from file -stdin read passwords from stdin -noverify never verify when reading password from terminal -quiet no warnings -table format output as table -reverse switch table columns
范例:
1 2 3 4 5 6 7 8
[root@centos8 ~]#getent shadow wang wang:$6$Y16DiwuVQtL6XCQK$DAQO4BhVbfQmaUMFWKR61hVwFvxk7J9U4pZaFcwf6nBwERUN6bL3wALPonDRebk3CgooupeXHfRuFKRciUe6q.:18373:0:99999:7:::
[root@centos8 ~]#openssl genrsa -out /data/app.key -des3 1024 Generating RSA private key, 1024 bit long modulus (2 primes) ......+++++ ...........+++++ e is 65537 (0x010001) Enter pass phrase for /data/app.key: Verifying - Enter pass phrase for /data/app.key:
[root@centos8 ~]#ls -l /data total 4 -rw------- 1 root root 963 Feb 3 15:27 app.key
dir = /etc/pki/CA # Where everything is kept certs = $dir/certs # Where the issued certs are kept crl_dir = $dir/crl # Where the issued crl are kept database = $dir/index.txt # database index file. #unique_subject = no # Set to 'no' to allow creation of # several certs with same subject. new_certs_dir = $dir/newcerts # default place for new certs.
certificate = $dir/cacert.pem # The CA certificate serial = $dir/serial # The current serial number crlnumber = $dir/crlnumber # the current crl number # must be commented out to leave a V1 CRL crl = $dir/crl.pem # The current CRL private_key = $dir/private/cakey.pem# The private key RANDFILE = $dir/private/.rand # private random number file x509_extensions = usr_cert # The extensions to add to the cert
# Comment out the following two lines for the "traditional" # (and highly broken) format. name_opt = ca_default # Subject Name options cert_opt = ca_default # Certificate field options
default_days = 365 # how long to certify for default_crl_days= 30 # how long before next CRL default_md = sha256 # use SHA-256 by default preserve = no # keep passed DN ordering
policy = policy_match
# For the CA policy [ policy_match ] countryName = match stateOrProvinceName = match organizationName = match organizationalUnitName = optional commonName = supplied emailAddress = optional
# For the 'anything' policy # At this point in time, you must list all acceptable 'object' # types. [ policy_anything ] countryName = optional stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional ......
创建私有CA
1、创建CA所需要的文件
1 2 3 4 5
#生成证书索引数据库文件 touch /etc/pki/CA/index.txt
#指定第一个颁发证书的序列号 echo 01 > /etc/pki/CA/serial
2、 生成CA私钥
1 2
cd /etc/pki/CA/ (umask 066; openssl genrsa -out private/cakey.pem 2048)
[root@centos7 ~]#cd /etc/pki/tls/certs [root@centos7 certs]# make This makefile allows you to create: o public/private key pairs o SSL certificate signing requests (CSRs) o self-signed SSL test certificates
To create a key pair, run "make SOMETHING.key". To create a CSR, run "make SOMETHING.csr". To create a test certificate, run "make SOMETHING.crt". To create a key and a test certificate in one file, run "make SOMETHING.pem".
To create a key for use with Apache, run "make genkey". To create a CSR for use with Apache, run "make certreq". To create a test certificate for use with Apache, run "make testcert".
To create a test certificate with serial number other than random, add SERIAL=num You can also specify key length with KEYLEN=n and expiration in days with DAYS=n Any additional options can be passed to openssl req via EXTRA_FLAGS
Examples: make server.key make server.csr make server.crt make stunnel.pem make genkey make certreq make testcert make server.crt SERIAL=1 make stunnel.pem EXTRA_FLAGS=-sha384 make testcert DAYS=600
[root@centos7 certs]# ls ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert
usage: @echo"This makefile allows you to create:" @echo" o public/private key pairs" @echo" o SSL certificate signing requests (CSRs)" @echo" o self-signed SSL test certificates" @echo @echo"To create a key pair, run \"make SOMETHING.key\"." @echo"To create a CSR, run \"make SOMETHING.csr\"." @echo"To create a test certificate, run \"make SOMETHING.crt\"." @echo"To create a key and a test certificate in one file, run \"make SOMETHING.pem\"." @echo @echo"To create a key for use with Apache, run \"make genkey\"." @echo"To create a CSR for use with Apache, run \"make certreq\"." @echo"To create a test certificate for use with Apache, run \"make testcert\"." @echo @echo"To create a test certificate with serial number other than random, add SERIAL=num" @echo"You can also specify key length with KEYLEN=n and expiration in days with DAYS=n" @echo"Any additional options can be passed to openssl req via EXTRA_FLAGS" @echo @echo Examples: @echo" make server.key" @echo" make server.csr" @echo" make server.crt" @echo" make stunnel.pem" @echo" make genkey" @echo" make certreq" @echo" make testcert" @echo" make server.crt SERIAL=1" @echo" make stunnel.pem EXTRA_FLAGS=-sha384" @echo" make testcert DAYS=600"
[root@centos7 certs]# make app.crt umask 77 ; \ /usr/bin/openssl genrsa -aes128 2048 > app.key Generating RSA private key, 2048 bit long modulus ...............+++ ............................................+++ e is 65537 (0x10001) Enter pass phrase: Verifying - Enter pass phrase: umask 77 ; \ /usr/bin/openssl req -utf8 -new -key app.key -x509 -days 365 -out app.crt Enter pass phrase for app.key: You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:hubei Locality Name (eg, city) [Default City]:wuhan Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:it Common Name (eg, your name or your server's hostname) []:www.magedu.org Email Address []:admin@magedu.org [root@centos7 certs]#ls app.crt app.key ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert [root@centos7 certs]#openssl x509 -in app.crt -noout -text Certificate: Data: Version: 3 (0x2) Serial Number: 90:d7:97:6a:21:21:f8:5e Signature Algorithm: sha256WithRSAEncryption Issuer: C=CN, ST=hubei, L=wuhan, O=magedu, OU=it, CN=www.magedu.org/emailAddress=admin@magedu.org Validity Not Before: Feb 5 00:28:31 2020 GMT Not After : Feb 4 00:28:31 2021 GMT Subject: C=CN, ST=hubei, L=wuhan, O=magedu, OU=it, CN=www.magedu.org/emailAddress=admin@magedu.org Subject Public Key Info: Public Key Algorithm: rsaEncryption Public-Key: (2048 bit) Modulus: 00:f8:dd:d3:ea:0b:f1:97:0f:27:de:44:a2:32:77: fb:5c:73:74:17:7b:5f:a4:9c:a2:d4:3b:d4:49:4c: da:e0:a2:6a:41:05:6e:10:1e:96:dc:95:34:ed:08: 05:18:ba:27:c5:e5:f0:7c:65:15:78:f8:9b:bf:ee: 41:ef:1c:6f:7f:35:29:fd:f5:cf:4a:f1:36:7e:0c: 37:96:b1:01:e5:aa:7f:6e:a0:56:b0:33:28:ed:db: 7a:56:34:67:83:be:bd:ad:3d:e7:80:d9:cf:6a:c7: c9:7f:d1:83:73:33:7f:77:27:a5:2e:17:84:82:c7: 50:3d:20:d8:20:f1:5e:61:d2:69:07:8f:0e:cd:ea: c2:51:bd:aa:a0:ce:61:18:6f:00:43:13:21:8d:6d: 3b:85:13:d8:93:ed:fc:65:28:ec:12:d1:67:40:d0: 98:54:9a:59:1e:10:4f:c5:8c:b5:b1:26:55:2f:e1: 53:1d:6b:71:88:64:e2:b1:21:28:8c:c7:04:3a:70: 87:c7:48:41:44:95:43:2f:e8:da:5f:f8:93:1a:9a: de:e4:e3:82:57:60:6a:49:08:2e:5f:57:f7:62:b2: bb:8a:1f:8b:2b:dc:40:dd:35:30:42:c1:f4:c6:1a: 0b:61:df:37:ed:bd:25:39:4c:5f:27:32:57:9e:d0: 11:9d Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: 28:48:D7:B5:02:7E:D7:4B:A1:74:A7:86:4B:3C:E5:FC:39:7B:F4:2E X509v3 Authority Key Identifier: keyid:28:48:D7:B5:02:7E:D7:4B:A1:74:A7:86:4B:3C:E5:FC:39:7B:F4:2E X509v3 Basic Constraints: CA:TRUE Signature Algorithm: sha256WithRSAEncryption a3:66:1b:85:dc:9e:1b:c7:c8:e4:29:3c:32:b2:fc:71:c9:79: 9e:ad:db:78:bd:a4:42:1a:ef:d7:7f:4a:84:d9:46:e1:60:fa: 9f:04:83:67:88:74:fd:99:d2:e3:7b:34:86:27:a1:d0:3c:be: 5f:93:d0:17:e9:d1:f6:19:2b:d5:e7:48:1f:56:ac:65:22:ec: 64:6f:a3:05:0c:83:2f:29:a8:ef:cc:25:51:d0:16:21:93:9e: 85:fc:82:d4:8c:ba:14:47:6e:fd:33:44:71:a7:c4:7f:92:2a: 01:40:f9:69:70:73:27:89:73:82:ea:21:95:48:e2:c1:5d:b8: ed:e7:61:49:88:1c:b6:8a:a6:bd:cc:83:6b:2c:19:b9:07:21: 46:f8:1f:dc:cb:3c:9c:6d:b9:b1:dc:03:b0:5a:00:de:41:7c: 96:d8:3a:f3:06:fc:24:03:60:54:35:85:a2:1e:79:fc:cb:6e: fd:e2:c3:7b:16:6e:7c:56:17:d4:64:c9:15:e9:a4:b0:9a:a7: c5:d6:f8:c8:e4:99:b1:b0:f0:8b:b4:ea:8e:a9:29:c1:4a:19: 69:7a:d7:51:93:23:51:b6:0b:63:e1:45:a7:3f:65:4d:89:55: e8:52:29:0a:41:d2:fb:76:20:7e:14:da:a8:ad:e6:fc:b0:a9: 5f:10:b0:d3
实战案例:在CentOS8上实现私有CA和证书申请
创建CA相关目录和文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[root@centos8 ~]#mkdir -pv /etc/pki/CA/{certs,crl,newcerts,private} mkdir: created directory '/etc/pki/CA' mkdir: created directory '/etc/pki/CA/certs' mkdir: created directory '/etc/pki/CA/crl' mkdir: created directory '/etc/pki/CA/newcerts' mkdir: created directory '/etc/pki/CA/private'
[root@centos8 ~]#openssl ca -in /data/app1/app1.csr -out /etc/pki/CA/certs/app1.crt -days 1000 Using configuration from /etc/pki/tls/openssl.cnf 140040142845760:error:02001002:system library:fopen:No such file or directory:crypto/bio/bss_file.c:72:fopen('/etc/pki/CA/index.txt','r') 140040142845760:error:2006D080:BIO routines:BIO_new_file:no such file:crypto/bio/bss_file.c:79:
[root@centos8 ~]#openssl ca -in /data/app1/app1.csr -out /etc/pki/CA/certs/app1.crt -days 1000 Using configuration from /etc/pki/tls/openssl.cnf /etc/pki/CA/serial: No such file or directory error while loading serial number 140240559408960:error:02001002:system library:fopen:No such file or directory:crypto/bio/bss_file.c:72:fopen('/etc/pki/CA/serial','r') 140240559408960:error:2006D080:BIO routines:BIO_new_file:no such file:crypto/bio/bss_file.c:79:
[root@centos8 ~]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:devops Common Name (eg, your name or your server's hostname) []:ca.magedu.org Email Address []:admin@magedu.org [root@centos8 ~]#tree /etc/pki/CA /etc/pki/CA ├── cacert.pem ├── certs ├── crl ├── newcerts └── private └── cakey.pem 4 directories, 2 files [root@centos8 ~]#cat /etc/pki/CA/cacert.pem -----BEGIN CERTIFICATE----- MIID+zCCAuOgAwIBAgIUOgPr416WnD8XXPst9HsDrXxCU+swDQYJ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XyOBlQc8CzTxL9BJcykJHWDihYYWeixorfKjzkJ9C4y poG/bIIj+JN3bD76BzQomYeUMzKv20cB7UvFYbg+Y01RuV62+BAM4qZP6W1ROi3e /ZrC5ODgwkcOVo56Fg6vLWeLwcPKN6+fGkWYXUsPMXhb43icQcUHZwKup0fKcLdT vx8uKwI8pfWfTK0Rie+igUSyIrvVGa6cvA6e2uBIk4SoAlNp+OElijY+3mE+jiqO 6AWx/63jzwWKe2YvL8at -----END CERTIFICATE----- [root@centos8 ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text Certificate: Data: Version: 3 (0x2) Serial Number: 3a:03:eb:e3:5e:96:9c:3f:17:5c:fb:2d:f4:7b:03:ad:7c:42:53:eb Signature Algorithm: sha256WithRSAEncryption Issuer: C = CN, ST = beijing, L = beijing, O = magedu, OU = devops, CN = ca.magedu.org, emailAddress = admin@magedu.org Validity Not Before: May 20 04:01:51 2020 GMT Not After : May 18 04:01:51 2030 GMT Subject: C = CN, ST = beijing, L = beijing, O = magedu, OU = devops, CN = ca.magedu.org, emailAddress = admin@magedu.org Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:ba:30:88:c8:64:c4:1d:f4:64:d5:10:5f:a0:56: 9c:6a:54:ee:ca:e9:61:2f:4e:4b:2b:b7:e1:66:17: 2e:0d:a7:dd:34:e3:f3:60:b5:4a:9f:6c:0f:c2:6b: 5a:81:ae:63:65:43:ac:d8:ae:81:cf:5c:46:60:56: 23:cf:4f:76:91:b4:21:af:c0:f6:f7:43:38:02:fb: 30:61:1a:fd:74:4d:1e:e9:2b:4f:8b:f1:9e:35:3f: 47:c6:22:65:47:7f:10:e4:49:f0:fa:bb:7e:7a:4e: 22:e3:4f:11:df:45:67:c0:ed:91:0f:34:52:43:94: da:18:e1:c1:cc:e3:2b:c6:c7:4a:1e:e3:1c:4d:27: 5e:91:41:ff:8d:e9:77:5e:9f:61:9e:05:01:ff:ee: fa:04:bf:13:2f:fb:91:6e:05:3f:7d:cd:2f:e3:f5: 7e:b4:7d:7b:73:54:be:5c:5d:bc:60:4b:75:b7:a4: 69:38:e1:15:b4:52:50:66:dc:03:05:14:18:bd:a0: 29:9d:db:87:34:32:43:e2:c7:42:8b:df:47:8b:9b: 21:76:c7:b1:6e:df:48:40:75:90:9e:b3:b7:a1:5c: 4c:a5:b2:8b:e1:45:35:13:f4:29:84:27:ca:05:07: be:ba:b6:b1:a6:66:7c:d9:d7:8c:f1:f4:49:6d:ef: f7:ff Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: 12:E1:71:0E:BD:9E:7D:6C:94:02:C9:F1:2E:66:B3:5E:12:B3:FE:5F X509v3 Authority Key Identifier: keyid:12:E1:71:0E:BD:9E:7D:6C:94:02:C9:F1:2E:66:B3:5E:12:B3:FE:5F X509v3 Basic Constraints: critical CA:TRUE Signature Algorithm: sha256WithRSAEncryption 6c:9a:7a:c7:b8:60:62:33:cd:05:2f:7b:ca:b3:7f:96:a1:24: af:8c:b4:5f:b6:3c:1e:40:61:a2:8c:9e:bc:e4:ef:66:0c:5a: b3:f8:e0:5c:16:d4:2d:92:54:46:b5:54:37:48:fb:5a:74:ed: a3:ef:4b:c6:9a:6a:19:6a:25:bd:cd:7c:8e:06:54:1c:f0:2c: d3:c4:bf:41:25:cc:a4:24:75:83:8a:16:18:59:e8:b1:a2:b7: ca:8f:39:09:f4:2e:32:a6:81:bf:6c:82:23:f8:93:77:6c:3e: fa:07:34:28:99:87:94:33:32:af:db:47:01:ed:4b:c5:61:b8: 3e:63:4d:51:b9:5e:b6:f8:10:0c:e2:a6:4f:e9:6d:51:3a:2d: de:fd:9a:c2:e4:e0:e0:c2:47:0e:56:8e:7a:16:0e:af:2d:67: 8b:c1:c3:ca:37:af:9f:1a:45:98:5d:4b:0f:31:78:5b:e3:78: 9c:41:c5:07:67:02:ae:a7:47:ca:70:b7:53:bf:1f:2e:2b:02: 3c:a5:f5:9f:4c:ad:11:89:ef:a2:81:44:b2:22:bb:d5:19:ae: 9c:bc:0e:9e:da:e0:48:93:84:a8:02:53:69:f8:e1:25:8a:36: 3e:de:61:3e:8e:2a:8e:e8:05:b1:ff:ad:e3:cf:05:8a:7b:66: 2f:2f:c6:ad [root@centos8 ~]#sz /etc/pki/CA/cacert.pem #将文件cacert.pem传到windows上,修改文件名为cacert.pem.crt,双击可以看到下面显示
#生成证书申请文件 [root@centos8 ~]# openssl req -new -key /data/app1/app1.key -out /data/app1/app1.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]:magedu Organizational Unit Name (eg, section) []:it Common Name (eg, your name or your server's hostname) []:app1.magedu.org Email Address []:root@magedu.org Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: [root@centos8 ~]#ll /data/app1/ total 8 -rw-r--r-- 1 root root 1045 May 20 14:11 app1.csr -rw------- 1 root root 1679 May 20 14:06 app1.key
默认有三项内容必须和CA一致:国家,省份,组织,如果不同,会出现下面的提示
1 2 3 4 5 6
[root@centos8 ~]# openssl ca -in /data/app2/app2.csr -out /etc/pki/CA/certs/app2.crt Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok The stateOrProvinceName field is different between CA certificate (beijing) and the request (hubei)
[root@centos8 ~]#openssl ca -in /data/app1/app1.csr -out /etc/pki/CA/certs/app1.crt -days 1000 Using configuration from /etc/pki/tls/openssl.cnf Check that the request matches the signature Signature ok Certificate Details: Serial Number: 15 (0xf) Validity Not Before: May 20 06:21:01 2020 GMT Not After : Feb 14 06:21:01 2023 GMT Subject: countryName = CN stateOrProvinceName = beijing organizationName = magedu organizationalUnitName = it commonName = app1.magedu.org emailAddress = root@magedu.org X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: BC:C0:D3:08:AE:E3:2C:0C:DB:2E:DC:B9:5F:65:E2:49:6A:D7:C9:30 X509v3 Authority Key Identifier: keyid:12:E1:71:0E:BD:9E:7D:6C:94:02:C9:F1:2E:66:B3:5E:12:B3:FE:5F Certificate is to be certified until Feb 14 06:21:01 2023 GMT (1000 days) Sign the certificate? [y/n]:y 1 out of 1 certificate requests certified, commit? [y/n]y Write out database with 1 new entries Data Base Updated
[root@centos8 ~]#openssl ca -revoke /etc/pki/CA/newcerts/11.pem Using configuration from /etc/pki/tls/openssl.cnf Revoking Certificate 11. Data Base Updated
[root@centos8 ~]#openssl ca -status 11 Using configuration from /etc/pki/tls/openssl.cnf 11=Revoked (R)
[root@centos8 ~]#cat /etc/pki/CA/index.txt V 230214062101Z 0F unknown /C=CN/ST=beijing/O=magedu/OU=it/CN=app1.magedu.org/emailAddress=root@magedu.org V 210520064452Z 10 unknown /C=CN/ST=hubei/L=wuhan/O=wangedu/OU=sales/CN=app2.wangedu.org/emailAddress=admin @wangedu.org R 210520065000Z 200520065821Z 11 unknown /C=CN/ST=hubei/L=wuhan/O=wangedu/OU=sales/CN=app2.wangedu.org/emailAddress=admin @wangedu.org
[root@centos8 ~]#openssl ca -gencrl -out /etc/pki/CA/crl.pem Using configuration from /etc/pki/tls/openssl.cnf /etc/pki/CA/crlnumber: No such file or directory error while loading CRL number 140511895181120:error:02001002:system library:fopen:No such file or directory:crypto/bio/bss_file.c:72:fopen('/etc/pki/CA/crlnumber','r') 140511895181120:error:2006D080:BIO routines:BIO_new_file:no such file:crypto/bio/bss_file.c:79:
[root@centos8 ~]# echo 01 > /etc/pki/CA/crlnumber [root@centos8 ~]# openssl ca -gencrl -out /etc/pki/CA/crl.pem Using configuration from /etc/pki/tls/openssl.cnf
[root@centos8 ~]# sshpass -p 123456 ssh -o StrictHostKeyChecking=no 10.0.0.7 hostname -I Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts. 10.0.0.7
[root@centos8 ~]#ssh-keygen Generating public/private rsa key pair. Enter file inwhich to save the key (/root/.ssh/id_rsa): #回车,接受默认值 Enter passphrase (empty for no passphrase): #回车,接受默认值,空密码 Enter same passphrase again: #回车,接受默认值 Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:vpPtmqRv1llmoSvqT2Lx5C0LPGTE0pvdAqhDqlR5jLY root@centos8.wangxiaochun.com The key's randomart image is: +---[RSA 3072]----+ | | | ++ | | .=oo= | | oo.oo = . . | |..oE * S .. . | |o . + * o. + | |. * B+.* | | . B*== | | .+*B=. | +----[SHA256]-----+ [root@centos8 ~]#ll .ssh/ total 8 -rw------- 1 root root 2622 May 22 09:51 id_rsa -rw-r--r-- 1 root root 583 May 22 09:51 id_rsa.pub [root@centos8 ~]#cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDEIP+dPPpgsvL+RdPbHvv5w88jOiRTO8Jz2aMcnRDE5UCbBjjg b/qqOuNEaQDk+RFfCRtxdm4o+B1NqBmFBCXVDJIy/dNzF/XyoQC1JzyBo9/sfggpeE5w3tQpJKQAIeQK rBZ6VD/otAHB/MO9NfQP21yZsgB1qXyY+3vvM8Hrk6mJf+J4+shyLnLYfDH6m93f7fMXcgiz2h0IuG5W 85vuMUK5XQKQNnB1Ev9QSkQWtRbhzJ2LRgyBPLeifGzeO9fsiNz9k9TWVPgx6WxaW3xZe/byipEBBs49 tMRFw/5E73H90g0lzBBzw5hUmDK1uieG6wU4/b/alJzqRsXSvm7s8ompfv9Cqigvy14H4ev79Ywi2aSe YacJ25MCmAHtwYMS5/Q25aTobpQF2DM57nlRxHF+biVjYgaJzZ+6eOIUjLzobFLqBzPsMC7DggJWjzRY y2MY1NJX97xjrkTP6zNPdWTnRieTo6d+BaHzj92uVJp3FfbkTg5pNqlguXdEMYU= root@centos8.wangxiaochun.com [root@centos8 ~]#cat .ssh/id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ngSuMBG9ThE6XyvB8GGHUXU2wIwv/DlNllRYWO6CO0C7Z Q0vOUI3fcWb4oAunlrKOZDKMJhhQEHAAAAwQDVJMz09F0jYGr/PBk9pAnZSpQuZwlnsKjt gni/Gz2dxiVI1JtpfWhctoch92e40DUQEX0rvzPCZGdzHRE84IlTfjWDx4hzYw4+zLv2v3 t+TMBTOleGuMNftrZCcQbWwVZPEtSTh1G7M5abthQcVZ+q3p+LM7zSHbB93LFSEccJF9EF tpiXz/yjPjNT05h52Jq0O3NxMG7LsHp4FxNx0X/5DXepnk0VGHqAZT9XmkKGRjXEWXfj9N AS8zTUHzSQchMAAAAdcm9vdEBjZW50b3M4Lndhbmd4aWFvY2h1bi5jb20BAgMEBQY= -----END OPENSSH PRIVATE KEY----- [root@centos8 ~]#ssh-copy-id root@10.0.0.7 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established. ECDSA key fingerprint is SHA256:s//WMgPVXmOjqfOg3f3X0nmaPZF+Fj5vPdWCnAzDcpU. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes /usr/bin/ssh-copy-id: INFO: attempting to login with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@10.0.0.7's password: #输入远程用户的密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh '10.0.0.7'" and check to make sure that only the key(s) you wanted were added. [root@centos7 ~]#ll .ssh total 4 -rw------- 1 root root 583 May 22 09:52 authorized_keys [root@centos7 ~]#cat .ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDEIP+dPPpgsvL+RdPbHvv5w88jOiRTO8Jz2aMcnRDE5UCbBjjg b/qqOuNEaQDk+RFfCRtxdm4o+B1NqBmFBCXVDJIy/dNzF/XyoQC1JzyBo9/sfggpeE5w3tQpJKQAIeQK rBZ6VD/otAHB/MO9NfQP21yZsgB1qXyY+3vvM8Hrk6mJf+J4+shyLnLYfDH6m93f7fMXcgiz2h0IuG5W 85vuMUK5XQKQNnB1Ev9QSkQWtRbhzJ2LRgyBPLeifGzeO9fsiNz9k9TWVPgx6WxaW3xZe/byipEBBs49 tMRFw/5E73H90g0lzBBzw5hUmDK1uieG6wU4/b/alJzqRsXSvm7s8ompfv9Cqigvy14H4ev79Ywi2aSe YacJ25MCmAHtwYMS5/Q25aTobpQF2DM57nlRxHF+biVjYgaJzZ+6eOIUjLzobFLqBzPsMC7DggJWjzRY y2MY1NJX97xjrkTP6zNPdWTnRieTo6d+BaHzj92uVJp3FfbkTg5pNqlguXdEMYU= root@centos8.wangxiaochun.com [root@centos8 ~]#ssh 10.0.0.7 Last login: Fri May 22 08:43:50 2020 from 10.0.0.1 [root@centos7 ~]#exit logout Connection to 10.0.0.7 closed. [root@centos8 ~]#scp /etc/fstab 10.0.0.7:/data fstab #对私钥加密 [root@centos8 ~]#ssh-keygen -p Enter file in which the key is (/root/.ssh/id_rsa): Key has comment 'root@centos8.wangxiaochun.com' Enter new passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved with the new passphrase. [root@centos8 ~]#ssh 10.0.0.7 Enter passphrase for key '/root/.ssh/id_rsa': #输入私钥的密码 Last login: Fri May 22 08:47:50 2020 from 10.0.0.8 [root@centos7 ~]#exit logout Connection to 10.0.0.7 closed. #启用ssh代理 [root@centos8 ~]#ssh-agent bash [root@centos8 ~]#ps aux|grep agent root 1972 0.0 0.0 29440 548 ? Ss 10:18 0:00 ssh-agent bash root 1992 0.0 0.1 12108 964 pts/0 S+ 10:18 0:00 grep -- color=auto agent [root@centos8 ~]#ssh-add Enter passphrase for /root/.ssh/id_rsa: Identity added: /root/.ssh/id_rsa (root@centos8.wangxiaochun.com) [root@centos8 ~]#ssh 10.0.0.7 Last login: Fri May 22 08:48:55 2020 from 10.0.0.8
Port 22 #生产建议修改 ListenAddress ip LoginGraceTime 2m PermitRootLogin yes#默认ubuntu不允许root远程ssh登录 StrictModes yes#检查.ssh/文件的所有者,权限等 MaxAuthTries 6 #pecifies the maximum number of authentication attempts permitted per connection. Once the number of failures reaches half this value, additional failures are logged. The default is 6. MaxSessions 10 #同一个连接最大会话 PubkeyAuthentication yes#基于key验证 PermitEmptyPasswords no #空密码连接 PasswordAuthentication yes#基于用户名和密码连接 GatewayPorts no ClientAliveInterval 10 #单位:秒 ClientAliveCountMax 3 #默认3 UseDNS yes#提高速度可改为no GSSAPIAuthentication yes#提高速度可改为no MaxStartups #未认证连接最大值,默认值10 Banner /path/file
root@ubuntu1804:~# grep %sudo /etc/sudoers %sudo ALL=(ALL:ALL) ALL
root@ubuntu1804:~# id wang uid=1000(wang) gid=1000(wang) groups=1000(wang),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lxd),113(lpadmin),114(sambashare)
# /etc/security/limits.conf # #This file sets the resource limits for the users logged in via PAM. #It does not affect resource limits of the system services. 在Centos7以上版本中,使用Systemd替代了之前的SysV。/etc/security/limits.conf文件的配置作 用域缩小了。/etc/security/limits.conf的配置,只适用于通过PAM认证登录用户的资源限制,它对 systemd的service的资源限制不生效。因此登录用户的限制,通过/etc/security/limits.conf 与/etc/security/limits.d下的文件设置即可。
vim /etc/systemd/system.conf DefaultLimitNOFILE=100000 DefaultLimitNPROC=65535 或者针对指定的service添加下面行 [Service] LimitNOFILE=100000 LimitNPROC=65535
案例:系统的各种资源的默认值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[root@centos8 ~]#ulimit -a core file size (blocks, -c) unlimited data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 3059 max locked memory (kbytes, -l) 16384 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 3059 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
案例: 查看指定进程的资源限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#cat /proc/PID/limits [root@wang-liyun-pc ~]# cat /proc/`pidof nginx | xargs -n1 | sort -n|head -1`/limits Limit Soft Limit Hard Limit Units Max cpu time unlimited unlimited seconds Max file size unlimited unlimited bytes Max data size unlimited unlimited bytes Max stack size 8388608 unlimited bytes Max core file size 0 unlimited bytes Max resident set unlimited unlimited bytes Max processes 7270 7270 processes Max open files 65535 65535 files Max locked memory 65536 65536 bytes Max address space unlimited unlimited bytes Max file locks unlimited unlimited locks Max pending signals 7270 7270 signals Max msgqueue size 819200 819200 bytes Max nice priority 0 0 Max realtime priority 0 0 Max realtime timeout unlimited unlimited us
[root@centos8 ~]#dnf info google-authenticator BaseOS 3.3 MB/s | 3.9 kB 00:00 AppStream 2.9 MB/s | 4.3 kB 00:00 EPEL 34 kB/s | 4.7 kB 00:00 extras 7.3 kB/s | 1.5 kB 00:00 Available Packages Name : google-authenticator Version : 1.07 Release : 1.el8 Architecture : x86_64 Size : 57 k Source : google-authenticator-1.07-1.el8.src.rpm Repository : epel Summary : One-time pass-code support using open standards URL : https://github.com/google/google-authenticator-libpam/ License : ASL 2.0 Description : The Google Authenticator package contains a plug-able authentication : module (PAM) which allows login using one-time pass-codes conforming to : the open standards developed by the Initiative for Open Authentication : (OATH) (which is unrelated to OAuth). : : Pass-code generators are available (separately) for several mobile : platforms. : : These implementations support the HMAC-Based One-time Password (HOTP) : algorithm specified in RFC 4226 and the Time-based One-time Password : (TOTP) algorithm currently in draft. [root@centos8 ~]#bash google-authenticator.sh Installed: google-authenticator-1.07-1.el8.x86_64 Complete! Do you want me to update your /root/.google_authenticator file? (y/n) y 你希望我更新你的“/root/.google_authenticator”文件吗(y/n)? Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) y 你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现或甚 至防止中间人攻击的可能性(y/n)? By default, a new token is generated every 30 seconds by the mobile app. In order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. This allows for a time skew of up to 30 seconds between authentication server and client. If you experience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the current code, the next code) to 17 permitted codes (the 8 previous codes, the current code, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server. Do you want to do so? (y/n) y 默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞, 我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3 个可通过验证码增加到17个可通过验证码, 这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)? If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting? (y/n) y 如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块 启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。 你希望启用尝试次数 限制吗(y/n)? 在App Store 搜索Google Authenticator 进行App安装 Do you want authentication tokens to be time-based (y/n) y Warning: pasting the following URL into your browser exposes the OTP secret to Google: https://www.google.com/chart? chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@centos8.localdomain%3Fsecret%3D7YTAL4GW3TND7BICUMJGJLIFVE%26issuer%3Dcentos8.localdomain #浏览器打开此地址 Failed to use libqrencode to show QR code visually for scanning. Consider typing the OTP secret into your app manually. Your new secret key is: 7YTAL4GW3TND7BICUMJGJLIFVE Enter code from app (-1 to skip):
Failed to use libqrencode to show QR code visually for scanning. Consider typing the OTP secret into your app manually. Your new secret key is: 7YTAL4GW3TND7BICUMJGJLIFVE Enter code from app (-1 to skip): 224421 #手机APP上的数字 Code confirmed Your emergency scratch codes are: 68820657 77385307 50928320 41000243 54628309 Do you want me to update your "/root/.google_authenticator" file? (y/n) y Do you want to disallow multiple uses of the same authentication token? This restricts you to one login about every 30s, but it increases your chances to notice or even prevent man-in-the-middle attacks (y/n) y By default, a new token is generated every 30 seconds by the mobile app. In order to compensate for possible time-skew between the client and the server, we allow an extra token before and after the current time. This allows for a time skew of up to 30 seconds between authentication server and client. If you experience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the current code, the next code) to 17 permitted codes (the 8 previous codes, the current code, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server. Do you want to do so? (y/n) y If the computer that you are logging into isn't hardened against brute-force login attempts, you can enable rate-limiting for the authentication module. By default, this limits attackers to no more than 3 login attempts every 30s. Do you want to enable rate-limiting? (y/n) y Redirecting to /bin/systemctl restart sshd.service
ssh 当前主机,可看到提示,输入手机APP上显示的数字码和root密码,可以登录,否则失败
1 2 3 4 5 6 7
[root@centos7 ~]#ssh 10.0.0.8 Verification code: Password: Last failed login: Fri Feb 7 12:11:12 CST 2020 from 10.0.0.7 on ssh:notty There were 6 failed login attempts since the last successful login. Last login: Fri Feb 7 12:09:47 2020 from 10.0.0.7 [root@centos8 ~]#
echo -e "\033[31mDo you want me to update your "/root/.google_authenticator" file? (y/n) y" echo -e "\033[31m你希望我更新你的“/root/.google_authenticator”文件吗(y/n)?\033[0m" echo -e "\033[31mDo you want to disallow multiple uses of the same authentication" echo -e "\033[31mtoken? This restricts you to one login about every 30s, but it increases" echo -e "\033[31myour chances to notice or even prevent man-in-the-middle attacks (y/n) y" echo -e "\033[31m你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?\033[0m" echo -e "\033[31mBy default, a new token is generated every 30 seconds by the mobile app." echo -e "\033[31mIn order to compensate for possible time-skew between the client and the server," echo -e "\033[31mwe allow an extra token before and after the current time. This allows for a" echo -e "\033[31mtime skew of up to 30 seconds between authentication server and client. If you" echo -e "\033[31mexperience problems with poor time synchronization, you can increase the window" echo -e "\033[31mfrom its default size of 3 permitted codes (one previous code, the current" echo -e "\033[31mcode, the next code) to 17 permitted codes (the 8 previous codes, the current" echo -e "\033[31mcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes" echo -e "\033[31mbetween client and server." echo -e "\033[31mDo you want to do so? (y/n) y" echo -e "\033[31m默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,\033[0m" echo -e "\033[31m我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3个可通过验证码增加到17个可通过验证码,\033[0m" echo -e "\033[31m这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)?\033[0m" echo -e "\033[31mIf the computer that you are logging into isn't hardened against brute-force" echo -e "\033[31mlogin attempts, you can enable rate-limiting for the authentication module." echo -e "\033[31mBy default, this limits attackers to no more than 3 login attempts every 30s." echo -e "\033[31mDo you want to enable rate-limiting? (y/n) y" echo -e "\033[31m如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块\033[0m" echo -e "\033[31m启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。 你希望启用尝试次数限制吗(y/n)?\033[0m" echo -e "\033[32m 在App Store 搜索Google Authenticator 进行App安装 \033[0m"
google-authenticator
#/etc/pam.d/sshd文件,修改或添加下行保存 #auth required pam_google_authenticator.so sed -i '1a\auth required pam_google_authenticator.so' /etc/pam.d/sshd
#编辑/etc/ssh/sshd_config找到下行 #ChallengeResponseAuthentication no #更改为 #ChallengeResponseAuthentication yes sed -i 's/.*ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/' /etc/ssh/sshd_config
[root@ubuntu2004 ~]#timedatectl Local time: Tue 2022-01-18 15:36:09 CST Universal time: Tue 2022-01-18 07:36:09 UTC RTC time: Tue 2022-01-18 07:36:09 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC inlocal TZ: no [root@ubuntu2004 ~]#systemctl status time-set.target ● time-set.target - System Time Set Loaded: loaded (/lib/systemd/system/time-set.target; static; vendor preset: disabled) Active: active since Fri 2022-01-07 06:23:27 UTC; 1 weeks 5 days ago Docs: man:systemd.special(7) Jan 07 06:23:27 ubuntu2004.magedu.org systemd[1]: Reached target System Time Set.
[root@ubuntu2004 ~]#systemctl status time-sync.target ● time-sync.target - System Time Synchronized Loaded: loaded (/lib/systemd/system/time-sync.target; static; vendor preset: disabled) Active: active since Fri 2022-01-07 06:23:27 UTC; 1 weeks 5 days ago Docs: man:systemd.special(7) Jan 07 06:23:27 ubuntu2004.magedu.org systemd[1]: Reached target System Time Synchronized.
[root@ubuntu2004 ~]#man 7 systemd.special
时间同步服务
时间同步服务
多主机协作工作时,各个主机的时间同步很重要,时间不一致会造成很多重要应用的故障,如:加密协议,日志,集群等, 利用NTP(Network Time Protocol) 协议使网络中的各个计算机时间达到同步。目前NTP协议属于运维基础架构中必备的基本服务之一
[root@centos7 ~]#chronyc chrony version 3.2 Copyright (C) 1997-2003, 2007, 2009-2017 Richard P. Curnow and others chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public License version 2 for details. chronyc> clients Hostname NTP Drop Int IntL Last Cmd Drop Int Last =============================================================================== 192.168.8.7 18 0 6 - 17 0 0 - - 192.168.8.6 14 0 6 - 56 0 0 - - chronyc> activity 200 OK 1 sources online 0 sources offline 0 sources doing burst (return to online) 0 sources doing burst (return to offline) 0 sources with unknown address chronyc> sources -v 210 Number of sources = 1 .-- Source mode '^' = server, '=' = peer, '#' = local clock. / .- Source state '*' = current synced, '+' = combined , '-' = not combined, | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable. || .- xxxx [ yyyy ] +/- zzzz || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^? 192.168.8.100 3 8 1 338 -40ms[ -40ms] +/- 237ms
范例: CentOS6 ntp客户端同步检查
1 2 3 4
[root@centos6 ~]#ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *10.0.0.8 202.112.29.82 3 u 16 64 1 0.424 1.956 1.771
[root@centos8 ~]#yum -y install chrony [root@ubuntu2004 ~]#apt install chrony -y [root@centos8 ~]#vim /etc/chrony.conf server ntp.aliyun.com iburst server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst #allow 192.168.0.0/16 allow 0.0.0.0/0 #加此行,指定允许同步的网段 # Serve time even if not synchronized to a time source. local stratum 10 #删除此行注释,当互联网无法连接,仍然可以为客户端提供时间同步服务