Here is a set of Linux commands used when operating as remote server admin: i.e. find and modify multiple files, sync local and remote files, generate keys, etc.
SSH and keygen
The first operation for working on a remote server is connecting via ssh. Typically, you connect to the server with a non-root user with sudo privileges. (if not, please read initial server setup).
ssh myuser@123.45.67.890
ssh keygen and copy-id
For connecting the local workstation to the server, a common practice is using public-key authentication. For this purpose, you first need to generate a key pair on the local workstation, as follows.
# from local workstation
ssh-keygen -t rsa -b 4096 -C "new key for VPS"
After the key pair is generated, you copy the public key from the local workstation to the remote host.
#copy PK from local to remote as remote-root
ssh-copy-id -i ~/.ssh/id_rsa.pub root@123.45.67.890
Find & C.
The find command operates on a file hierarchy. It can be used to find files by their properties and perform subsequent operations on them. For instance, you can search file or directories by type, owner, last modified, etc, and then print, delete, or execute other commands.
In this section, we see a few examples of using find for searching and operating on multiple files. If you just need more information about searching, read search files in bash.
Find Files
Recursively find and delete all files named .DS_Store
find . -name '.DS_Store' -type f -delete
In some cases, you might want to prepend sudo
to the command.
Find User Files
Find and print all files owned by a given user (root)
find . -user root -print
Find all the files not owned by a given user
find . \! -user www-data -print
Find and Change
Find all files not owned by www-data, and change owner and group
find . \! -user www-data -exec chown www-data:www-data {} +
Rsync
Copy all files from a local directory to a remote host
rsync -rh ./local-source/ $USER@$HOST:~/remote-target/
Copy all files from local to remote, excluding files specified in an external file (one exclusion per line)
rsync -rh --exclude-from=./exclude.list ./local-source/ $USER@$HOST:~/remote-target/
Copy files from a remote host to local directory
rsync -rh $USER@$HOST:~/remote-source/ ./local-target/
0 Comments