ssh -v -i ~/.ssh/your-key.pem ec2-user@<ec2-public-ip>Linux Commands
Log in
Start by logging into the AWS EC2 instance. You can refer back to the AWS Server Login and Managing SSH key pairs chapters if you need a refresher.
SSH commands
The commands below connect to the EC2 instance as ec2-user with verbose output:
The -i flag suggests an identity file, but SSH will still try keys from your ssh-agent first. If the agent has a key the server accepts, that key wins and the pem is never used.
Or you can force SSH to authenticate using only the specified pem file:1
ssh -v -i ~/.ssh/your-key.pem -o IdentitiesOnly=yes -o IdentityAgent=none ec2-user@<ec2-public-ip>IdentitiesOnly=yesrestricts SSH to the key given by-i, ignoring other keys it might otherwise try.IdentityAgent=nonedisables thessh-agentfor this session, preventing agent-held keys from being offered.
After logging in, you’ll see something like this:
Last login: Fri Jul 17 17:42:40 2026 from ip<your-ip-address.net>
, #_
~\_ ####_ Amazon Linux 2
~~ \_#####\
~~ \###| AL2 End of Life is 2026-06-30.
~~ \#/ ___
~~ V~' '->
~~~ / A newer version of Amazon Linux is available!
~~._. _/
_/ _/ Amazon Linux 2023, GA and supported until 2029-06-30.
_/m/' https://aws.amazon.com/linux/amazon-linux-2023/
21 package(s) needed for security, out of 24 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-your-ip-address]$Be sure to run sudo yum update to keep all the packages updated.
Getting your bearings on a new instance
I’ll cover the handful of commands I use on every new instance, starting with whoami, which reports the account the shell is running as:
whoamiec2-userWhere am I, what’s here
Once again, we want to get our bearings:
pwd/home/ec2-userList everything, including hidden files, with permissions:
ls -latotal 16
drwx------ 4 ec2-user ec2-user 110 Jul 17 19:09 .
drwxr-xr-x 3 root root 22 Jul 8 13:28 ..
-rw------- 1 ec2-user ec2-user 566 Aug 2 07:45 .bash_history
-rw-r--r-- 1 ec2-user ec2-user 18 Jul 15 2020 .bash_logout
-rw-r--r-- 1 ec2-user ec2-user 193 Jul 15 2020 .bash_profile
-rw-r--r-- 1 ec2-user ec2-user 231 Jul 15 2020 .bashrc
drwx------ 3 ec2-user ec2-user 18 Jul 17 19:09 .config
drwx------ 2 ec2-user ec2-user 29 Jul 8 13:28 .sshec2-user owns everything here, and .ssh is already locked down to 700 before I’ve touched it. That’s Amazon Linux 2’s default, not something I set.
Who am I, what can I do
id shows the groups behind the sudo access I’ve been using without thinking about it:
iduid=1000(ec2-user) gid=1000(ec2-user) groups=1000(ec2-user),4(adm),10(wheel),190(systemd-journal)wheel is the group that grants sudo on Amazon Linux 2. Ubuntu calls the equivalent group sudo (same idea, different name). If you’re ever unsure which convention an AMI uses, id will tell you. I’ll use wheel again in the next chapter when I create a second user.
Checking disk space
Worth doing right after logging into any new instance, before you forget:
df -hFilesystem Size Used Avail Use% Mounted on
devtmpfs 462M 0 462M 0% /dev
tmpfs 471M 0 471M 0% /dev/shm
tmpfs 471M 428K 470M 1% /run
tmpfs 471M 0 471M 0% /sys/fs/cgroup
/dev/nvme0n1p1 8.0G 2.6G 5.5G 32% /
tmpfs 95M 0 95M 0% /run/user/1000The default EC2 root volume is small. Installing R packages or pulling a Docker image is usually what finally runs it out.
Piping output
A pipe (|) feeds the output of one command into the next. Counting files in /etc without opening it:
ls /etc | wc -l191Reading the manual
Every command’s flags are documented with man. Before creating a user in the next chapter, it’s worth checking what useradd does on this particular AMI, since its defaults aren’t the same on every distro:
man useraddArrow keys scroll, q quits back to the shell.
Recap
whoami,pwd, andls -laare worth running right after logging in: they confirm which account the shell is running as, where it starts, and what’s in the home directory (including hidden files and their permissions).idlists the groups behind an account’s privileges. On Amazon Linux 2,wheelis the group that grantssudo; Ubuntu calls the equivalent groupsudo.df -hreports disk usage. The default EC2 root volume is small, so check it on any new instance before installing R packages or pulling Docker images.A pipe (
|) feeds one command’s output into the next, andmandocuments every command’s flags (worth reading on each AMI, since defaults likeuseradd’s vary by distro).
In Linux Users, we’ll put useradd and the wheel group to work by creating a non-root user, giving it an SSH key, and setting up a service account.
Use this when you need to verify a specific key works, or when the agent would otherwise shadow the intended key.↩︎