Ubuntu Commands: A Cheat Sheet With Examples

There are numerous useful Ubuntu Linux commands at your disposal, and they actually make life easier in some cases. Conveniently, many of the Ubuntu commands provided herein will work on other Linux distributions as well.

This list of Linux commands is ever-expanding, and does not contain all Linux shell commands, but it will cover as many as possible and provide a brief example (and a description) showing how to use them. These commands are not guaranteed to have the effect that you need them to, and by continuing to read this article, your acceptance of Kompulsa’s disclaimer is assumed.

Command line
Command line interface
Table Of Contents
Start A Desktop Environment.
Displaying, Printing To The Screen, And Editing.
Installing Software.
Uninstalling Software.
Authentication And User Account Control (UAC Commands).
Download Files Via The Command Line.
Drives And Directory Management.
File Transfer Commands.
Networking Commands.
Managing Processes And System Resources.
Shutting Down, Rebooting, And Power Management Via The Terminal.

Starting Desktop Environments

If you exit your desktop environment to a shell or boot into a shell, you might want to start a desktop environment such as KDE, GNOME, LXDE, or XFCE.

Start the X Server (this will start your desktop environment as well if it is configured to do so) via the Ubuntu shell:

startx

Start XFCE 4:

exec startxfce4

Start KDE:

exec startkde

Start LXDE:

exec startlxde

In some cases, you may need to put those commands in your ~/.xinitrc file and then type ‘startx’ for them to work correctly.

Displaying, Viewing, And Editing Files

nano: Nano is a text editor that is often preinstalled on Linux distributions. You can create a new text file or edit an existing one (whether text or a configuration file). Here are two examples of its usage via the Ubuntu shell:

nano newfile.txt
nano .xinitrc

less: The ‘less’ command displays the contents of a file right there in the command prompt window, and you can scroll through it easily by pressing the enter button. An example of this command’s usage:

less filename.txt

echo: The ‘echo’ command can be used to print a value or a string to the screen, and it can also be used to write text to a file. For example, you can use it to write ‘Yay’ to a text file named yay.txt as shown below:

echo "Yay" > yay.txt

Adding Software: Installing Packages In Ubuntu

You might have to type sudo before some of these commands

apt-get install: This command installs a package in Ubuntu. An example of this command’s usage is: sudo apt-get install nano. If you want to install a package, but want to get the exact package name needed for installation (we’ll use Chromium for this example, a browser), you can use apt-cache search as shown below.

apt-cache search chromium

That should return a list of packages matching those search terms (along with their descriptions, if you’re exploring and want to find new apps), and you’ll most likely see ‘chromium-browser’ in the results. That means you can install Chromium by typing:

apt-get install chromium-browser

Removing Packages/Uninstalling Software In Ubuntu

The apt-get remove command uninstalls packages in Ubuntu. For example, you could use it like this: apt-get remove chromium-browser.

Bear in mind that apt-get remove won’t always remove associated packages (dependencies, in this case) that may have been installed.

Another useful command is ‘apt-get autoremove’. The autoremove command cleans up Ubuntu packages left behind during the (sometimes) incomplete uninstall process mentioned above. Example Usage:

apt-get autoremove

Ubuntu Authentication/User Account Commands

login: The login command can be used to log into a user account via the command line on Ubuntu. Example Usage:

login

logout: The logout command logs you out of a user account via the command line/terminal. This is useful for ssh sessions if you want to get off a VPS that you’re logged into. Example Usage:

logout

sudo: The sudo command lets a non-root account perform administrative tasks on Ubuntu (depending on whether they are in the sudoers file and the permissions set in the configuration) by typing sudo before commands they execute, and they normally have to enter a password to proceed.

This is useful if you want to let someone use your computer, while limiting their permissions. Example Usage:

sudo apt-get install package-name

su: The su command lets you quickly switch to the root account in an Ubuntu terminal window. Example Usage: type ‘su’ followed by the root password when it prompts you for it.

It is inadvisable to stay logged in as the root user on Linux.

passwd: The passwd command changes a user password. Example Usage: Type ‘passwd’ and follow the provided instructions/enter your new password.

ssh: You can use this command to connect to your remote server or other machine (for example: a server on a VPS web hosting account) for shell access without having to use your browser. Example Usage:

ssh username@domain.com

OR

ssh username@ipaddress

chmod: The ‘chmod’ command changes permissions. You can use it to set read, write, and access permissions on a file, for example. Example Usage: chmod 754 filename.txt.

chown: The ‘chown’ command changes ownership of a file or directory. Example Usage:

chown newowner plop.txt

chroot: This enables you to change the current root directory for the terminal window you’re in. Usage.

users: The ‘users’ command in Ubuntu will list the users that are currently logged in. Example Usage:

users

adduser: The ‘adduser’ command in Ubuntu creates a new user.

Example Usage:

adduser newusername

userdel: The ‘userdel’ command allows you to delete a user. Example Usage:

userdel username

visudo: This command enables you to edit the sudoers file, which you may need if creating a new user and need to give them sudo or other administrative privileges. Example Usage:

visudo

groups: The ‘groups’ command lists user groups. It can also list the groups that a particular user is in. Example Usage: groups username.

Download Files Via The Command Line On Linux

wget‘ is one of those useful commands you can use to download files via the command line. It saves URLs to a file, for example performing wget on an HTML page will download it and save it to a file. This works on not only Ubuntu, but the rest of the Linux distributions as well.

Example Usage:

wget http://domain.com/filename.txt

curl: curl is not installed on all machines, but it is found on many. You can use it download and print the contents of a file to the screen, or for other purposes as well. Example Usage:

curl http://domain.com/filename.txt

Accessing/Manipulating Drives And Directories In Linux

If you haven’t booted into a desktop environment and don’t see your partition, second hard drive, or external USB drive in Ubuntu (or any Linux distribution) you may need to mount the drive using the mount command.

‘*’ refers to the number of the device. If you find the device and it is /dev/sda6, then you would simply replace the ‘*’ with ‘6’. Example Usage: mount /dev/sda* /mnt/directory_you_created

An example command to unmount a drive:

umount /mnt/directory_you_created

Enter a directory or mounted drive:

You can use the ‘cd’ command to enter a directory by typing ‘cd directory_name‘.

List directory contents:

You can list the contents of the directory you entered by typing ‘ls‘. You can type ‘ls -a‘ to list the files and folders with their respective permissions.

Remove a directory:

You can remove a directory using the ‘rmdir’ command if it is empty. In order to remove a directory that contains files, you can use the ‘rm’ command with the parameter ‘rf’ like this: ‘rm -rf plop‘. ‘plop’ is the directory name in this case.

Copying Files And Moving Them In Ubuntu Linux

You can use the ‘cp’ command to copy files or directories in Ubuntu or any other Linux distribution. Example Usage:

cp -R home/username/filename.txt /directory/subdirectory/filename.txt

The ‘-R’ option means recursive and copies files inside the directory. Learn more about cp and its options.

Ubuntu/Linux Networking Commands

ifconfig: This can be used to provide information about running network interfaces, including their IP addresses. This can be used to derive your computer’s IP address on your local network.

If it returns ‘eth0’, then you’re probably using an ethernet connection, hence the ‘eth’, and ‘wlan0’ refers to Wi-Fi (wireless LAN). It may also be both if you have both a wired and a wireless network interface card (NIC).

ping: The ping command lets you ‘ping’ another machine such as a server to see if it responds. You can also use ping to determine if you have a working Internet connection. If you receive a response such as this:

PING google.com (IP Address) 56(84) bytes of data.
64 bytes from lga25s61-in-f14.1e100.net (IP Address): icmp_seq=1 ttl=57 time=2.17 ms

from a website using this command, your Internet connection is working: ping google.com

ifup: Start a network interface. Example Usage:

ifup eth0

ifdown: Stop a network interface. Example Usage:

ifdown eth0

whois: The whois command can provide domain info by simply typing whois domain.com.

For example:

whois microsoft.com

netstat: The ‘netstat’ command displays the Internet connections among other network statistics for your Ubuntu machine. Example Usage:

netstat

traceroute: The ‘traceroute’ command attempts to trace the route that a packet has to travel to get to the specified host.

Minimum required parameters: traceroute domain.com

Managing Processes, Closing Programs, And System Resources

crontab: The ‘crontab’ command is used to make programs run on startup in Ubuntu. Example Usage

top: The ‘top’ command displays running processes and their system resource usage such as RAM and CPU usage. Just type ‘top’ to run it and Ctrl + C to exit.

vmstat: Display virtual memory statistics. Just type ‘vmstat’ to launch it.

pkill: This shuts down processes by sending them the SIGTERM signal. Please be careful when using any kill commands. Learn about its usage thoroughly before trying it.

pgrep: pgrep will list the IDs of processes with the name you provided. For example, if you want to see all the running mysqld processes, typing ‘pregrep mysqld’ will list the IDs of the running mysqld processes.

w: The ‘w’ command lists users’ processes beside their usernames, their average system loads. Example Usage:

w

Shutting Down, Rebooting, and Power Management

shutdown: This can shut down or reboot the computer. Example Usage:

shutdown -h now

or:

shutdown -r now

The ‘-h’ option powers off the computer, and the ‘-r’ option reboots it.

reboot: This reboots the computer. Example Usage:

reboot

Facebook Comments

comments