💾 Archived View for thorjhanson.com › miscellaneous › 20210919-linux-reference-sheet.gmi captured on 2022-01-08 at 13:49:10. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

Linux Reference Sheet

Last Update: 9/19/2021

This is a very basic list of common commands used when administering a Debian system, typed up for a friend who is just getting started with Linux administration.

Moving, Viewing, Editing Files

List files:

ls

List all files, even hidden ones:

ls -a

Show where you currently are:

pwd

Change to a new folder:

cd <foldername>

For example:

cd /home/john/Documents

Print out the contents of a file:

cat <nameoffile>

Example:

cat /var/www/html/index.html

Edit a file:

nano <nameoffile>

Example:

nano /var/www/html/index.html

Move or rename a file (renaming a file is just moving it with a new name):

mv <oldfilename> <newfilename>

Example moving and renaming at the same time:

mv /home/John/Documents/oldphoto.jpg /home/John/Pictures/oldphoto1.jpg

Tips:

If a file or folder has certain types of characters in its name, such as spaces or $, you need to put '' around the file name.

Examples:

nano '/home/john/Documents/tax information.txt'

SFTP

Files can be transferred to and from the server using SFTP. SFTP is very versatile, but this will describe the most straightforward way to use it.

To start the SFTP tool, use the same command as you would for SSH, but use the word "sftp" instead of "ssh". For example, if the following is used to log into a server:

ssh -i ~/.ssh/mykey user@host

Then the SFTP tool would be started with:

sftp -i ~/.ssh/mykey user@host

When the tool starts, it acts as a connection between your machine and the server.

It simply waits for commands. There are four main commands that are most useful:

ls
cd
get
put

Use `ls` to list the files on the server.

Use `lls` to list the files on your machine.

Use `cd` to change where you are on the server.

Use `lcd` to change where you are on your machine.

So, after starting the tool, you might use `cd` to move to a location on the server where you want to upload files, and you might use `lcd` to move to a location on your machine where you have files to upload.

Once you're in place, you can use `put` to upload files, and `put -R` to upload folders.

You can use `get` to download files and `get -R` to download folders.

Here's an example uploading `dog-picture.jpg` to a website, with # in front of the explanations about what is happening;

# Start the tool
$ sftp -i ~/.ssh/myserverkey root@example

# Now the tool is started. Use ls to see what is on the server
sftp> ls
trash
old-files
notes

# We realize this isn't where we want to upload to. We want to upload to the apache website folder
# Use cd to switch there
sftp> cd "/var/www/html"

# Check what's there - to see if this is the right place
sftp> ls
blog
index.html
me-picture.jpg

# We are in the right place
# Now we want to see what is on our laptop
sftp> lls
Documents
Downloads
Pictures
Videos

# We know that we keep our website files in the Documents folder. Use lcd to switch there
sftp> lcd Documents

# List the files
sftp> lls
art
my website
school
taxes

# Here we see the folder 'my website', which we know has all the website files.
# Switch into it
sftp> lcd "my website"

# List the files
sftp> lls
blog
dog-picture.jpg
index.html
me-picture.jpg

# Here we are! The folder where we keep our website files.
# Now we can upload `dog-picture.jpg`
sftp> put dog-picture.jpg

# When it's done uploading, check to make sure it's there:
sftp> ls
blog
dog-picture.jpg
index.html
me-picture.jpg

# Now that we're done, exit sftp
sftp> exit

Just remember these steps:

1. Log in

2. Use `ls` and `cd` to get the server side in place

3. Use `lls` and `lcd` to get your machine's side in place

4. Use get or put to either download or upload the files/folders you're interested in.

It might be helpful to have an ssh connection up in one window, and an sftp connection in another one. This makes it easy to switch back and forth between uploading/downloading files and doing things like moving those files around, restarting programs, etc. without having to log back in and out.

System Administration

Run a command as administrator:

sudo <command>

Example (works with almost any command):

sudo apt update

Change your password:

passwd

Change another user's password:

passwd <username>

Example:

passwd john

Reboot the whole machine:

reboot

Power off the whole machine:

poweroff

Check the resource useage of the machine:

htop

Update everything:

apt update
apt upgrade

Search for a package by name:

apt search <packagename>

Example:

apt search htop

Install a package by name:

apt install <packagename>

Example:

apt install htop

Managing system services:

systemctl restart <service>
systemctl stop <service>
systemct start <service>
systemctl disable <service>
systemctl enable <service>

Example:

systemctl restart apache2

Etc.

View status of a service:

systemctl status <service>

Example:

systemctl status apache2