You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

689 lines
8.5 KiB

---
3 years ago
Alias: ["VPS command-line", "command-line"]
Tag: ["Computer", "Server", "Web", "Cloud"]
Date: 2021-08-28
DocType: "Personal"
Hierarchy: "NonRoot"
TimeStamp:
location: [48.8570517, 2.3677354]
CollapseMetaTable: Yes
---
3 years ago
Parent:: [[Selfhosting]], [[Server Alias]], [[Server Cloud]], [[Server Tools]]
 
```button
name Save
type command
action Save current file
id Save
```
^button-VPSConsoleSave
 
# VPS Console Dialogue
 
```ad-abstract
title: Summary
collapse: open
A quick note to use command-line to interact with VPS.
```
 
```toc
style: number
```
 
---
 
### Connection and initialisation
 
```ad-abstract
title: Summary
collapse: open
Simple commands to start using a Virtual Private Server.
```
 
#### Connection
`ssh username@IPv4`
It is usual to change password:
`passwd`
 
#### Initialisation and updates
`sudo apt update`
`sudo apt upgrade`
 
#### User accounts
3 years ago
```ad-command
title: Add user
~~~
sudo adduser 'username'
~~~
```
3 years ago
```ad-command
title: Delete user
~~~
sudo userdel -r 'username'
~~~
```
3 years ago
```ad-command
title: Grant admin privileges
~~~
usermod -aG sudo 'username'
~~~
```
 
#### Switch between user accounts
3 years ago
```ad-command
~~~
su - (username)
~~~
```
 
#### Reboot
3 years ago
```ad-command
~~~
sudo reboot now
~~~
```
Or
3 years ago
```ad-command
~~~
sudo systemctl reboot
~~~
```
 
#### Change hostname
1. **Check the static hostname**
3 years ago
```ad-command
~~~
sudo hostnamectl
~~~
```
2. **Change the hostname**
3 years ago
```ad-command
~~~
sudo hostnamectl set-hostname (hostname)
~~~
```
 
---
 
### Securing Server access
 
```ad-abstract
title: Summary
collapse: open
This section gives an overview of how to switch signing-in to a machine without having to go through typing passwords and limiting surface of brute-force attacks.
```
 
#### Server-side RSA preparation
2 simple commands to prepare the server:
3 years ago
```ad-command
~~~
mkdir -p ~/.ssh
~~~
```
3 years ago
```ad-command
~~~
chmod 700 ~/.ssh
~~~
```
 
#### Generating a RSA key pair
On Linux & MacOS clients, the process is simple:
3 years ago
```ad-command
~~~
ssh-keygen -t rsa
~~~
```
And follow the prompts.
You can then send the public key to the server:
3 years ago
```ad-command
~~~
ssh-copy-id -i ~/.ssh/(key name).pub (user)@(server)
~~~
```
 
#### Client's computer SSH setup
##### SSH Agent
In order to active SSH Agent, run:
3 years ago
```ad-command
~~~
ssh-agent $BASH
~~~
```
To add any key to the agent:
3 years ago
```ad-command
~~~
ssh-add ~/.ssh/(key name)
~~~
```
 
##### SSH script
SSH can understand scripting for ease of use. To create and edit a config file on the local machine:
3 years ago
```ad-command
~~~
touch/vim ~/.ssh/config
~~~
```
The declaration of a connection follows this nomenclature:
3 years ago
```ad-code
~~~
Host (scriptname) (serverIP)
HostName (serverIP)
IdentityFile ~/.ssh/(private key path)
User (remoteusername)
~~~
```
Once set up, a connection can be called from Terminal with the following command:
3 years ago
```ad-command
~~~
ssh (scriptname)
~~~
```
 
#### Editing the Server's SSH config
To open the config file:
3 years ago
```ad-command
~~~
sudo (nano/vim) /etc/ssh/sshd_config
~~~
```
The following parameters enable to restrict access to the server:
3 years ago
```ad-code
~~~
// Enables SSH Key authentication
PubkeyAuthentication yes
3 years ago
// Disables password authentication (not recommended)
PasswordAuthentication no
3 years ago
// Disable root access (to diminish a known attack surface)
PermitRootLogin no
3 years ago
// Disables empty passwords
PermitEmptyPasswords no
3 years ago
// Set a Banner
Banner /etc/issue.net
3 years ago
// Manage White/Blacklists
AllowUsers (username)
AllowGroups (groupname)
DenyUsers (username)
DenyGroups (groupname)
3 years ago
// Change connection Port
Port xxxxx
3 years ago
~~~
```
After any change of the config file, restart the SSH service:
3 years ago
```ad-command
~~~
sudo systemctl restart sshd
~~~
```
**Note**:
issue.net needs to be set:
3 years ago
```ad-command
~~~
sudo nano/vim /etc/issue.net
~~~
```
With a text as set out below:
3 years ago
```ad-code
~~~
Warning! Authorised use only.
This server is the property of mydomain.example
~~~
```
3 years ago
 
#### Network monitoring
3 years ago
```ad-command
3 years ago
title: simple port monitoring
3 years ago
~~~
sudo netstat -an
~~~
3 years ago
```
3 years ago
```ad-command
3 years ago
title: acti r port monitoring
3 years ago
~~~
sudo netstat -anp (IP/TCP/UDP)
~~~
3 years ago
```
```ad-info
title: simple port stats
3 years ago
~~~
sudo netstat -sp (IP/TCP/UDP)
~~~
3 years ago
```
 
---
 
### File management
 
```ad-abstract
title: Summary
collapse: open
Simple commands to access files on the server.
```
 
#### File navigation
3 years ago
```ad-command
title: Explore current directory
3 years ago
~~~
ls -alh
~~~
```
3 years ago
```ad-command
title: Change directory
3 years ago
~~~
cd (folder path)
~~~
```
3 years ago
```ad-command
title: Find a file
3 years ago
~~~
sudo find / -iname (filename)
~~~
```
 
#### Create file
3 years ago
```ad-command
~~~
touch (filepath/name)
~~~
```
 
#### Edit file
3 years ago
```ad-command
~~~
vi (filepath/name)
~~~
```
1. Press 'i' for the edit mode
2. 'Esc' key to exit edit mode
3. Type ':wq' to save & close
 
#### Delete files & folders
3 years ago
```ad-command
title: Delete file
3 years ago
~~~
rm (file path & name)
~~~
```
3 years ago
```ad-command
title: Delete folder and contents
3 years ago
~~~
rm -r (folder path)
~~~
```
 
#### File permissions
##### Checking file permissions
3 years ago
```ad-command
title: Permissions
3 years ago
**For a file**: ls -l (file path & name)
**For a folder**: ls -ld (folder path)
```
 
##### Changing file permissions
3 years ago
```ad-command
title: Change permissions
3 years ago
~~~
chmod xxx (folder/file path)
~~~
```
For x:
1. read-only: 4
2. write: 2
3. execute: 1
3 years ago
```ad-command
title: Change owner
3 years ago
~~~
chown (owner):(group) (folder/file path)
~~~
```
3 years ago
```ad-command
title: Change group
3 years ago
~~~
chgrp -R (new group) (folder/file path)
~~~
```
 
##### Bulk changes
3 years ago
```ad-command
title: Change file permission in a folder
3 years ago
~~~
find (folder path) -type f -exec chmod xxx {} \;
~~~
```
3 years ago
```ad-command
title: Change sub-folder permission in a folder
3 years ago
~~~
find (folder path) -type d -exec chmod xxx {} \;
~~~
```
 
#### File transfer
3 years ago
Instructions to use rclone for file transfers can be found [[Server Cloud#Cloud2Cloud|here]].
 
---
 
### Backing up a server
 
#### Backup preparation
Create a directory for backup:
3 years ago
```ad-command
~~~
sudo mkdir /Backup
~~~
```
 
#### Backup creation
Best is to launch the command from the Backup folder:
3 years ago
```ad-command
~~~
cd /Backup
~~~
```
Command:
3 years ago
```ad-command
~~~
sudo tar -cvpfz /Backup/backup.tar.gz --exlude=/Backup/backup.tar.gz --exlude=/proc --exlude=/tmp --exlude=/mnt --exlude=/dev --exlude=/sys --exlude=/run --exlude=/var/cache/apt/archives --exlude=/usr/src/linux-headers* --exlude=/home/*/.gvfs --exlude=/home/*/.local/share/Trash /
~~~
```
Once created, the backup can be transferred using the [[#File transfer]] script.
 
#### Backup cleanup
After transfer, [[#Delete files folders|delete]] the .tar.gz file from its folder.
 
#### Backup restoring
1. From the server:
3 years ago
```ad-command
~~~
sudo nc -l 1024 | sudo tar -xvpzf - -C /media/backup
~~~
```
2. From the Client's machine, instruct:
3 years ago
```ad-command
~~~
cat (backup path & name.tar.gz) | nc -q 0 (hostname) 1024
~~~
```
 
Or through **FTP**:
1. Send backup to the root folder over FTP
2. Copy /boot/grub/menu.lst to menu.lst.bak
3. Restore
3 years ago
```ad-command
~~~
sudo tar xvpfz backup.tar.gz -C /
~~~
```
3 years ago
4. Recreate excluded directories
```ad-command
~~~
mkdir proc
Mkdir lost+found
mkdir mnt
mkdir sys
...
3 years ago
~~~
```
5. Replace the restored *menu.lst* file with the *.bak* created in Step 2 (dropping bak)
6. MAC address may need to be change
3 years ago
Check:
```ad-path
~~~
/etc/udev/rules.d/70-persistent-net.rules
~~~
```
 
---
 
### Manage programs
 
#### Check if program is running
And how many instances:
3 years ago
```ad-command
~~~
sudo ps ax | grep (program)
~~~
```
 
#### Check what program uses a port
3 years ago
```ad-command
~~~
sudo netstar -lntup | grep (port#)
~~~
```
 
#### List all programs
3 years ago
```ad-command
~~~
sudo apt list --installed
~~~
```
 
#### Remove a package
3 years ago
```ad-command
~~~
sudo apt remove (package name)
~~~
```
For cleaner removal:
3 years ago
```ad-command
~~~
sudo apt purge (package name)
~~~
```
 
---
 
### Documentation
 
```ad-example
title: OSXdaily
[SSH generic](https://osxdaily.com/tag/ssh/)
[All SSH commands](https://osxdaily.com/2017/02/06/list-all-terminal-commands-mac/)
[Log off user](https://osxdaily.com/2019/04/03/log-off-ssh-user/)
```
```ad-tip
title: Mediatemple
[Common SSH commands](https://mediatemple.net/community/products/dv/204643550/common-ssh-commands)
```
```ad-tip
title: Scripting OSX
[Intro to SSH for Mac admins](https://scriptingosx.com/2017/07/quick-introduction-to-ssh-for-mac-admins/)
```