This article provides a comprehensive guide to the most commonly used SSH commands for remote server management. From basic connection commands to advanced file transfers and remote executions, this guide is perfect for beginners and experienced users looking to streamline their workflow with SSH.
35 min
Edited:15-09-2024
In this guide, we'll explore essential SSH commands, providing clear explanations and practical examples. Whether you're managing remote servers or securely transferring files, these commands are fundamental for efficient server administration. Perfect for beginners and those looking to enhance their skills, this guide covers everything you need to streamline your workflow using SSH.
The ls command in Linux is used to list the contents of a directory. By default, it displays the names of files and directories in the current working directory. You can use options like -l for detailed information (permissions, size, modification date) or -a to show hidden files.
1. ls folder
Or use it alone to list the content of the current directory:
1. ls
The cd command in Linux is used to change the current working directory. You can navigate to a specific directory by providing its path (e.g., cd /path/to/directory), or use cd .. to move up one level in the directory hierarchy. Typing cd alone takes you to your home directory.
1. cd folder
The cp command in Linux is used to copy files or directories. To copy a file, use cp source destination (e.g., cp file.txt /path/to/destination). You can add options like -r to copy directories recursively and -i to prompt for confirmation before overwriting files.
1. cp /path/file.txt /newpath/file.txt
mv: The mv command in Linux is used to move or rename files and directories. To move a file, use mv source destination (e.g., mv file.txt /new/path/). You can also rename a file by specifying a new name in the destination.
1. mv /path/file.txt /newpath/file.txt
rm: The rm command is used to remove (delete) files or directories. To remove a file, use rm filename. Use rm -r directory to delete a directory and its contents recursively. Be careful, as rm permanently deletes files.
1. rm /path/file.txt
The rm command will delete the files permanently, so be careful when using it
mkdir: The mkdir command creates new directories. To create a directory, use mkdir directory_name. You can use -p to create parent directories if they don’t already exist (e.g., mkdir -p /path/to/directory).
1. mkdir /path/new_directory
chown: The chown command changes the ownership of files or directories. To change the owner, use chown user:group filename. For example, chown john:admin file.txt makes "john" the owner and assigns the "admin" group.
1. chown user:group /path/file.txt
sftp: The sftp command provides secure file transfer over SSH. You can connect to a remote server using sftp user@hostname. Once connected, you can upload, download, or manage files just like in a local file system.
1. sftp user@remote
scp: The scp command securely copies files between hosts using SSH. To copy a file to a remote server, use scp file.txt user@host:/path/to/destination. It supports file transfers between local and remote systems
1. scp file.txt user@remote:/path/to/destination
Please note that those commands are for educational purpose only, and helps you find and fix issues on your server, the wrong use of such tools is your responsibility
ping: The ping command tests network connectivity between your computer and a target host. For example, ping example.com sends ICMP echo requests to the host and displays the response time, helping to diagnose network issues.
1. ping 127.0.0.1
traceroute: The traceroute command tracks the path packets take to reach a network host. For example, traceroute example.com shows each hop (intermediate router) along the route and their response times.
1. traceroute server-ip
netstat: The netstat command provides network statistics and information about network connections, routing tables, and interface statistics. Use netstat -a to list all connections and listening ports, or netstat -r to view the routing table.
1. netstat -tuln
ifconfig: The ifconfig command displays or configures network interface parameters. To view the status of all interfaces, use ifconfig. To configure an interface, you can specify options like ifconfig eth0 up to activate it.
1. ifconfig
nslookup: The nslookup command queries DNS to obtain domain name or IP address information. For example, nslookup example.com retrieves the IP address associated with the domain. It can also be used to diagnose DNS issues.
1. nslookup your-domain-name
When your website is not responding and giving you connection issues, checking the network status is always a good start to find the issue
dig: The dig command performs DNS queries and provides detailed information about DNS records. For example, dig example.com retrieves DNS records for the domain, including A records, MX records, and more.
1. dig your-domain-name
telnet: The telnet command establishes a text-based connection to a remote server, typically for troubleshooting or accessing remote services. For example, telnet example.com 80 connects to port 80 on the specified host.
1. telnet your_domain 80
mtr: The mtr command combines the functionality of traceroute and ping to diagnose network connectivity issues. It continuously displays the route and packet loss statistics for each hop to a target host (e.g., mtr example.com).
1. mtr your-domain-name
ss: The ss command provides detailed information about network sockets. It can display open connections, listening ports, and socket statistics. For example, ss -tuln shows TCP and UDP listening sockets with numeric addresses.
1. ss -tuln
iptables: The iptables command configures the Linux kernel's packet filtering rules. You can use it to set up firewall rules, manage network traffic, and define security policies
1. sudo iptables -L
You can use the man command to get more details and options that can be used with the domain, just type man and the command that you want to know more about.
adduser: The adduser command adds a new user to the system with a user-friendly interface. It sets up the user's home directory, creates a user account, and prompts for additional details (e.g., adduser username).
1. sudo adduser newuser
useradd: The useradd command creates a new user account with more options for customization. For example, useradd -m -s /bin/bash username creates a user with a home directory and a specified shell. It requires additional configuration for some user details.
1. sudo useradd -m -s /bin/bash newuser
usermod: The usermod command modifies existing user accounts. You can change user details like the home directory, shell, or group memberships (e.g., usermod -aG groupname username to add a user to a group).
1. sudo usermod -aG sudo newuser
deluser: The deluser command removes a user from the system. By default, it deletes the user but retains their home directory. For example, deluser username removes the user account.
1. sudo deluser newuser
userdel: The userdel command deletes a user account and, optionally, their home directory and files. For example, userdel -r username removes the user and their home directory.
1. sudo userdel -r newuser
passwd: The passwd command changes a user’s password. To set or update a password for a user, use passwd username. If used without a username, it changes the password for the current user.
1. sudo passwd newuser
chsh: The chsh command changes a user's default login shell. For example, chsh -s /bin/zsh sets the default shell to Zsh. Users can only change their own shell unless run by a superuser.
1. sudo chsh -s /bin/zsh newuser
chage: The chage command changes user password expiration information. For example, chage -M 30 username sets the maximum password age to 30 days. It helps manage password aging and expiration policies.
1. sudo chage -M 30 newuser
id: The id command displays the user ID (UID) and group ID (GID) for the current or specified user. For example, id username shows the UID, GID, and group memberships of the user.
1. id newuser
groups: The groups command lists the groups to which a user belongs. For example, groups username shows the group memberships for the specified user. If used without a username, it shows the groups for the current user.
1. groups newuser
we've explored essential SSH commands for managing files, troubleshooting networks, and handling user accounts on remote systems. Mastering these commands not only streamlines administrative tasks but also enhances your ability to maintain and troubleshoot servers efficiently. Whether you're transferring files securely, diagnosing network issues, or managing user permissions, these commands form the backbone of effective remote system management. By integrating these practices into your workflow, you'll improve your proficiency and ensure a smoother, more controlled server environment.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min