Introduction
When you log into your VPS via SSH, you might want to start in a specific directory rather than your home directory. This is especially useful for developers and system administrators who frequently work in a particular project folder.
In this tutorial, we'll guide you through how to set a default directory when you SSH into your VPS.
Prerequisites
- Access to your VPS via SSH
- Basic knowledge of the command line
- A text editor installed on your VPS (
nano,vim, etc.)
Step-by-Step Guide
Step 1: SSH into Your VPS
ssh your-username@your-vps-ipReplace your-username with your SSH username and your-vps-ip with your VPS IP address.
Step 2: Determine Your Shell
Check which shell you're using:
echo $SHELL- If the output includes
/bash, you're using bash. - If it includes
/zsh, you're using zsh.
Step 3: Edit the Shell Configuration File
Depending on your shell, you'll edit a different file:
| Shell | Configuration File |
|---|---|
| bash | ~/.bashrc or ~/.bash_profile |
| zsh | ~/.zshrc |
> Example below assumes you're using bash (.bashrc).
Open the .bashrc file:
nano ~/.bashrc_or_
vim ~/.bashrcAdd the cd Command at the End of the File:
cd /home/examplewebsite/htdocs/examplewebsite.comThis command will change the directory automatically when you log in via SSH.
Save and Exit:
- If using
nano:
Press CTRL + O → Enter → CTRL + X.
- If using
vim:
Press ESC, then type :wq → Enter.
Step 4: Apply the Changes
Apply the changes immediately without logging out:
source ~/.bashrc_or just log out and back in._
Step 5: Verify the Configuration
Logout and reconnect via SSH:
exit
ssh your-username@your-vps-ip You should now start in the /home/examplewebsite/htdocs/examplewebsite.com directory.
Troubleshooting
- Permission Issues:
Make sure your SSH user has permission to access the specified directory.
- Directory Existence:
Double-check that the directory exists.
- Shell Type:
Ensure you're editing the correct configuration file for your shell (.bashrc, .zshrc, etc.).
Conclusion
By following these steps, you can set a default directory when SSHing into your VPS, streamlining your workflow and saving time navigating to your project folders. This simple tweak can greatly enhance your efficiency when working on your server.