How to SSH to a specific directory in a Linux server and execute commands?
Have you ever encountered such a scene? Use the SSH command to enter the server, then use the cd command to enter the corresponding directory, and then continue your work.
This kind of operation is very common for novices, and it was the same before. In this article, experienced drivers will lead you to perform more efficient operations and achieve the results you want in just one step.
Moreover, not only can you quickly enter a specific directory on the Linux server, but you can also execute a corresponding command when connecting to the server.
Inefficient operation method
If you don’t know the method introduced in this article, you are likely to do it in two steps:
Step one: Use SSH command to enter the remote server
ssh user@remote-system
Step 2: Use the cd command to enter the directory you want
cd
Quickly enter the specified directory on the server with one command
The method mentioned above is certainly possible, but it is too inefficient. You need to use two commands to do this, but in fact, you can use one command to achieve the effect you want, such as:
ssh -t pi@192.168.0.116 'cd /home/pi/tests ; bash'
With this command, we can directly enter the corresponding directory (i.e. /home/pi/tests) in the Raspberry Pi (remote server). You can then continue your work.
Here, the -t
option is to force pseudo-terminal allocation even if standard input is not a terminal. If not added, the following prompt may appear:
Pseudo-terminal will not be allocated because stdin
Here we use another animation to visually demonstrate this process:
In addition, you can also use the following command:
ssh -t pi@192.168.0.116 'cd /home/pi/tests ; exec bash'
or:
ssh -t pi@192.168.0.116 'cd /home/pi/tests && exec bash -l'
Here, the -l
option sets this bash as the login shell.
In the above three commands, the last parameters are all bash, because the default shell interpreter of my remote server is bash. If you don't know the shell interpreter used by your remote server, you can use the following command:
ssh -t pi@192.168.0.116 'cd /home/pi/tests && exec $SHELL'
One command to remotely execute server commands
As mentioned at the beginning of this article, we can not only use a command to enter the specified directory of the remote server, but also use a command to remotely execute server commands. We can even use a command to enter the specified directory of the remote server and execute another command.
In fact, the methods used are the same. For example, if we want to enter the /home/pi/tests
directory of Raspberry Pi, and then execute the ls -al
command, we You can enter the command like this:
ssh -t pi@192.168.0.116 'cd /home/pi/tests && ls -al && exec $SHELL'
The execution results are as follows:
[Alvin.Alvin-computer] ➤ ssh -t pi@192.168.0.116 'cd /home/pi/tests && ls -al && exec $SHELL' total 48 drwxr-xr-x 4 pi pi 4096 Apr 5 14:36 . drwxr-xr-x 21 pi pi 4096 Apr 21 19:26 .. drwxrwxrwx 7 pi pi 4096 Apr 5 17:28 GIC drwxrwxrwx 3 pi pi 4096 Apr 5 17:37 gitchat -rw-r--r-- 1 pi pi 474 Apr 5 11:21 liangxu.json -rwxr-xr-x 1 pi pi 8184 Mar 17 15:34 test -rwxr-xr-x 1 pi pi 8184 Mar 17 15:34 test2 -rwxr-xr-x 1 pi pi 8184 Mar 17 15:34 test3 -rw-r--r-- 1 pi pi 131 Mar 17 15:34 test.c
A compromise solution
If you think this command is too long to type, you must first enter the server and then cd to the corresponding directory. Then, we can modify the .bashrc
file of the remote server.
vim ~/.bashrc
Write the command you want to execute in it. For example, in this scenario, we can add:
cd /home/pi/tests >& /dev/null
Then we execute :wq
to save the file, and then execute the following command to make the changes take effect:
source ~/.bashrc 或者 . ~/.bashrc
In this way, as soon as we enter the server, we will automatically enter the /home/pi/tests
directory. As shown in the following animation:

However, this has an obvious drawback, that is, we can only enter the directory we specify. If we want to change to another directory, we can only change the .bashrc file. 1
The above is the detailed content of How to SSH to a specific directory in a Linux server and execute commands?. For more information, please follow other related articles on the PHP Chinese website!

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.