Lab No 4 Running commands as root
When working on the command line, you usually want to work with the default permissions. This way you insure that you won't accidentally break anything belonging to the system or other users, so long as the system and other users haven't altered their file permissions. Yet there will be times when you wish to copy a file to a system folder (like /usr/local/bin) to make it available to all users of the system. Only the system administrator (i.e. the user 'root') should have permission to alter the contents of system directories like /usr/local/bin. Therefore trying to copy a file (like a program downloaded from the Internet) into that folder is forbidden by default.
cp Downloads/some_downloaded_program /usr/local/bin
cp: cannot create regular file `/usr/local/bin/some_downloaded_program': Permission denied
Since it would be very tedious to always login as root to do administrative work (in fact you should avoid logging in as root with a graphical desktop) you can use the ‘sudo’ program to execute a single command as root.
sudo cp Downloads/some_downloaded_program /usr/local/bin
The default Ubuntu installation is configured so that the user who was created during system installation is allowed to use this command. It will prompt for the password and execute the command as the root user.
Opening GUI applications as root
Sometimes you will want to edit a config file in your root folder, in order to save changes to this file you need root privileges so we need to open our text editor as root.
1. (assuming your text editor is gedit)
gksudo gedit /path/to/conf_file.txt
gksudo is the same as sudo but should be used to open any graphical applications as root while sudo is intended for executing single commands.Serious damage can be done to your system by editing these files. It is advised to create a backup of any file you edit.
Backing up your files
To create a backup of a file, we're going to use the cp (copy) command. The basic syntax for cp is as follows:
cp source_file dest_file
This will copy the 'source file' to the 'dest_file'. Now, using the previous example, we want to backup '/path/to/conf_file.txt'. To accomplish this, we type the following:
sudo cp /path/to/conf_file.txt /path/to/conf_file.txt.old
That's fine and dandy, but what if I want to copy my file to another directory? Well that's just as easy. Let's say instead of copying /path/to/conf_file.txt to the /path/to/ directory, you want to copy it to a directory where you store all of your backup files, say /my/backup/folder/. In order to accomplish this you would type:
cp /path/to/conf_file.txt /my/backup/folder/ #saves conf_file.txt to /my/backup/folder/
#OR
cp /path/to/conf_file.txt /my/backup/folder/conf_file_new_name.txt
***This is a typical safety measure that has saved many users in the past from a complete disaster.***
Okay, so we know how to copy a file:
a) To a different filename
b) To a different folder. But how do we copy entire directories?
Backing up your Directories
To backup one directory to another, we introduce cp -r (recursive) option. The basic syntax is as follow:
cp -r /directory/to/be/copied/ /where/to/copy/to/
So if we wanted to copy all of the contents of our /path/to/ folder to our /my/backup/folder, we would type the following:
cp -r /path/to/ /my/backup/folder/foldername #foldername can be whatever you want the foldername to be
Checking system performance
If your computer starts to lag, you can see which applications are using the most CPU power with this command:
top
This is generally the same information given as the GUI application 'System Monitor'.
Check Devices
USB Devices:
If a USB device stops working, you may want to see if it is still connected/detected. To check if a device is connected/ detected, type the following:
Lsusb
PCI Devices:
As PCI devices are checked with:
lspci
Show network Information
To see the status of your network connection, use the command:
ip addr
This command will show you your ip, what type of connection you are using, etc.
Show wireless information
Like the command ip stated above, you can use iwconfig to check the settings of your wireless connection without editing anything. In a terminal enter:
iwconfig
This also shows packets sent/received.
Scripting
NOTE: The commands given in the scripting section are to be put into the text editor and not in the terminal unless instructed otherwise.
Bash is primarily a scripting language, so it would be a crime not to talk about scripting. Let's dive straight in with a bash script. More precisely the infamous "Hello World" script. You can create a bash script by opening your favorite text editor to edit your script and then saving it (typically the .sh file extension is used for your reference, but is not necessary. In our examples, we will be using the .sh extension).
#!/bin/bash
echo "Hello, World"
The first line of the script just defines which interpreter to use. NOTE: There is no leading whitespace before #!/bin/bash. That's it, simple as that. To run a bash script you first have to have the correct file permissions. We do this with chmod command in terminal (change mode) as follows:
chmod a+x /where/i/saved/it/hello_world.sh #Gives everyone execute permissions
# OR
chmod 700 /where/i/saved/it/hello_world.sh #Gives read,write,execute permissions to the Owner
This will give the file the appropriate permissions so that it can be executed. Now open a terminal and run the script like this:
/where/i/saved/it/hello_world.sh
Hopefully you should have seen it print Hello, World onto your screen. If so well done! That is your first Bash script.
TIP If you type:
pwd
You will see the directory that you are currently working in (pwd stands for 'print working directory'). If your current working directory is /where/i/saved/it/, then you can shorten the above command to:
prompt$ pwd
/where/i/saved/it
prompt$ ./hello_world.sh
Now, lets get to more interesting aspects of Bash programming, Variables!
ctrl+alt+T for opening the command option
pwd , so it shows the present working Directory
ls for listing out
ls-a to show all hidden file
clear for clear the screen
Comments
Post a Comment