openssl certificate generation commands [duplicate]

I am making a self signed certificate using OpenSSL. I want to make the certificate in one go, means that it will not ask me for the input for Company Name, Common Name etc etc. Is there anyway to do this like a switch e.g. /noprompt or any other through which I can input my all fields in one go. Is it possible that the following command takes all arguments in this call which it takes after pressing enter

openssl x509 -req -days 30 -in request.pem -signkey key.pem -out certificate.pem

Solution 1:

Edit: This question was closed as a duplicate of OpenSSL without prompt. See my accepted answer there as well. This answer has now been updated with an ECDSA variant as well. If you can use ECDSA you should.

You need to specify the subject as part of your command.

This command is one step, non-interactive, self-signed certificate creation.

RSA version

openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
    -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" \
    -keyout www.example.com.key \
    -out www.example.com.cert

ECDSA version

openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
    -days 365 -nodes -x509 \
    -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" \
    -keyout www.example.com.key \
    -out www.example.com.cert