By Nicholas Brown – Follow me on Twitter.
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. Bear in mind that these commands won’t necessarily work correctly on all versions of Ubuntu.
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 Ubuntu 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.
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:
Start XFCE 4:
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
How to edit or view the contents of a file in Ubuntu via command line:
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:
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
Converting a string to hexadecimal: You can convert a string to hexadecimal using the echo command as shown:
echo -n "This string will be converted to hexadecimal format" | od -A n -t x1
Adding Software: Ubuntu Commands For Package Installation
How to install an app/program in Ubuntu via command line:
You might have to type sudo before some of these Ubuntu 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 Ubuntu 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
How to uninstall an Ubuntu package via command line:
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 Ubuntu command is ‘apt-get autoremove’. The autoremove command cleans up Ubuntu packages left behind during the (sometimes) incomplete uninstall process mentioned above. Example Usage:
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:
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:
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.
Generate a random password using OpenSSL:
openssl rand -base64 16
This can also be done with the GPG tool:
gpg --gen-random --armor 1 14
Clear Scrollback/Erase Terminal Contents
If you’ve typed a password or something confidential in the terminal and need to clear your scrollback or erase your terminal history, use the following command:
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:
Shut down your SSH server:
service ssh stop
OR to start your SSH server:
service ssh start
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:
adduser: The ‘adduser’ command in Ubuntu creates a new user.
userdel: The ‘userdel’ command allows you to delete a user. Example Usage:
visudo: This Ubuntu 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:
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 Ubuntu commands (although not exclusive to Ubuntu) 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.
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:
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 Ubuntu command to unmount a drive:
Ubuntu command to 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 one of the ‘ls‘ Ubuntu commands. 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.
Create A RAM Disk To Run Your Programs From RAM (Or Load Files From RAM)
You can create a partition in RAM (use a portion of your memory as a drive) to store frequently-accessed files or programs. This will enable them to load much faster (as RAM is much faster than a hard drive).
Bear in mind that this has to be recreated every time your computer is restarted, so you may want to make these commands run on startup. You also need root priviledges to run the following commands (or the ‘sudo’ command).
mkdir -p /media/ramdisk mount -t tmpfs -o size=1024M tmpfs /media/ramdisk
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:
ifdown: Stop a network interface. Example Usage:
whois: The whois command can provide domain info by simply typing whois domain.com.
netstat: The ‘netstat’ command displays the Internet connections among other network statistics for your Ubuntu machine. Example Usage:
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’ Ubuntu 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: This Ubuntu command displays 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:
Shutting Down, Rebooting, and Power Management
shutdown: This Ubuntu command can shut down or reboot your machine. Example Usage:
shutdown -h now
shutdown -r now
The ‘-h’ option powers off your Ubuntu machine, and the ‘-r’ option reboots it.
reboot: This reboots the computer. Example Usage:
Get the path of a command/get the path of a program in Linux
You can get the path of a command or find out where a program’s binaries are installed by using the ‘dpkg-query’ command:
dpkg-query -L programname
You may see many paths. For the binaries, look for the ‘usr/bin’ paths.
Get BIOS info via command line in Linux (this works for other distributions as well):
You may need to install dmidecode first.
Check CPU Temperature In Ubuntu
You can check the temperature of your CPU, GPU and drives using the sensors command. You may have to install lm-sensors if it isn’t already installed.
sudo apt install lm-sensors sensors
You can also use the “wmtemp” command (must be installed as well).