Before connecting to the server, install P4PORT (to tell the client where the server is) and P4USER (to tell the server who you are). P4PORT is indicated as the host name: port, port is usually (but not always) 1666. If you do not know what server address and Perforce username is, contact your sys administrator.
p4 set P4PORT=your.server.hostname:1666 p4 set P4USER=your.username
You can also use "set" or "setenv" or "export" according to your shell, but with 2014.2 or newer Perforce client (use "p4 -V" to check your version information), you can use "p4 to establish "as an ongoing cross-platform alternative. Another option is to use the "-u" and "-p" flags for each command you execute (for example, "p4 -p your.server.hostname: 1666 -u your.username sync ..."), but it gets tedious quickly .
To verify the correctness of your connection, run:
p4 info
If this gives you an error message or says that your user is unknown, contact your Perforce administrator to ensure that you have the correct P4PORT and P4USER values.
To enter, run:
p4 login
If the client can connect to the server and your username is correct, you will be prompted for a password. Enter it and you will be logged in.
If you are running scripts of Perforce commands that require credentials and you do not want your script to stop in the middle to request a password, my recommendation was to run "p4 login" as mentioned above at some point before running the script . The entry ticket will be saved on your computer, and the script will pick it up (provided that it connects to the same P4PORT and P4USER that you used to create the ticket). This is the safest method, because your plaintext password is not stored anywhere on your computer, and the login ticket cannot be used from other computers by default (also probably a limited time, depending on how your administrator configured that).
An unsafe method is to enter the plaintext password in your script:
echo my.formerly.secure.password|p4 login
Posting the plaintext of the password to the "p4 login" command will cause it to behave as if you entered it at the invitation and continue execution, rather than wait for additional input.