0

I am trying to setup vagrant box using shell script where in I am storing key and cert files. Normal format of any cert or key file is as below

-----BEGIN CERTIFICATE-----
MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV
....
0+ZQ+Yxjzs69VHdPJbzu3cxjNQ==
-----END CERTIFICATE-----

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC6qoE3hNniDqD7
....
wr5wa+bGBgNzIBLS5j9F9Kul
-----END PRIVATE KEY-----

However when I check on vagrant box it looks like below.

-----BEGIN CERTIFICATE----- MIIDzzCCAregAwIB ..... 69VHdPJbzu3cxjNQ== -----END CERTIFICATE-----

-----BEGIN PRIVATE KEY----- MIIEvgIBADANBgkqhki  .... OB7jnwaqq wr5wa+bGBgNzIBLS5j9F9Kul -----END PRIVATE KEY-----

(basically, everything in one row)

Due to which Apache2 has errors in the log. Now I want to convert these keys to their proper format. I did try below

cat server.crt | tr " " "\n" 
cat server.crt | tr "\ /" " " 

but it didn't help.

    -----BEGIN
CERTIFICATE-----
MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV

0+ZQ+Yxjzs69VHdPJbzu3cxjNQ==
-----END
CERTIFICATE-----

This is what I am getting as a result of sed -i 's/ /\n/g' *.crt command. after END it goes to next line due to space character. Objective here is keep the first and last line as it is and format the content between them.

3
  • 1
    I really hope that you didn't copy the real private key over here. If you did, revoke the old certificate and renew certificate immediately. Sep 7, 2017 at 15:51
  • No worries. I am using this for testing purpose. Sep 7, 2017 at 15:53
  • 1
    Then please make extra sure to not reuse this pair anywhere.
    – Sven
    Sep 7, 2017 at 15:55

2 Answers 2

0

You want sed -i 's/ /\n/g' *.crt (assuming you have GNU sed)

But have you addressed the root cause? How did the files get corrupted?

4
  • I am sending the whole file as variable in shell script which is causing this change in file. Sep 7, 2017 at 15:52
  • -----BEGIN CERTIFICATE----- MIIDzzCCAregAwIBAgIJAP384f0KeRndMA0GCSqGSIb3DQEBCwUAMH4xCzAJBgNV ----------------------------------------- 0+ZQ+Yxjzs69VHdPJbzu3cxjNQ== -----END CERTIFICATE----- This is what I am getting as a result of this command. after END it goes to next line due to space character. Sep 7, 2017 at 16:38
  • Do you quote your "$variable" ? Sep 7, 2017 at 16:46
  • You should read this: unix.stackexchange.com/q/171346/4667 Sep 7, 2017 at 16:55
0

You should use tr '\ ' '\n'

Example:

[root@localhost ~]# uname -a
Linux localhost 2.6.32-504.16.2.el6.x86_64 #1 SMP Tue Mar 10 17:01:00 EDT 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -a | tr '\ ' '\n'
Linux
localhost
2.6.32-504.16.2.el6.x86_64
#1
SMP
Tue
Mar
10
17:01:00
EDT
2015
x86_64
x86_64
x86_64
GNU/Linux

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .